java字符流字节流复制文件

2024-04-23 18:09:55 浏览数 (2)

一、通过字符流复制文件

import java.io.*; public class Test { public static void main(String args[]) throws IOException{ File f1 = new File("e:/java/code/"); f1.mkdirs(); File f2 = new File(f1,"aa.txt"); f2.createNewFile(); int n; FileReader in = new FileReader("e:/java/code/aa.txt"); FileWriter ci = new FileWriter("e:/java/code/copy.txt"); while((n=in.read())!=-1) { ci.write(n); System.out.print((char)n); } in.close(); ci.close(); } }

二、通过字节流复制文件

import java.io.*; public class Test1 { public static void main(String args[]) throws IOException{ File f1 = new File("e:/a/b"); f1.mkdirs(); File f2 = new File(f1,"aa.jpg"); f2.createNewFile(); int n; InputStream in = new FileInputStream("e:/java/code/aa.jpg"); OutputStream ci = new FileOutputStream("e:/java/code/copy.jpg"); while((n=in.read())!=-1) { ci.write(n); } in.close(); ci.close(); } }

0 人点赞