Java基础 文件拷贝的代码及改进

基本代码:

FileInputStream fis = new FileInputStream("E:\\Java基础资料\\a.txt");
FileOutputStream fos = new FileOutputStream("E:\\Java基础资料\\b.txt" );

while (true) {
int b = fis.read();
if (b == -1)
break;
fos.write(b);
}

fos.close();
fis.close();

----------------------------------------------------------------

改进:

FileInputStream fis = new FileInputStream("E:\\Java基础资料\\a.txt");
FileOutputStream fos = new FileOutputStream("E:\\Java基础资料\\b.txt" );

byte[] bytes = new byte[5];
while (true) {
int len = fis.read(bytes);
if (len == -1)
break;
fos.write(bytes, 0, len);
}

fos.close();
fis.close();

posted @ 2023-10-23 15:25  1stzz1  阅读(11)  评论(0)    收藏  举报