java:IO流(字节数组拷贝之available()方法---不推荐使用)

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_24644517/article/details/83415148

* int read(byte[] b):一次读取一个字节数组
* write(byte[] b):一次写出一个字节数组
* available()获取读的文件所有的字节个数

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo3_Copy {

	public static void main(String[] args) throws IOException {
        ----不推荐使用,文件过大时,有可能导致内存溢出
		FileInputStream fis=new FileInputStream("D:\\图片\\图片.jpg");//创建输入流对象,关联图片
		FileOutputStream fos=new FileOutputStream("copy.jpg");//创建输出流对象,关联copy
		int len=fis.available();
		System.out.println(len);
		
		byte[] arr=new byte[len];//创建与文件一样大小的字节数组
		fis.read(arr);//将文件上的字节读取到字节数组中(内存上-容易内存溢出)
		fos.write(arr);//将字节数组中的数据写到文件上
		fis.close();//关流释放资源
		fos.close();
		System.out.println("已执行完毕");
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_24644517/article/details/83415148
今日推荐