import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class Test2 {
public static void main(String[] args) throws Exception{
InputStream in = new FileInputStream("bbb\\aaa.txt");
FileOutputStream out =new FileOutputStream("ccc\\aaa.txt");
int len;
while ((len=in.read())!=-1){
out.write(len);
}
in.close();
out.close();
}
}
运行完代码,刷新目录后,ccc文件夹下就会出现从bbb文件夹里复制过来的文件aaa.txt
2.使用字符流复制文件
示例代码如下:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOError;
public class Test3 {
public static void main(String[] args) throws Exception{
FileReader rea = new FileReader("bbb\\aaaaaa.txt");
FileWriter wri = new FileWriter("ccc\\aaaaaa.txt");
int a;
while((a=rea.read())!=-1){
wri.write(a);
}
rea.close();
wri.close();
}
}