【Java】Input,Output,Stream I/O流 01 概述 & 4个基础流

Input,Output,Stream IO流

I/O Input/Output缩写、I/O技术用于处理设备之间的数据传输,读写文件,网络通讯

Java程序对于数据的操作以Stream流的形式进行

io包提供了各种流的类与接口,以获取不同种类型的数据,并通过标准方式输入输出数据

输入和输出的关系

Input 输入  是从数据存储文件中把数据读取到内存中的过程【硬盘  -> 内存】

Output输出  是从内存中把数据写入到存储文件中的过程 【内存 -> 硬盘】

流的分类

 - 按数据单位分:字节流 8bit、字符流 16bit

- 按流的方向分:输入流、输出流

- 按流的角色分: 节点流 处理流

抽象基类4个

- 输入流  InputStream

- 输出流  OutputStream

- 读取流  Reader

- 读写流  Writter

继承的实现就是这4个实现类加前缀名称,节点流、缓冲流、XX流等等

节点的读取操作:

public class IOTest {
    public static void main(String[] args) throws IOException {
        File file = new File("fileSample.txt");
        FileReader fileReader = new FileReader(file);
        // 返回读入的一个字符,如果读入文件的末尾,返回-1
        //int read = fileReader.read();

        int c = fileReader.read() ;

        while ( c != -1 ){
            System.out.print((char)c + "\t");
            c = fileReader.read();
        }

        // 读取完毕 释放资源    物理链接GC不会对其回收 Scanner JDBC I/O Socket
        fileReader.close();
        
        // 异常处理、为了保证流资源一定可以执行关闭操作、建议使用try-catch-finally
        // FileNotFoundException 系统找不到指定的文件 要保证文件位置正确和存在
    }
}

使用try-catch-finally处理

public class IOTest {
    public static void main(String[] args) {
        File file = new File("fileSample.txt");
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(file);
            int c = fileReader.read() ;

            while ( c != -1 ){
                System.out.print((char)c + "\t");
                c = fileReader.read();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

使用数组作为缓冲容器的问题:

public class IOTest {
    public static void main(String[] args) throws Exception {
        File file = new File("fileSample.txt");

        Reader reader = new FileReader(file);

        // 缓冲容器
        char[] buffer = new char[5];
        int len;
        
        while ( (len = reader.read(buffer)) != -1){
            // 根据索引的个数遍历
            for (int i = 0; i < buffer.length; i++) {
                // Hello World!!!ld!!! 可能会出现这样的情况
                // 问题出现在于如果已经读完了数据,但是多出的之前的已经放进去的数据也被读出来了!!!
                System.out.println(buffer[i]); 
            }
        }
        reader.close();
    }
}

正确的遍历方式

public class IOTest {
    public static void main(String[] args) throws Exception {
        File file = new File("fileSample.txt");

        Reader reader = new FileReader(file);

        // 缓冲容器
        char[] buffer = new char[5];
        int len;

        while ( (len = reader.read(buffer)) != -1){
            // 根据索引的个数遍历
            for (int i = 0; i < len; i++) {
                // Hello World!!!ld!!! 可能会出现这样的情况
                // 问题出现在于如果已经读完了数据,但是多出的之前的已经放进去的数据也被读出来了!!!
                System.out.println(buffer[i]);
            }
        }
        reader.close();
    }
}

用String接收

public class IOTest {
    public static void main(String[] args) throws Exception {
        File file = new File("fileSample.txt");
        Reader reader = new FileReader(file);
        // 缓冲容器
        char[] buffer = new char[5];
        int len;

        while ( (len = reader.read(buffer)) != -1){
            // 用String读取 从0 到末尾的前一位读取
            String str = new String(buffer,0,len);
            System.out.print(str + "\t");
        }
        reader.close();
    }
}

写出数据:

public class IOTest {
    public static void main(String[] args) throws Exception {
        // 输出的文件如果不存在,输出流对象会直接创建文件然后进行输出
        File file = new File("fileSample.txt");

        // 写出文件的位置
        Writer writer = new FileWriter(file,true);
        // new FileWriter(file,true) 在原有文件上追加写入数据
        // new FileWriter(file,false) or new FileWriter(file) 覆写操作

        // 原始数据
        String data = "\n这是我们要写入的数据!!!";

        // 转换成字符数组作为可传输的格式
        char[] chars = data.toCharArray();

        // 读写数据 覆盖写入的操作
        writer.write(chars);
        writer.write("\n直接写入的数据".toCharArray());

        writer.close();
    }
}

读写同时操作,复制文件

public class IOTest {
    public static void main(String[] args) throws Exception {
        // 读取文件
        File src = new File("fileSample.txt");
        // 读写文件
        File dest = new File("destSample.txt");

        // 文件读取流对象
        Reader reader = new FileReader(src);
        // 文件读写对象
        Writer writer = new FileWriter(dest);

        // 缓冲数组
        char[] readerBuffer = new char[3];
        int read; // 每次读取的个数
        while ( (read = reader.read(readerBuffer)) != -1 ){
            System.out.print(read+"..."); // 打印显示
            System.out.print("\n" + new String(readerBuffer) + "\n");
            
            writer.write(readerBuffer,0,read); // 写入字符数组
        }
        reader.close();
        writer.close();
    }
}

  

原文地址:https://www.cnblogs.com/mindzone/p/12748305.html