Java輸入輸出(2)--流

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();

      }

 }

 }

 

}

 

 

 

 

發佈了15 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章