大神们,来新问题了
为什么这样可以复制成功文件但是字节数目却变了,如下 package InputStreamUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class InputRaderDemo { public static void main(String[] args) throws IOException { Long start = System.currentTimeMillis(); File file = new File("G:/深渊党专属补丁套装/file.txt") ; File file1 = new File("G:/深渊党专属补丁套装/file1.txt") ; if(!file1.isFile()){ file1.createNewFile(); } InputStreamReader isr = new InputStreamReader(new FileInputStream(file)); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file1)); char []buffer = new char[8*1024]; isr.read(buffer,0,buffer.length); String s = new String(buffer,0,buffer.length); System.out.println(s); osw.write(buffer,0,buffer.length); Long end = System.currentTimeMillis(); isr.close(); System.out.println("花费了"+(end - start)+"毫秒"); } }
而这样却只是创建了文件,而没有复制成功,如下
package InputStreamUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class InputRaderDemo {
public static void main(String[] args) throws IOException {
Long start = System.currentTimeMillis();
File file = new File("G:/深渊党专属补丁套装/file.txt") ;
File file1 = new File("G:/深渊党专属补丁套装/file1.txt") ;
if(!file1.isFile()){
file1.createNewFile();
}
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file1));
char []buffer = new char[8*1024];
int x ;
while((x = isr.read(buffer,0,buffer.length))!=-1){
String s = new String(buffer,0,buffer.length);
System.out.println(s);
osw.write(buffer,0,x);
}
Long end = System.currentTimeMillis();
isr.close();
System.out.println("花费了"+(end - start)+"毫秒");
}
}
来吧,都来看看,共同进步