Java 实例 - 将文件内容复制到另一个文件
程序员文章站
2022-06-03 21:17:33
...
以下实例演示了使用 BufferedWriter 类的 read 和 write 方法将文件内容复制到另一个文件:
/* author by w3cschool.cc Main.java */import java.io.*;public class Main { public static void main(String[] args) throws Exception { BufferedWriter out1 = new BufferedWriter (new FileWriter("srcfile")); out1.write("string to be copied\n"); out1.close(); InputStream in = new FileInputStream (new File("srcfile")); OutputStream out = new FileOutputStream (new File("destnfile")); 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("destnfile")); String str; while ((str = in1.readLine()) != null) { System.out.println(str); } in.close(); }}
以上代码运行输出结果为:
string to be copied
以上就是Java 实例 - 将文件内容复制到另一个文件的内容,更多相关内容请关注PHP中文网(www.php.cn)!