IO流基础-------------------------------字节流输入输出

概念

每一个文件都是以字节形式存储,字节是计算机中最小的存储单元。

所以文件字节流的特点是文件当中每次只操作一个字节。

一个字节占8个二进制位,所以叫字节输出流。

所有文件都可以用字节流写入,因为他是最小存储单元。

ouputstream 输出流  往文件里写

inputstream 输入流  往对象里写 往程序里写


字节输出流
   java.io.OutputStream 所有字节输出流的超类
   作用: 从Java程序,写出文件
   字节: 这样流每次只操作文件中的1个字节
   写任意文件
   
   方法都是写文入的方法
   write(int b) 写入1个字节

   write(byte[] b) 写入字节数组

   write(byte[] b,int,int)写入字节数组,int 开始写入的索引, int 写几个

   close() 方法,关闭流对象,释放与次流相关的资源   

  流对象,操作文件的时候, 自己不做,依赖操作系统


OUTPUTSTREAM基本操作 输出流 往文件里写

	public static void main(String []args) throws IOException{

		FileOutputStream output = new FileOutputStream("E:\\io\\output.txt");
		
		byte [] file ={1,2,3,4,5};
		//output.write(100);也可以只写一个字节
		output.write(file);
		//假如E:\\io\\output.txt 有数据将会被直接覆盖
		output.close();
	}

也可以直接String类型获取bytes写入

		FileOutputStream output = new FileOutputStream("E:\\io\\output.txt");
		
		String hello = new String ("Hello World!");
		
		output.write(hello.getBytes());
		output.close();

之前都是写入直接覆盖下面提供续写换行方法

在构造方法后面加上true就会设置为续写

		FileOutputStream output = new FileOutputStream("E:\\io\\output.txt",true);
		
		String hello = new String ("Hello World!");
		
		output.write(hello.getBytes());
		output.close();
	}

INPUTSTREAM基本操作 输入流 往程序里写

概念 每次只读取一个字节

/*
 *   字节输入流
 *     java.io.InputStream 所有字节输入流的超类
 *   作用: 读取任意文件,每次只读取1个字节
 *   读取的方法  read
 *     int  read() 读取1个字节
 *     int  read(byte[] b) 读取一定量的字节,存储到数组中

 */

public static void main(String []args) throws IOException{

		FileInputStream is = new FileInputStream("E:\\io\\output.txt");
		//读取一个字节,调用read方法 返回int
		int read = is.read();
		
		System.out.println(read);
		
		read = is.read();
		
		System.out.println(read);
		
		read = is.read();
		
		System.out.println(read);
		
		//read返回值是读取到的字节,假如后续没有数据了,read方法会返回-1
		//返回-1
		//返回-1
		//返回-1
		//所以可以拿-1判断是否还有后续数据
		is.close();
	}

所以可以拿-1去循环判断

public static void main(String []args) throws IOException{

		File outoput = new File("E:\\io\\output.txt");
		
		FileInputStream is = new FileInputStream(outoput);
				
	    int len = 0;
	    
	    while (  (len = is.read()) != -1   ){
	    	
	    	System.out.println(len);
	    }
                is.close();	}  
/*
 *  FileInputStream读取文件
 *   读取方法  int read(byte[] b) 读取字节数组
 *   数组作用: 缓冲的作用, 提高效率
 *   read返回的int,表示什么含义 读取到多少个有效的字节数
 */
public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream("c:\\a.txt");
		//创建字节数组 开辟了1024长度 一般都是1024或1024的倍数
		byte[] b = new byte[1024];
		
		int len = 0 ;
		while( (len = fis.read(b)) !=-1){
			System.out.print(new String(b,0,len));
		}
		fis.close();
	}

文件的基本复制   读一个字节写一个字节 这样写的弊端就是效率很慢 假如一个文件4mb 换算字节 400多万个字节 

循环400多万次 考虑一下

public static void main(String []args) throws IOException{

		FileOutputStream os = null;
		FileInputStream is = null;
		
		try{
			
			is = new FileInputStream("E:\\io\\itest.jpeg");
			
			os = new FileOutputStream("E:\\iocopy\\itest.jpeg");
			
			int len = 0;			
			
			while((len = is.read()) != -1){
				
				os.write(len);
			}
			
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			
			os.close();
			is.close();
		}
	
		
	}
/*
 *  字节流复制文件
 *   采用数组缓冲提高效率
 *   字节数组
 *   FileInputStream 读取字节数组
 *   FileOutputStream 写字节数组

 */

public static void main(String []args) throws IOException{

		FileOutputStream os = null;
		FileInputStream is = null;
		
		long startTime = 0;
		
		try{
			startTime=System.currentTimeMillis();
			
			is = new FileInputStream("E:\\io\\nox_setup_v6.1.0.0_full.exe");
			
			os = new FileOutputStream("E:\\iocopy\\nox_setup_v6.1.0.0_full.exe");
			
			//定义一个缓冲数组 目前测试 1024*1024效率比较高 比1024*1024*1024高
			byte [] b = new byte[1024*1024];
			
			//读取数组,写入数组
			int len = 0;
			
			while((len = is.read(b)) != -1){
				//第一个参数是从哪个数组写,第二个是从哪开始写,第三个是他每次读到的字节个数
				//可以按照原理图理解  第一次过来读到了1024个字节 len 是1024 ,第二次读到了1024个字节 len 是1024
				//第二次读到了1024个字节 len 是1024 。。。。。 到末尾只读到了521个新字节  那么 len 就是 521
				//os.write(b, 0, len);就会是os.write(b, 0, 521); 是写入 字节数组的前面的521个字节
				os.write(b, 0, len);
			}
			
		}catch(Exception e){
			
			e.printStackTrace();
		}finally{
			
			os.close();
			is.close();
			long endTime=System.currentTimeMillis();

			System.out.println("程序运行时间: "+(endTime-startTime)+"ms");

			
		}
	
		
	}

上图是字节数组读取数据的原理图---------------------------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/jiulanhao/article/details/80815873