java将一个文件的内容复制到另一个文件。
使用BufferedWriter类的read和write方法将一个文件的内容复制到另一个文件。
完整代码
import java.io.*;public class Main {public static void main(String[] args) throws Exception {BufferedWriter out1 = new BufferedWriter(new FileWriter("onefile"));out1.write("string to be copied\n");out1.close();InputStream in = new FileInputStream(new File("onefile"));OutputStream out = new FileOutputStream(new File("twofile"));byte[] buf = new byte[1024];int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}in.close();out.close();BufferedReader in1 = new BufferedReader(new FileReader("twofile"));String str;while ((str = in1.readLine()) != null) {System.out.println(str);}in1.close();}
}
结果输出
string to be copied