zoukankan      html  css  js  c++  java
  • I/O流的文件输入输出四部曲

    第一部曲——多个字节读取到缓存


    步骤1:创建文件输入流和输出流对象

      方式1:先创建要读取文件,把文件的路径和文件名当做参数传递进去。而后再创建文件输入流对象。该对象会抛出文件未找到异常(FileNotFoundException),需要捕获该异常。

    File file = new File("E:/作业/WorkerTest.java");
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

       方式2:直接创建文件输入流对象,其参数为new一个文件对象。

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(new File("E:/作业/WorkerTest.java"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

     步骤2:先创建缓存数组byte,如果没有读取到文件最后(即-1),则输出到控制台上

    byte[] b = new byte[1024];
    int length = fis.read(b);
    while ((length = fis.read(b)) != -1) {
        for (int i = 0; i < length; i++) {
            System.out.print((char)b[i]);
        }
    }

    第二部曲——一个字节一个字节读取并写入文件


    步骤1:创建文件输入流和输出流对象

    File inputFile = new File("E:/Excercise01.java");
    File outputFile = new File("E:/写入文件.txt");

    try {

      fis = new FileInputStream(inputFile); // 创建输入数据流
      fos = new FileOutputStream(outputFile); // 创建输出数据流

    } catch (Exception e) {
      e.printStackTrace();
    }

    步骤2:使用FileInputStream.read()方法进行一个字节一个字节读取

    int read;
    while ((read = fis.read()) != -1) {
        fos.write(read);
    }

     

    第三部曲——多个字节进行缓存,一次写入


     

      一次读取固定长度的字节数缓存到数组(byte),然后用文件输入流读取byte数组中的字节,如果没有读取到文件末尾,则用文件输入流写入文件。

    // 多个字节进行缓存,一次写入
    byte[] b = new byte[1024];
    int length = -1;
    while ((length = fis.read(b)) != -1) {
        for (int i = 0; i < length; i++) {
            fos.write(b[i]);
        }
    }

    第四部曲——一行一行读取


     

      使用BufferedReader读取文件,BufferedWriter写入文件。在catch结尾一定要加上finally,里面要对输入流和输出流进行判断,如果不为空,则关闭输入输出流。否则将会一直占有系统内存。

    File inputFile = new File("E:/Excercise01.java");
    File outputFile = new File("E:/output.txt");
    BufferedWriter bw = null;
    BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(inputFile));
                bw = new BufferedWriter(new FileWriter(outputFile));
                String str = null;while ((str = br.readLine()) != null) {
                    bw.write(str); // 写入一行数据
                    bw.newLine(); // 写完一行之后换行
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
             // 判断文件输入流是否为空
    if (br != null) { br.close(); }
             // 判断文件输出流是否为空
              if (bw != null) { 
                bw.close();
              }
            }
    catch (IOException e) {
               e.printStackTrace();
            }
        }
  • 相关阅读:
    matplotlib数据可视化之柱形图
    xpath排坑记
    Leetcode 100. 相同的树
    Leetcode 173. 二叉搜索树迭代器
    Leetcode 199. 二叉树的右视图
    Leetcode 102. 二叉树的层次遍历
    Leetcode 96. 不同的二叉搜索树
    Leetcode 700. 二叉搜索树中的搜索
    Leetcode 2. Add Two Numbers
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/snow1234/p/7198791.html
Copyright © 2011-2022 走看看