美文网首页
Java IO之节点流(文件流)的使用

Java IO之节点流(文件流)的使用

作者: 程序员汪汪 | 来源:发表于2021-04-02 22:55 被阅读0次

文件字符流FileReader和FileWriter的使用

文件的输入

从文件中读取到内存(程序)中

步骤:

  1. 建立一个流对象,将已存在的一个文件加载进流FileReader fr = new FileReader(new File("Test.txt"));
  2. 创建一个临时存放数据的数组char[] ch = new char[1024];
  3. 调用流对象的读取方法将流中的数据读入到数组中fr.read(ch);
  4. 关闭资源fr.close();

代码示例:

@Test
public void testFileReader1() {
    FileReader fr = null;
    try {
        // 1.File类的实例化,文件不存在就会报FileNotFoundException
        File file = new File("D:\\io\\hello.txt");

        // 2.FileReader流的实例化
        fr = new FileReader(file);

        // 3. 读入的操作
        // read(char[] cbuf):返回每次读入cbuf数据中的字符的个数。如果达到文件末尾,返回-1
        char[] cbuf = new char[1024];
        int len;
        while ((len = fr.read(cbuf)) != -1) {
            String str = new String(cbuf, 0, len);
            System.out.println(str);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 4.关闭资源
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

注意点:

  1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
  2. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
  3. 读入的文件一定要存在,否则就会报FileNotFoundException

文件的输出

从内存(程序)到硬盘文件中

步骤:

  1. 创建流对象,建立数据存放文件File Writer fw = new File Writer(new File("Test.txt"))
  2. 调用流对象的写入方法,将数据写入流fw.write("HelloWord")
  3. 关闭流资源,并将流中的数据清空到文件中fw.close();

代码示例:

@Test
public void testFileWriter() {

    FileWriter fw = null;
    try {
        // 1.提供File类的对象,指明写出到的文件
        File file = new File("D:\\io\\hello.txt");
        // 2.提供FileWriter的对象,用于数据的写出
        // false:表示不在文件末尾追加,而是直接覆盖原有文件内容。
        fw = new FileWriter(file, true);
        // 3.写出的操作
        fw.write("I have a dream!\n");
        fw.write("you need to have a dream!");

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 4.流资源的关闭
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

小练习

实现文本文件的复制操作

/**
 * 实现文本的复制操作
 */
@Test
public void testFileReaderFileWriter() {

    FileWriter fw = null;
    FileReader fr = null;
    try {
        // 1.创建File类的对象,指明读入和写出的文件
        File srcFile = new File("D:\\io\\hello.txt");
        File destFile = new File("D:\\io\\hello2.txt");
        // 2.创建输入流和输出流的对象
        fr = new FileReader(srcFile);
        fw = new FileWriter(destFile);
        // 3.数据的读入和写出操作
        char[] cbuf = new char[5];
        int len;    // 记录每次读入到cbuf数组中的字符的个数
        while ((len = fr.read(cbuf)) != -1) {
            // 每次写出len个字符
            fw.write(cbuf, 0, len);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 4.关闭资源
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

文件字节流FileInputStream和FileOutputStream的使用

文件字节流操作与字符流操作类似,只是实例化对象操作和数据类型不同。

代码示例:

// 使用字节流FileInputStream处理文本文件,可能出现乱码
@Test
public void testFileInputStream() {

    FileInputStream fis = null;
    try {
        //1.指定要读取的文件
        File file = new File("D:\\io\\hello.txt");
        // 2.造流
        fis = new FileInputStream(file);
        // 3.读数据
        byte[] buffer = new byte[5];
        int len; // 记录每次读取的自己的个数
        while ((len = fis.read(buffer)) != -1) {
            String str = new String(buffer, 0, len);
            System.out.println(str);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            // 4.关闭资源
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

小练习

实现图片文件复制操作

@Test
public void testFileInptStreamAndFileOutputStream() {

    FileOutputStream fos = null;
    FileInputStream fis = null;
    try {
        // 1.创建File对象
        File srcFile = new File("D:\\io\\test.jpg");
        File destFile = new File("D:\\io\\test2.jpg");

        // 2.创建流
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);

        // 3.复制图片,想复制快一点,byte数组设置大一点
        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 4.关闭资源
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

注意点

  • 定义路径时,可以用/\\
  • 输出操作,对应的File可以不存在的。并不会报异常。
  • File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。
  • File对应的硬盘中的文件如果存在:
    • 如果流使用的构造器是:FileWriter(file, false)FileWriter(file):对原有文件的覆盖。
    • 如果流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是在原有文件基础上追加内容。
  • 读取文件时,必须保证文件存在,否则会报异常。
  • 对于文本文件(.txt.java.c.cpp),使用字符流处理。
  • 对于非文本文件(.jpg.mp3.mp4.avi.doc.ppt,...),使用字节流处理。

相关文章

  • Java IO之节点流(文件流)的使用

    文件字符流FileReader和FileWriter的使用 文件的输入 从文件中读取到内存(程序)中 步骤: 建立...

  • IO流之节点流(文件流)

    IO流的分类 按操作的数据单位:字节流(8bit),字符流(16bit)---->对于文本文件使用字符流处理,对于...

  • java_io

    java.io.File类的使用IO原理及流的分类  |---文件流   操作对象是文件     FileInp...

  • 【JavaSE(十三)】JavaIO流(中)

    1 IO流 1.1 IO流概述 Java中使用 IO流 来读取和写入,读写设备上的数据、硬盘文件、内存、键盘等等,...

  • Java之IO流详解

    title: Java之IO流详解tags: Java IO流categories: Java IO流 大多数应用...

  • Java IO 节点流和字符流 学习 + 案例分析

    Java IO 节点流和字符流 学习 + 案例分析

  • JAVA API-day07

    A 基本的IO操作 文件流 缓冲流 节点流和处理流 对象流

  • 13. IO流

    一、文件创建文件: 二、IO流分类 三、节点流FileInputStream文件字节输入流: 四、节点流FileO...

  • Java基础之IO流

    ##Java基础之IO流IO流常用几个类的关系如下: 字节流 字节输入流FileInputStream 读取文件用...

  • 5-Java IO-装饰模式

    1.Java IO分类节点流-过滤流输入流-输出流字节流-字符流 2.InputStream,节点输入字节流的祖宗...

网友评论

      本文标题:Java IO之节点流(文件流)的使用

      本文链接:https://www.haomeiwen.com/subject/uzgxkltx.html