现在的位置: 首页 > 综合 > 正文

Java输入输出(2)–流

2018年04月06日 ⁄ 综合 ⁄ 共 879字 ⁄ 字号 评论关闭

1.流的分类。输入、输出都是从程序运行所在内存的角度来划分的。

2.字节流操作的最小数据单元是8位的字节,

   字符流操作的最小数据单元是16位的字符。

3.使用FileInputStream读取自身

  public class FileInputStreamTest

{

  public static void main(String[] args) throws IOException

  {

   //创建字节输入流

    FileInoutStream fis = new FileInputStream("FileInputSteaTest.java");

    byte[] bbuf = new byte[1024];

    int hasRead = 0;

    while((hasRead = fis.read(bbuf)) > 0)

     {

          System.out.println(new String(bbuf,0,hasRead));
     }

  //关闭文件输入流,放在finally块里更安全

    fis.close();

  }

}

4.使用FileReader读取自身

  public class FileReaderTest

{

  public static void main(String[] args) throws IOException

{

   FileReader fr = null;

   try{

      fr = new FileReader("FileReaderTest.java");

      char[]  cbuf = new char[32];

      int  hasRead = 0;  

     while((hasRead =  fr.read(cbuf))>0)

   {

      System.out.println(new String(cbuf,0,hasRead));

   }

  }

catch(IOException ioe)

 {

   ioe.printStackTrace();

 }

finally

{

       if(fr != null)

       {

       fr.close();

      }

 }

 }

 

}

 

 

 

 

抱歉!评论已关闭.