欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一系列的IO操作

程序员文章站 2022-03-29 08:14:24
...

最近看到IO操作部分,发现这部分比较模糊了,所以又写一个测试程序来帮忙记忆

如下是代码:

package com.demo;


import java.io.*;


/**
* Created with IntelliJ IDEA.
* User: bokee
* Date: 12-10-9
* Time: 下午5:38
* To change this template use File | Settings | File Templates.
*/
public class IOTest {
  public static void main(String[] args)
  {
    IOTest client=new IOTest();
    //client.consoleIO();
   // client.FileIO();
    //client.FileIO2();
    client.stringIO();
    //client.OutTest();
   // client.OutTest2();
    //client.OutTest3();


    client.FileWriterTest();
  }
  /**
  * console控制台输入测试
  */
  public void consoleIO()
  {
    System.out.println("console控制台输入测试 开始!");
    while(true)
    {
      BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)) ;
      try {
        System.out.println(reader.readLine());
      } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
      }
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
      }
    }
    //System.out.println("console控制台输入测试 结束!");
  }


  /**
  * StringReader测试
  */
  public void stringIO()
  {
    String str="stringIO:阿呆 还是第一 阿!\n换行试试" ;
    StringReader stringReader=new StringReader(str); //从字符串读取数据的推荐做法
    BufferedReader bufferedReader=new BufferedReader(stringReader);
    String str2=null;
    try {
      while((str=bufferedReader.readLine())!=null)
         System.out.println(str);
    } catch (IOException e) {
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
  }
  /**
  * 文件读取数据测试 -方式1
  * 直接按字符来读取
  */
  public void FileIO()
  {
    System.out.println("文件读取数据测试-方式1 开始!");
    BufferedReader reader= null;
    try {
      reader = new BufferedReader(new FileReader("data.ini"));
    } catch (FileNotFoundException e) {
      //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
      System.out.println("File 'data.ini' not found");
    }
    String buffer=null;
    try {
      while (null != (buffer = reader.readLine()))
        System.out.println(buffer);
    } catch (IOException e) {
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    try {
      reader.close();
    } catch (IOException e) {
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    System.out.println("文件读取数据测试-方式1 结束!");
  }


  /**
  * 文件读取数据测试-方式2
  * 先按字节读取,然后转换成字符
  */
  public void FileIO2()
  {
    System.out.println("文件读取数据测试-方式2 开始!");
    BufferedInputStream inputStream=null;
    try {
      inputStream=new BufferedInputStream(new FileInputStream("data.ini"));
    } catch (FileNotFoundException e) {
     // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
      System.out.println("File 'data.ini' not found");
    }
    InputStreamReader inputStreamReader=new InputStreamReader(inputStream);  //字节转换成字符
    BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
    String buffer=null;
    try {
      while (null != (buffer = bufferedReader.readLine()))
        System.out.println(buffer);
    } catch (IOException e) {
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    try {
      bufferedReader.close();
    } catch (IOException e) {
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    System.out.println("文件读取数据测试-方式2 结束!");
  }


  /**
  * 文件写的测试
  */
  public void FileWriterTest() {
    BufferedWriter bufferedWriter= null;
    try {
      bufferedWriter = new BufferedWriter(new FileWriter("data2.ini",true));
      bufferedWriter.write("阿呆 还是第一 阿!\n");
      bufferedWriter.close();
    } catch (IOException e) {
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }


  }


  /**
  * 往控制台输出,使用bufferedWriter
  */
  public void OutTest()
  {
    BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(System.out)) ;
    try {
      bufferedWriter.write("OutTest:阿呆 还是第一 阿! \n 还在阿!");
      bufferedWriter.newLine();
      /**
      * 使用BufferedWriter的话得手工flush,因此下面使用PrintWriter试试
      * 然而 在 FileWriterTest 里面可以看到,没有flush也是可以的,纳闷????????????????????????????????
      */
      bufferedWriter.flush();
     //bufferedWriter.close();
    } catch (IOException e) {
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
  }


  /**
  * 往控制台输出,使用PrintStream
  * 发现write方法无效 ,
  */
  public void OutTest2()
  {
    PrintStream printStream=new PrintStream(System.out,true);
    printStream.println("OutTest2:阿呆 还是第一 阿! \n 还在阿!");
   // printStream.close();


  }


  /**
  * 往控制台输出,使用PrintWriter
  * 然而可以看到必须使用flush才行.
  * 还发现一点, OutTest2和OutTest3无法同时使用
  * 经过一段时间的测试发现导致outTest3无法运行的原因是 printStream.close();
  */
  public void OutTest3()
  {
    PrintWriter printWriter=new PrintWriter(System.out);
    printWriter.println("OutTest3:阿呆 还是第一 阿! \n 还在阿!");
    printWriter.flush();
    //printWriter.close();
  }
}

最近看了IO的源码;现在解释下上面的疑问:

System.out的类型是PrintStream;

1,使用printStream发现writer无效:该方法是private的

2.使用printWriter发现必须使用autoflash才行:

PrintWriter printWriter=new PrintWriter(System.out);
这句话里面的autoflush默认是false,所以才会调用println的时候不会自动的flush,除非这样:

PrintWriter printWriter=new PrintWriter(System.out,true);