JAva IOアクション_002

6271 ワード

               I/O,              I/O      。

           I/O  ,        ,          I/O  。                ,           ,           ,        。 

Java                 。        ,        。 Java              ,          c         。  ,                。               ,                。


  I/O     :

   
       
       
     
  I/O  

   
     
   
    
       
     
  I/O     

       
            
       
             
                  ,              I/O   。



    1 MB     ,           : 

 FileInputStream read  ,        ,                 6.9 

 BufferedInputStream read    BufferedInputStream              0.9 

 FileInputStream read                                  0.4 

                 17 1   。 



                        ,      。                               。              。                            。  2            "  "   . 

   2   3        ,           ,             。             I/O    ,          (BufferedInputStream     , BufferedReader     )。 

     I/O   ?   Java    1024    2048   ,              I/O     ,  5  10%。 

  1:    

          FileInputStream read  : 

FileInputStream read              ,               

  :    ,      

  :    ,       

import java.io.*;

  public class intro1 {

    public static void main(String args[]) {

      if (args.length != 1) {

        System.err.println("missing filename");

        System.exit(1);

      }

      try {

        FileInputStream fis = new FileInputStream(args[0]); //          

        int cnt = 0;

        int b;

        while ((b = fis.read()) != -1) {  // FileInputStream read            

          if (b == '
') cnt++; } fis.close(); System.out.println(cnt); } catch (IOException e) { System.err.println(e); } } } 2: : BufferedInputStream read , , : , : import java.io.*; public class intro2 { public static void main(String args[]) { if (args.length != 1) { System.err.println("missing filename"); System.exit(1); } try { FileInputStream fis = new FileInputStream(args[0]); BufferedInputStream bis = new BufferedInputStream(fis); // int cnt = 0; int b; while ((b = bis.read()) != -1) { //BufferedInputStream read // BufferedInputStream, if (b == '
') cnt++; } bis.close(); System.out.println(cnt); } catch (IOException e) { System.err.println(e); } } } 3: FileInputStream read buf, 。 : , : , , , import java.io.*; public class intro3 { public static void main(String args[]) { if (args.length != 1) { System.err.println("missing filename"); System.exit(1); } try { FileInputStream fis = new FileInputStream(args[0]); byte buf[] = new byte[2048]; int cnt = 0; int n; while ((n = fis.read(buf)) != -1) { // FileInputStream read // buf, for (int i = 0; i < n; i++) { if (buf[i] == '
') cnt++; } } fis.close(); System.out.println(cnt); } catch (IOException e) { System.err.println(e); } } } 4: , 。 : , , : 。 import java.io.*; public class readfile { public static void main(String args[]) { if (args.length != 1) { System.err.println("missing filename"); System.exit(1); } try { int len = (int)(new File(args[0]).length()); FileInputStream fis = new FileInputStream(args[0]); byte buf[] = new byte[len]; // fis.read(buf); // fis.close(); int cnt = 0; for (int i = 0; i < len; i++) { if (buf[i] == '
') cnt++; } System.out.println(cnt); } catch (IOException e) { System.err.println(e); } } } , 。 。 。 , System.out ( PrintStream) , 。