您的位置:首页 > 编程语言 > Java开发

Java读写文件夹下的txt文件,已读的txt文件修改后缀名为.bat。

2014-12-08 14:53 483 查看
package test1;
/**
* 一个文件夹,里面包括很多个txt文件或者子文件夹,依次读取文件夹里面的内容,
* 以%~、~%开始结尾为一条数据,依次遍历解析到需要的数据。
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

public class Big {

public static  final String SRC_FILE_NAME = "F:\\java\\testRead";

public static  final String DEC_FILE_NAME = "F:\\java";

private static final int FILE_ROWS = 3;		//每个文档写的行数

static OutputStreamWriter output = null;

static int indexFlag = 1;		                //写的文件标识

static int count = 0;                           //记录条数

static final String separator = ":";

static final String balnkSpace = " ";

static final String A = "A";

static final String B = "B";

static final String C = "C";

static final String D = "D";

public static boolean readFile(String filepath) throws FileNotFoundException {
//OutputStreamWriter 将字节流转换为字符流。
output = new OutputStreamWriter(new FileOutputStream(new File(SRC_FILE_NAME + "newFile_" + indexFlag++ +".txt")));

File file = new File(filepath);

if (file.isDirectory()) {
//过滤只解析后缀为.txt的文件
String[] filelist = file.list(new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
if(name.endsWith(".txt")) {
return true;
}
return false;
}
});

for (int i = 0; i < filelist.length; i++) {
File txtfile = new File(filepath + "\\" + filelist[i]);
if (!txtfile.isDirectory()) {
try {
readTxtFile(txtfile);
} catch (Exception e) {
e.printStackTrace();
}

} else if (txtfile.isDirectory()) {
readFile(filepath + "\\" + filelist[i]);
}
}
}
return true;
}
/**
* 读取txt文件,获取需要的字段,写入新的txt文件,当txt文件解析完毕,修改txt文件后缀。
* @param fileName
* @return
* @throws Exception
*/
public static String readTxtFile(File fileName) throws Exception {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
StringBuffer buffer  = new StringBuffer();
try {

fileReader = new FileReader(fileName);
bufferedReader = new BufferedReader(fileReader);
try {
String read = null;
Map<String, String> dataMap = new HashMap<String, String>();

while ((read = bufferedReader.readLine()) != null) {
int indexOf = read.indexOf(separator);

if (indexOf > 0) {
String key = read.substring(0,indexOf);
String value = read.substring(indexOf+1);
if (value !=null || !value.equals("")) {
dataMap.put(key, value);
}
}

if (read.contains("~%")) {
if (dataMap.get(A)!=null && dataMap.get(B)!=null) {
buffer.append(dataMap.get(A));
buffer.append(balnkSpace);

buffer.append(dataMap.get(B));
buffer.append(balnkSpace);

buffer.append("\r\n");
dataMap.clear();

String res = buffer.toString();
//判断输入的条数,如果大于50000则新建一个txt文件
count++;
if (count % 3 == 0) {
output.flush();
output.close();
output = new OutputStreamWriter(new FileOutputStream(new File(SRC_FILE_NAME + "newFile_" + indexFlag++ +".txt")));
}
/**
* 符合条件的信息写入新的txt文档。
*/
output.write(res);
}

}
}
/**
* 注意:此处需要关闭流才能修改后缀成功。WHY?
* 关闭是为了回收资源,IO设备在打开时会占用资源。如果不用close关闭,占用的资源就不会释放。
*/
bufferedReader.close();
//当一个txt读完,修改该文档的后缀名为.bat
update(fileName);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
if (fileReader != null) {
fileReader.close();
}
}
return buffer.toString();
}

private static void update(File file) {
String filename = file.getAbsolutePath();
if (filename.indexOf(".") >= 0) {
filename = filename.substring(0,filename.lastIndexOf("."));
}
file.renameTo(new File(filename+".bat"));   //改名
}

public static void main(String[] args) {
try {
readFile("F:\\java\\testRead");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐