zoukankan      html  css  js  c++  java
  • 07 IO流(四)——文件字节流 FileInputStream/FileOutputStream与文件的拷贝

    两个类的简述

    专门用来对文件进行读写的类。

    父类是InputStream、OutputStream

    文件读入细节

    FileOutputStream流的构造方法:new FileOutputStream(File f,boolean append),第二个参数默认值为false,表示数据不追加到文件,也就是直接覆盖文件,

    缓冲池细节

    1. 将数据读入程序时,用byte数组存储,起到一个缓冲池的作用。
    2. byte数组(缓冲池)的初始长度写法应为:new byte[1024*n];使用1024乘n的方式更能直观的表现出缓冲池的大小。
    3. byte数组,一个长度代表一个字节(Byte)。

    FileInputStream的read(byte[] b)方法细节

    1. int read(byte b)
    2. 返回值为int型,返回的是单次读入的数据长度,若无数据可读,则返回-1。
    3. 该方法将读到的数据存到b数组中。

    文件写入细节

    FileOutputStream的write方法

    1. void write(byte[] b)
    2. void write(byte[] b,int offset,int length) //从数组b中,从offset偏移量开始,写入lenght长度的数据到输出流指向的文件中去。

    写完之后,使用flush方法手动刷新(流关闭时会自动刷新一次)。

    写入的是byte[]数组,所以,也可以将字符串使用String.getBytes()方法得到数组写入文件。例如从网络爬到的字符串。

    关闭流细节

    先打开的后关闭

    各个流分别关闭,分别写自己的try-catch

    需要捕捉的异常

    1.读入时

    在流搭建的时候(InputStream fis = new FileInputStream(src)),可能存在读取的文件源src不存在的情况,就要报FileNotFoundException,捕获之。

    2.写入时

    写入时,有可能报IOException异常,捕获之。

    3.关闭流时

    关闭流时可能报IOException,捕获之。

    案例1:从文件到文件的数据读写——文件拷贝

    流程为

    1. 新建文件源,包括输入文件源,输出文件源
    2. 新建流,包括FileInputStream输入流与FileOutputStream输出流
    3. 新建缓冲区byte数组
    4. 将数据从输入文件源读入缓冲区
    5. 将数据写入输出文件源
    6. 关闭流

    代import java.io.*;

    public class IO_Test1
    {
    	public static void main(String[] args){
    		//例子1:拷贝文件(将一个文件的数据读入并写入另一个文件中)
    		//1.创建文件源	
    			//文件源
    		File src = new File("a1.txt");
    			//输出文件
    		File des = new File("a2.txt");
    		//2.选择流:先初始化流
    			//输入流
    		InputStream fis = null;
    			//输出流
    		OutputStream fos = null;
    		try{	
    		fis = new FileInputStream(src);
    		fos = new FileOutputStream(des);//FileOutputStream(String/File s/f,bolean append)false表示不追加
    		//3.操作
    			//读取文件1的数据
    				//定义缓冲池
    		byte[] datas = new byte[1024*3];//单词读取3kB
    				//定义一个变量用以接收单次读入数据的长度
    		int length;
    				//开始读取
    		while((length = fis.read(datas))!=-1){ //int read(byte[] byteArray)方法:fis对象读取数据至数组中,返回值为读取的数据的长度。当为空时,返回-1
    				
    			//写入文件2中
    			fos.write(datas,0,length);//boolean write(byte[] byteArray,int offset,int length,) 将数组中的数据通过输出流写入,从数组的offset位开始,长度为length
    				//刷新
    			fos.flush();
    			}
    		}catch(FileNotFoundException e1){
    			e1.printStackTrace();
    			System.err.println("找不到文件");
    		}catch(IOException e2){
    			e2.printStackTrace();
    			System.err.println("IO异常");
    		}finally{
              //4.关闭流 try{ fos.close(); }catch(IOException e3){ e3.printStackTrace(); System.out.println("流关闭异常"); }
                  try{
                       fis.close();
                  }catch(IOException e4){
                      e4.printStackTrace();
                  } } } }

      

    案例2:从网络数据等到文件的写入

    流程

    1. 新建输出文件源
    2. 新建FileOutputStream输出流
    3. 将字符串(如网络爬下来的)调用getBytes方法以获得存储着数据的byte数组
    4. 将数据写入输出文件源
    5. 关闭流

    代码

    import java.io.*;
    public class IO_Test1
    {
    	public static void main(String[] args){
    
    		//例子2:将一个字符串的内容写入文件2
    		//1.文件源
    			//String文件源
    		String str = "是个傻子";
    			//将字符串编码为byte数组
    		byte[] strByte = str.getBytes();
    			//输出文件源
    		File fw = new File("fw.txt");	
    		//2.选择流:初始化一个输出流
    		FileOutputStream fos2 = null;
    		try{
    			//与输出文件源建立输出流
    			fos2 = new FileOutputStream(fw);
    		//3.操作
    			//通过输出流写入数组中的数据
    			fos2.write(strByte);
    			fos2.flush();//刷新
    		}catch(IOException e4){
    			e4.printStackTrace();
    			System.err.println("例子2失败");
    		}finally{
    		//4.关闭流
    			try{
    				if(null!=fos2){
    					fos2.close();
    					System.out.println("例子2成功!");
    				}
    			}catch(IOException e5){
    				e5.printStackTrace();
    				System.out.println("IO流关闭异常");
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    设计模式系列
    设计模式系列
    设计模式系列- 抽象工厂模式
    设计模式系列
    Python3 系列之 编程规范篇
    【ABAP系列】SAP ABAP BDC_OKCODE 解释
    【ABAP系列】SAP ABAP MIR7预制凭证BAPI
    【ABAP系列】SAP ABAP 的替代和校验
    【ABAP系列】SAP ABAP 开发中的SMARTFORMS 参数
    【ABAP系列】SAP ABAP 实现FTP的文件上传与下载
  • 原文地址:https://www.cnblogs.com/Scorpicat/p/11911942.html
Copyright © 2011-2022 走看看