1. 程式人生 > >read輸入流與writer輸出流的對比

read輸入流與writer輸出流的對比

new throw port 單行 length txt cep not 讀取

package io.day4;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;

public class Test {

  public static void main(String[] args) throws Exception {
  read();//輸入流
  write();//輸出流

  }

   //輸出流

  private static void write() throws Exception {
  Writer wt = new FileWriter("Test/xy.txt");
  char[] c = {‘誰‘,‘是‘,‘大‘,‘傻‘,‘逼‘};
  wt.write(c);
  wt.close();
  }

  //輸入流

  private static void read() throws Exception {
  Reader rd= new FileReader("Test/xy.txt");
  int b=rd.read();                //讀取單行字符
  System.out.println((char)b);
  char[] chars = new char[10];          //讀取10個字符
  int length = rd.read(chars);
  System.out.println(Arrays.toString(chars));
  rd.close();
   }
}

read輸入流與writer輸出流的對比