qq_什么亡魂溺海没有跳海实在_03283094
2017-03-07 22:12
package IsPackage; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; public class IsrAndOswDemo { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream("e:\\javaio\\imooc.txt"); InputStreamReader isr = new InputStreamReader(in); int c; while((c = isr.read())!=-1){ System.out.print((char)c); } char[] buffer = new char[8*1024]; //int c; //while((c=isr.read(buffer,0,buffer.length))!=-1){ //String s= new String(buffer,0,c); //System.out.print(s); // //} FileOutputStream out = new FileOutputStream("e:\\javaio\\imooc2.txt"); OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8"); while((c=isr.read(buffer,0,buffer.length))!=-1){ String s = new String(buffer,0,c); //System.out.print(s); osw.write(buffer,0,c); osw.flush(); } osw.close(); isr.close(); } }
因为你在复制到文件之前,通过 int c; while((c = isr.read())!=-1){ System.out.print((char)c); }给字节全部读完了,并打印出来,再次拿isr这个对象去read的时候,是读取不到字节了
你可以将
while((c = isr.read())!=-1){
System.out.print((char)c); } 注释掉
或者放在
while((c=isr.read(buffer,0,buffer.length))!=-1){
String s = new String(buffer,0,c);
// System.out.print(s);
osw.write(buffer,0,c);
osw.flush();
} 后面
就可以成功复制了
文件传输基础——Java IO流
133754 学习 · 1030 问题
相似问题