package com.yonge.day07;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class ReadWriterTXT {
//读出硬盘中的txt文件的数据。
public static String read(String path1,String path2) {
String encoding = "GBK";
String lineTxt2 = null;
String lineTxt1 = null;
File f1 = new File(path1);
File f2 = new File(path2);
//isFile()测试此抽象路径名表示的文件是否是一个标准文件。
//exists() 测试此抽象路径名表示的文件或目录是否存在。
if(f1.isFile()&&f1.exists()){
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(f1),encoding);
BufferedReader br = new BufferedReader(read);
lineTxt1 = null;
lineTxt1 = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
if(f2.isFile()&&f2.exists()){
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(f2),encoding);
BufferedReader br = new BufferedReader(read);
lineTxt2 = null;
lineTxt2 = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
return lineTxt1+lineTxt2;
}
//往硬盘中的txt文件写入数据。
public static void writer(String path,String txt){
File file = new File(path);
FileWriter writer;
try {
writer = new FileWriter(file, true);
writer.write(txt);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String path = "f:/test/dd/ss.txt";
String path1 = "f:/test/dd/ee.txt";
String path2 = "f:/test/dd/dd.txt";
String txt=read(path1,path2);
System.out.println(txt);
writer(path,txt);
}
}
将硬盘中两个TXT文件读出来,并写入另一个文件
最新推荐文章于 2024-06-04 12:01:50 发布