s-day09-文件复制的分析与实现(两种方式)
This commit is contained in:
@@ -36,6 +36,12 @@ public class Demo07 {
|
|||||||
|
|
||||||
byte[] bytes = new byte[1024];//用来保存字节输入流读取到的字节数据
|
byte[] bytes = new byte[1024];//用来保存字节输入流读取到的字节数据
|
||||||
int len;//用来接收读取到的字节数据的个数,如果返回-1表示读取到末尾了
|
int len;//用来接收读取到的字节数据的个数,如果返回-1表示读取到末尾了
|
||||||
|
/*
|
||||||
|
((len = fis.read(arr))!= -1)执行流程:
|
||||||
|
1.is.read(arr) :从字节输入流中最多读取数组长度的字节数据保存在数组中,返回读取到的字节的数量
|
||||||
|
2.len = fis.read(arr):返回读取到的字节的数量保存到变量len中
|
||||||
|
3.(len = fis.read(arr))!= -1:判断是否读取到字节数据的数量,如果读取到,那么就获取多少个字节数据,如果没有-1则结束循环
|
||||||
|
*/
|
||||||
while ((len = fis.read(bytes)) != -1) {//(核心代码)
|
while ((len = fis.read(bytes)) != -1) {//(核心代码)
|
||||||
System.out.println(new String(bytes,0,len));
|
System.out.println(new String(bytes,0,len));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.inmind.io_in02;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
需求:将D:/io_test/upload中的1.jpg复制到2.jpg中
|
||||||
|
*/
|
||||||
|
public class Test08 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建字节输入流
|
||||||
|
FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||||
|
//2.创建字节输出流
|
||||||
|
FileOutputStream fos = new FileOutputStream("3.jpg");
|
||||||
|
//3.不停地读写到另一个文件中(边读边写)
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
//方式一:一次读写一个字节
|
||||||
|
/* int c;//用来记录读取的字节数据
|
||||||
|
while ((c = fis.read()) != -1) {
|
||||||
|
fos.write(c);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
//方式二:一次读写一个字节数组!!
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = fis.read(bytes))!=-1){
|
||||||
|
fos.write(bytes,0,len);
|
||||||
|
}
|
||||||
|
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
System.out.println("复制所需的毫秒值:"+(end-start));
|
||||||
|
|
||||||
|
//4.资源释放
|
||||||
|
fis.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user