public void methodCopy() throws Exception{
File file1=new File("C:\\Users\\Liu\\Desktop\\【必看】资料来源.txt");
File file2=new File("C:\\Users\\Liu\\Desktop\\【必看】资料来源1.txt");
//1.使用字节流实现文本文件的复制
FileInputStream fis=new FileInputStream(file1);
FileOutputStream fos=new FileOutputStream(file2);
InputStreamReader read=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(read);
OutputStreamWriter write=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(write);
char[] c=new char[1024];
int len;
while((len=read.read(c))!=-1){
bw.write(c, 0, len);
bw.flush();
if(fos!=null){
bw.close();
}
if(fis!=null){
br.close();
}
}
}
Exception in thread "main" java.io.IOException: Stream closed
at sun.nio.cs.StreamDecoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.Reader.read(Unknown Source)
at at.xupt.liu.FileCopy.methodCopy(TestFileCopy.java:35)
at at.xupt.liu.TestFileCopy.main(TestFileCopy.java:17) |
|