天天看點

IO流

問題1:IO流一定要關閉麼?

問題2:字元流和位元組流使用上有什麼差別?

問題3:IO流分哪幾種?

一、思維導圖

    前言:IO流都為對檔案操作,是以IO流都需要使用File類

String filePath="/Users/text.txt";
File file = new File(filePath);
file.isDirectory();//判斷是否為檔案夾
File files[] = file.listFiles();//擷取路徑下的所有檔案&檔案夾      
IO流

 一、緩沖流--最常用

1、字元緩沖流(BufferedReader&BufferedWriter)

String filePath = "/Users/haoc/course/code/cakes-course/0.notes/day01.md";
    File file = new File(filePath);
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(file));//不一樣的地方

      String res;
      StringBuilder sb = new StringBuilder();
      while ((res = reader.readLine()) != null) {
        sb.append(res).append("\n");
      }

      String val = sb.toString();

      System.out.println("val = " + val);
    } catch (FileNotFoundException e) {
      System.out.println("這個檔案不存在呀," + e.getMessage());
      throw new RuntimeException(e);
    } catch (IOException e) {
      System.out.println("檔案讀取失敗了," + e.getMessage());
      throw new RuntimeException(e);
    } finally {
      if (null != reader) {
        try {
          reader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
      

  

2、位元組緩沖流(BufferedInputStream&BufferedOutputStream)

二、字元流(FileReader&FileWriter)

public static void normalReader1() {
    String filePath = "/Users/haoc/course/code/cakes-course/0.notes/day01.md";
    File file = new File(filePath);
    Reader reader = null;
    try {
      reader = new FileReader(file);

      int len;
      char[] buf = new char[256];

      StringBuilder sb = new StringBuilder();
      while ((len = reader.read(buf)) != -1) {
        String str = new String(buf, 0, len);
        sb.append(str);
      }

      String val = sb.toString();

      System.out.println("val = " + val);
    } catch (FileNotFoundException e) {
      System.out.println("這個檔案不存在呀," + e.getMessage());
      throw new RuntimeException(e);
    } catch (IOException e) {
      System.out.println("檔案讀取失敗了," + e.getMessage());
      throw new RuntimeException(e);
    } finally {
      if (null != reader) {
        try {
          reader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
      

 

//FileWrite
    public void FileWrite(){
        File file =new File("");
        FileWriter fileWriter=null;
        byte[] result =new byte[256];
        try {
            fileWriter=new FileWriter(file);
            fileWriter.write(result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileWriter !=null){

            }
        }

    }
      

三、位元組流(InputSream&OutputStream)

public static void normalRead1() {
    String filePath = "/Users/haoc/course/code/cakes-course/0.notes/day01.md";

    InputStream ins = null;
    try {
      File file = new File(filePath);
      ins = new FileInputStream(file);

      int len;
      byte[] buf = new byte[256];

      StringBuilder sb = new StringBuilder();
      while ((len = ins.read(buf)) != -1) {
        String str = new String(buf, 0, len);
        sb.append(str);
      }
    } catch (FileNotFoundException e) { // if e == FileNotFoundException
      System.out.println("這個檔案不存在呀," + e.getMessage());
      throw new RuntimeException(e);
    } catch (IOException e) {
      System.out.println("檔案讀取失敗了," + e.getMessage());
      throw new RuntimeException(e);
    } finally {
      if (null != ins) {
        try {
          ins.close();
        } catch (IOException e) {
          System.out.println("流關閉失敗了," + e.getMessage());
        }
      }
    }      
public static void testWriteFile1() throws Exception {
    String filePath = "/Users/haoc/course/temp/xxxx.md";
    File file = new File(filePath);
    OutputStream ous = new FileOutputStream(file);

    for (int i = 0; i < 1000; i++) {
      String str = "hello java io" + i + "\n";
      ous.write(str.getBytes());
    }

    ous.flush();
  }