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

Java批量读取txt文件并转换为.bat
这个Java程序用于遍历指定文件夹下的所有txt文件,读取以%~、~%标记的数据,将内容按指定格式写入新txt文件。读取完成后,原txt文件被修改为.bat后缀。
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();
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值