IO_字节流_文件写入、写出

概念

流:流向,从一端到另一端     源头与目的地

程序 与 文件、数组、网络连接、数据库 ——以程序为中心

IO流分类

1、按流向:输入流和输出流

2、按数据:字节流:二进制——可以处理一切文件,包括 纯文本、doc、音频、视频等

                   字符流:文本文件——只能处理纯文本

3、按功能:节点:包裹源头

                   处理:增强功能,提供性能

字符流与字节流(重点)

1、字节流

        输入流:InputStream  方法:read(byte[ ] b)、read(byte[ ] b,int off, int len)+close( )

        输出流:OutputStream  方法:write(byte[ ] b)、write(byte[ ] b,int off, int len)+flush( ) +close( )

2、字符流

        输入流:Reader

        输出流:Writer

1、举例

举例:搬家 ——> 读取文件
1、关联房子 ——> 建立与文件的联系
2、选择搬家 ——> 选择对应的流

3、搬家

    a、卡车大小

    b、运输

——>

读取 / 写出

    数组大小

    读取/写出

4、打发over ——> 释放资源

2、操作

1)建立联系    File对象 源头

2)选择流    文件输入流 InputStream FileInputStream

3)操作    数组大小+read / write    byte[ ] car = new byte[1024] 

4)  释放资源

读取文件
/**
 * 文件的读取
 * 1)建立联系    File对象
 * 2)选择流    文件输入流 InputStream FileInputStream
 * 3)操作    数组大小+read / write    byte[ ] car = new byte[1024]
 * 4)释放资源
 * @author 
 * @date 2018/5/27 20:41
 */
public class Steam {
    public static void main(String[] args) {

        //1、建立联系
        String path = "F:/test/1.txt";
        File file = new File(path);
        //2、选择流
        InputStream is = null;//提高作用域

        {
            try {
                is = new FileInputStream(file);
                //3、操作——读取,不断的读取,缓冲数组
                byte[] car = new byte[1024];
                int len = 0;//接收实际读取的大小
                //循环读取
                while (-1!=(len = (is.read(car)))){//-1表示没有数据了
                    //输出 需要将字节数组转成字符串
                    String  str = new String(car,0,len);
                    System.out.println(str);
                }
            } catch (FileNotFoundException e) {
                System.out.println("文件不存在");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("读取文件失败");
                e.printStackTrace();
            }finally {
                //4、释放资源
                if (null!=is){
                    try {
                        is.close();
                    } catch (IOException e) {
                        System.out.println("关闭文件输入流失败");
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

写出文件

2、操作

1)建立联系    File对象 目的地

2)选择流    文件输出流 OutputStream FileOutputStream

3)操作    write( )+flush

4)释放资源

/**
 * 2、操作
 * 1)建立联系    File对象 目的地
 * 2)选择流    文件输出流 OutputStreams FileOutputStream
 * 3)操作    write( )+flush
 * 4)释放资源
 * @author 
 * @date 2018/5/27 21:01
 */
public class OutputStreams {
    public static void main(String[] args) {

        //1)建立联系    File对象 目的地
        String path = "F:/test/1.txt";
        File file = new File(path);
        //2)选择流    文件输出流 OutputStreams FileOutputStream
        OutputStream os = null;
        try {
            //以追加的形式写出文件
            os = new FileOutputStream(file,true);//false表示覆盖,true表示追加
            //操作
            String str = "You are so excellent!";
            //字符串转字符数组
            byte[] bytes = str.getBytes();
            os.write(bytes);

            os.flush();//强制刷新出去
        } catch (FileNotFoundException e) {
            System.out.println("文件未找到");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("文件写出失败");
            e.printStackTrace();
        }finally {
            //4、释放资源
            if(null!=os){
                try {
                    os.close();
                } catch (IOException e) {
                    System.out.println("关闭输出流失败");
                    e.printStackTrace();
                }
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_30604989/article/details/80471512