在Java中怎样利用输出输入流将一个记事本中的内容写入另一个记事本中.谁给我做个答案!

2025-01-01 07:57:09
推荐回答(4个)
回答1:

大体思路如下...楼主可以试试

public static void main(String[] args) {
try {
FileReader reader = new FileReader("d:/file.txt");//流出文件
BufferedReader br = new BufferedReader(reader);
FileWriter writer = new FileWriter("d:/file1.txt");//流入文件
BufferedWriter bw = new BufferedWriter(writer);
String str;
while (br.ready()) {
str = br.readLine();//逐行读出
bw.write(str);//逐行写入
bw.newLine();//换行
}
br.close();
bw.close();
reader.close();
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

回答2:

import java.io.*;
public class TestFileOutputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("d:/share/java/HelloWorld.java");
out = new FileOutputStream("d:/share/java/io/HW.java");
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件"); System.exit(-1);
} catch (IOException e1) {
System.out.println("文件复制错误"); System.exit(-1);
}
System.out.println("文件已复制");
}
}

回答3:

package test;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class ReadFile {

public static void main(String[] args) {
File f1 = new File("text1.txt");
File f2 = new File("text2.txt");
int b=0;
try {
FileReader reader = new FileReader(f1);
FileWriter writer = new FileWriter(f2);
while((b=reader.read())!=-1){
writer.write(b);
}
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}

}

}

回答4:

先读出来再写..代码自己去看看.io吧 列子实在太多了...