请教:在使用字节流复制文件时候,使用read重载方法时,运行的结果却不一样。
public class FileCopyTest {
public static void main(String[] args) {
File file2 = new File("F:\\test\\青春.txt");
CopyByFileInputStream(file2);
}
/**
* 使用字节流复制文件,创建输入输出流,利用循环进行读写
* @param file
*/
public static void CopyByFileInputStream(File file) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream("F:\\test\\1\\青春13.txt");
int by;
while((by=fis.read())!=-1){
fos.write(by);
System.out.println("复制成功");
}
byte[] words = new byte[1024];
while (fis.read() != -1) {
fis.read(words);
fos.write(words, 0, words.length);
System.out.println("复制成功1!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第一种,使用 read() 读取时
第二种,使用read(byte[] b) 读取时