IO流-----(字符流)

1. Reader(字符输出流):所有实现了Reader都是字符输入流

 

范例:

public class Test{

  public static void mian(String[] args){

  int temp=0;

  try(Reader reader =new FileReader("c:/a.txt');){

   //可以读取汉字

    while((temp=reader.read())>0){

        System.out.println((char)temp);

        }

  }catch(Exception e){

  e.printStackTrace();

  }

  }

}

2. Write(字符输出流):所有实现了Write都是字符输出流

范例:

public class Test{

  public static void main(String[] args) {

  try(Writer write = new FileWriter("c:/d.txt")){

  //写到指定文件中

  write.write("实现梦想");

  //追加到指定文件中

  write.append(" 加油");

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

  }

}

猜你喜欢

转载自www.cnblogs.com/heshiping/p/9750672.html