Java将文件目录下的所有TXT输入到同一个TXT当中

 负责维护某系统写的

主要是为了将一个文件目录下的所有TXT文件都写到同一个文件中,还对不同行之间保留了换行。参看writeALL

这个类还包括了删除一个目录下所有的文件,由于我确定目录下不会有文件夹因此,没有写是否为目录的判断操作

感觉写的不算太好,啰啰嗦嗦的,随意看吧

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class TXTHandle {
	String folderpath;//文件路径获取
	String TXTpath;//保存的TXT路径+名称
	public TXTHandle(String FileNameStr) {
		File temp = new File(FileNameStr);
		this.folderpath=temp.getName();
		this.folderpath=FileNameStr.replace(folderpath, "");
		this.TXTpath=FileNameStr.replace("XX.xls", "All.txt");
	}
	
	//本类是负责对输出ALLText文件以及删除所有目前存在的txt文件而存在的
	public boolean writeAll() {
		System.out.println(this.folderpath);
		File dir = new File(this.folderpath);
		File[] files = dir.listFiles();// 该文件目录下文件全部放入数组
		File all=new File(this.TXTpath);
		//文件不存在则创建,注意已存在文件就会继续在之前的文件上绣
		if(!all.exists())
		{
			try {
				all.createNewFile();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		//追加的方式,创建输出流
		FileOutputStream fs=null;
		try {
			fs = new FileOutputStream(all, true);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fs);
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()&& files[i].getAbsolutePath().endsWith(".txt")){
					System.out.println(files[i].isFile() + " " + files[i].getAbsolutePath());
					String str="";
					try {
						str = readFileContent(files[i]);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						//bufferedOutputStream.write(hexStr2Byte(codeStr));
						bufferedOutputStream.write(str.getBytes());

					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
				}
			}
			try {
				bufferedOutputStream.close();
				fs.close();	
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
					
			return true;
		}

		return false;
	}
	public static String readFileContent(File file) throws IOException{
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		StringBuffer sb = new StringBuffer();
		while(br.ready()){
			sb.append(br.readLine());
			sb.append("\r\n");
		}
		br.close();
//		sb.append("\r\n");
//		System.out.println(sb.toString());
		return sb.toString();
	}
	public boolean deleteALLTXT(){
		//注意本方法是删除所有目前存在的TXT文件,以防多次写入
		File file = new File(this.folderpath);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()&& files[i].getAbsolutePath().endsWith(".txt")){
					files[i].delete();
				}
			}
			return true;
		}
		return false;
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就是一只白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值