发布网友 发布时间:2022-04-25 14:41
共2个回答
热心网友 时间:2023-10-09 14:57
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class CompareFile {
public static List<String> readFile(File file) throws IOException {
FileInputStream input = null;
InputStreamReader in = null;
BufferedReader reader = null;
List<String> list=new ArrayList<String>();
String code= "";
try {
input = new FileInputStream(file);
in=new InputStreamReader(input , "gbk") ;
reader = new BufferedReader(in);
while((code=reader.readLine())!=null ){
if(code.length()>1){
list.add(code);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(input!=null){
input.close();
}
if(in!=null){
in.close();
}
if(reader!=null){
reader.close();
}
}
return list;
}
public static void writeFile(List<String> list,String file) {
File file1 = new File(file);
PrintWriter writer=null;
try {
writer = new PrintWriter(file1);
for(String str:list){
writer.write(str+"\r");
writer.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(writer==null&&writer.equals("")){
writer.close();
}
}
}
public static void main(String[] args) {
try {
/*mapToString( sortByValue(sortMap( )));*/
writeFile( readFile(new File("D:/suiji.txt")),"D:/new.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
热心网友 时间:2023-10-09 14:58
利用Java IO包中的FileReader类从一个文本文件中读入信息到内存,然后再把内存中的信息利用FileWriter写到另一个文本文件。