java中导出txt文件公共方法

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

public class WriteAndReadText {
               //textPath指要导出的文件的路径
	private String textPath;

	public String readText(String textname) {
		File file = new File(textPath + File.separator + textname);
		try {
			BufferedReader br = new BufferedReader(new java.io.FileReader(file));
			StringBuffer sb = new StringBuffer();
			String line = br.readLine();
			while (line != null) {
				sb.append(line);
				line = br.readLine();
			}
			br.close();
			return sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public byte[] readTextToBype(String textname) {
		File file = new File(textPath + File.separator + textname);
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			byte[] inOutb = new byte[fileInputStream.available()];
			fileInputStream.read(inOutb);
			fileInputStream.close();
			return inOutb;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
                 //textname:要导出的文件的名字;date:要导出的内容	
	public boolean writeText(String textname, String date) {
		boolean flag = false;
		File filePath = new File(textPath);
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		try {
			PrintWriter writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(textPath + File.separator + textname),"UTF-8"));
			writer.write(date);
			flag = true;
			if (writer != null)
				writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return flag;
	}
	
	public boolean writeByteText(String textname, byte[] date) {
		boolean flag = false;
		File filePath = new File(textPath);
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		try {
//			PrintWriter writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(textPath + File.separator + textname),"utf-8"));
			
			FileOutputStream fileOutputStream = new FileOutputStream(textPath + File.separator + textname);
			//Writer out = new OutputStreamWriter(fileOutputStream, "utf-8");
			fileOutputStream.write(date);
			flag = true;
			fileOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public boolean appendText(String textName, String date) {
		boolean flag = false;
		File filePath = new File(textPath);
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		try {
			FileWriter fw = new FileWriter(
					textPath + File.separator + textName, true);
			fw.append(date);
			flag = true;
			if (fw != null)
				fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}

	public String getTextPath() {
		return textPath;
	}

	public void setTextPath(String textPath) {
		this.textPath = textPath;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值