public static voidmain(String[] args) throws Exception{
InputStream in = newFileInputStream("bbb\aaa.txt");
FileOutputStream out =newFileOutputStream("ccc\aaa.txt");
int len;
while((len=in.read())!=-1){
out.write(len);
}
in.close();
out.close();
}
}
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();
}
}
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
图 1
2.使用字符流复制文件
示例代码如下:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOError;
public class Test3 {
public static voidmain(String[] args) throws Exception{
FileReader rea = newFileReader("bbb\aaaaaa.txt");
FileWriter wri = newFileWriter("ccc\aaaaaa.txt");
int a;
while((a=rea.read())!=-1){
wri.write(a);
}
rea.close();
wri.close();
}
}
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();
}
}
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();
}
}