IO

  1. 使用字節流 
  2.     讀取控制檯輸入 
  3.         因爲System.in是InputStream的一個實例,然而InputStream只定義了一個讀取字節的輸入方法read() 
  4.         int read() throws IOException  int read(byte data[]) throws IOException  int read(byte data[],int start,int max) throws IOException 
  5.         import java.io.*; 
  6.         class javadir{ 
  7.             public static void main(String args[]) throws IOException { 
  8.                 byte data[]=new byte[111]; 
  9.                 System.out.println("Enter some characters:"); 
  10.                 System.in.read(data); 
  11.                 System.out.print("you entered:"); 
  12.                 for(int i=0;i<10;i++) System.out.print((char)data[i]); 
  13.             } 
  14.         } 
  15.     寫入控制檯輸出 
  16.         print,println是由類PrintStream定義的,PrintStream是一個從OutputStream派生出來的輸出流,所以它還執行低級別的方法write() 
  17.         因此使用write()向控制檯寫實完全可行的 
  18.         import java.io.*; 
  19.         class javadir{ 
  20.             public static void main(String args[]) { 
  21.                 int b; 
  22.                 b='D'
  23.                 System.out.write(b); 
  24.                 System.out.write('\n'); 
  25.             } 
  26.         } 
  27.          
  28.          
  29.          
  30.     使用字節流讀取文件 
  31.         Java可以讓你將一個字節組成的文件包含在一個基於字符的對象中,這在後面介紹 
  32.         爲創建一個與文件相連接的字節流,需要使用FileInputStream,FileOutputStream,要打開文件,需要創建對象 
  33.          
  34.          
  35.         從文件輸入 
  36.             通過創建一個FileInputStream對象可以打開一個用於輸入的文件,下面是它最常用的構造函數 
  37.             FileInputStream(String filename) throws FileNotFoundException 
  38.             需要使用read()方法從文件中讀取,  當處理完後應該用close()方法關閉它 
  39.             import java.io.*; 
  40.             class javadir{ 
  41.                 public static void main(String args[]) { 
  42.                     int i; 
  43.                     FileInputStream fin; 
  44.                     if(args.length!=1){ 
  45.                         System.out.println("show file:"); 
  46.                         return
  47.                     } 
  48.                     try
  49.                         fin=new FileInputStream(args[0]); 
  50.                     }catch(FileNotFoundException exc){ 
  51.                         System.out.println("file not found"); 
  52.                         return;  //需要加上 
  53.                     } 
  54.                     try
  55.                         do
  56.                             i=fin.read(); 
  57.                             if(i!=-1) System.out.print((char)i); 
  58.                         }while(i!=-1); 
  59.                     }catch(IOException exc){ 
  60.                         System.out.println("Error reading file"); 
  61.                     } 
  62.                     try
  63.                         fin.close(); 
  64.                     }catch(IOException exc){ 
  65.                         System.out.println("Error closing file"); 
  66.                     } 
  67.                 } 
  68.             } 
  69.          
  70.     使用字節流寫入文件 
  71.         爲打開一個文件用於輸出,需要創建一個FileOutputStream對象,下面是它的常用構造函數 
  72.         FileOutputStream(String filename) throws FileNotFoundException 
  73.         FileOutputStream(String filename,boolean append) throws FileNotFoundException 
  74.         需要使用 void write(int byteval) throws IOException 來寫入文件 
  75.         該方法向文件寫入byteval指定的字節,儘管byteval被聲明一個整數,但它只有低八位可以寫入文件 
  76.         輸出的內容不會馬上寫到物理設備而會緩存 
  77.         可以使用 void flush() throws IOException 不讓它緩存 
  78.         輸出文件結束後,就必須使用close()關閉它 
  79.         例子如下: 
  80.             import java.io.*; 
  81.             class javadir{ 
  82.                 public static void main(String args[]){ 
  83.                     FileInputStream fin; 
  84.                     FileOutputStream finn; 
  85.                     int i; 
  86.                     if(args.length!=2){ 
  87.                         System.out.println("輸入的文件名不正確!"); 
  88.                         return
  89.                     } 
  90.                     try
  91.                         fin=new FileInputStream(args[0]); 
  92.                         finn=new FileOutputStream(args[1]); 
  93.                     }catch(FileNotFoundException exc){ 
  94.                         System.out.println("文件不存在"); 
  95.                         return
  96.                     } 
  97.                     try
  98.                         do
  99.                             i=fin.read(); 
  100.                             if(i!=-1) finn.write(i); 
  101.                         }while(i!=-1); 
  102.                     }catch(IOException exc){ 
  103.                         System.out.println("讀取文件錯誤"); 
  104.                     } 
  105.                     try
  106.                         fin.close(); 
  107.                         finn.close(); 
  108.                     }catch(IOException exc){ 
  109.                         System.out.println("Error closing file"); 
  110.                     } 
  111.                 } 
  112.             } 
  113.          
  114.          
  115.     讀寫二進制數據 
  116.         目前我們已經讀寫的字節包括ASCII字符,但是也可以讀取和寫入其他類型的數據,例如可以創建一個包含int double 的文件 
  117.         要讀取和寫入Java的簡單類型的二進制值,需要使用DataInputStream和DataOutputStream 
  118.         DataOutputStream實現DataOutput接口,該接口定義了所有Java簡單類型向文件寫入的方法,有一點很重要的是: 
  119.         數據寫入使用的是內部二進制數據而不是人們可讀的文本形式 
  120.         以下是構造方法 
  121.         DataOutputStream(OutputStream os) 這裏的os是寫入數據的流,要向文件輸出,可以使用FileOutputStream爲該參數創建的對象 
  122.         void writeBoolean(boolean val) 
  123.         void writeByte(int val) 
  124.         void writeChar(int val) 
  125.         void writeDouble(double val) 
  126.         void writeInt(int val) 
  127.         void writeLong(long val) 
  128.         void writeShort(short val) 
  129.         void writeFloat(float val) 
  130.          
  131.         DataInputStream實現DataInput接口,該接口定義了所有Java簡單類型讀取文件的方法 
  132.         DataInputStream是以二進制讀取數據的,而不是以可讀文本格式讀取數據 
  133.         它的構造函數如下: 
  134.         DataInputStream(InputStream is) 這裏is是與創建DataInputStream的實例相關聯的流,要從文件讀取數據 
  135.         可以使用FileInputStream爲該參數創建的對象 
  136.         boolean readBoolean()  ......與上面相對應 
  137.         例子如下: 
  138.         import java.io.*; 
  139.         class javadir{ 
  140.             public static void main(String args[]){ 
  141.                 DataOutputStream dataout; 
  142.                 DataInputStream datain; 
  143.                 int i=10
  144.                 double d=1209.33
  145.                 boolean b=true
  146.                 try
  147.                     dataout=new DataOutputStream(new FileOutputStream("t.txt")); 
  148.                 }catch(IOException exc){ 
  149.                     System.out.println("can not open the file"); 
  150.                     return
  151.                 } 
  152.                 try
  153.                     System.out.println("Writing"+i); 
  154.                     dataout.writeInt(i); 
  155.                     System.out.println("Writing"+d); 
  156.                     dataout.writeDouble(d); 
  157.                     System.out.println("Writing"+b); 
  158.                     dataout.writeBoolean(b); 
  159.                 }catch(IOException exc){ 
  160.                     System.out.println("Write error"); 
  161.                 } 
  162.                 try
  163.                     dataout.close(); 
  164.                 }catch(IOException exc){ 
  165.                     System.out.println(); 
  166.                     return
  167.                 } 
  168.                 System.out.println(); 
  169.                 try
  170.                     datain=new DataInputStream(new FileInputStream("t.txt")); 
  171.                 }catch(IOException exc){ 
  172.                     System.out.println("can not open the file"); 
  173.                     return
  174.                 } 
  175.                 try
  176.                     i=datain.readInt(); 
  177.                     System.out.println("Reading"+i); 
  178.                     d=datain.readDouble(); 
  179.                     System.out.println("Reading"+d); 
  180.                     b=datain.readBoolean(); 
  181.                     System.out.println("Reading"+b); 
  182.                 }catch(IOException exc){ 
  183.                     System.out.println("can not read"); 
  184.                 } 
  185.                 try
  186.                     datain.close(); 
  187.                 }catch(IOException exc){ 
  188.                     System.out.println("Error closing file"); 
  189.                 } 
  190.             } 
  191.         } 
  192.  
  193.     隨即訪問文件 
  194.     RandomAccessFile類不是從InputStream 或 OutputStream 派生而來的,相反它實現定義I/O基本方法的接口DataInput 和DataOutput 
  195.     構造函數如下: 
  196.     RandomAccessFile(String filename,String access) throws FileNotFoundException   
  197.     access確定了允許的文件訪問類型,如果是"r" 文件只能讀不能寫,如果是"rw"文件既可以讀也可以寫 
  198.     這裏的 void seek(long newPos) throws IOException  ,這裏的newPos 制定了新的文件位置發生位置 
  199.     RandomAccessFile實現了read()和write()方法,也可以使用如readInt(),writeDouble()...方法 
  200.         import java.io.*; 
  201.         class javadir{ 
  202.             public static void  main(String args[]){ 
  203.                 double data[]={12.3,34.4,26.7,26.7,98.4}; 
  204.                 double d; 
  205.                 RandomAccessFile raf; 
  206.                 try
  207.                     raf=new RandomAccessFile("t.txt","rw"); 
  208.                 }catch(FileNotFoundException exc){ 
  209.                     System.out.println("File not Found"); 
  210.                     return
  211.                 } 
  212.                 try
  213.                     for(int i=0;i<data.length;i++){ 
  214.                         raf.writeDouble(data[i]); 
  215.                     } 
  216.                     raf.seek(0); 
  217.                     d=raf.readDouble(); 
  218.                     System.out.println("Value is"+d); 
  219.                     raf.seek(2*8); 
  220.                     d=raf.readDouble(); 
  221.                     System.out.println("Value is"+d); 
  222.                      
  223.                 }catch(IOException exc){ 
  224.                     System.out.println("Seeking is Error"); 
  225.                 } 
  226.                 try
  227.                     raf.close(); 
  228.                 }catch(IOException exc){ 
  229.                     System.out.println("Error closing file"); 
  230.                 } 
  231.             } 
  232.         } 
  233.  
  234.     使用java字符流 
  235.     使用字符流的控制檯輸入 
  236.     因爲是一個System.in字節流,所以需要將System.in包含在某一類型的Reader中。讀取控制檯輸入最好的類是BufferedReader, 
  237.     它支持緩衝輸入流。然而不能直接從System.in構造BufferedReader,必須首先將它轉換爲一個字符流,爲此,需要使用InputStreamReader把字節轉換爲字符 
  238.     爲了獲得與System.in鏈接的InputStreamReader對象,需要使用下面的構造函數 
  239.     InputStreamReader(InputStream is) 因爲System.in引用一個InputStream對象,所以它用於InputStream對象 
  240.     接下來使用一個BufferedStream 構造函數,如下:BufferedStream(Reader ir) 這裏ir是與創建的BufferedStream實例鏈接的流 
  241.     下面的代碼創建了與鍵盤連接的流:BufferedStream br=new BufferedStream(new InputStreamReader(System.in)) br將成爲一個通過System.in與控制檯鏈接的字符流 
  242.     使用read()方法讀取字符 
  243.     import java.io.*; 
  244.     class javadir{ 
  245.         public static void main(String args[]) throws IOException { 
  246.             BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
  247.             char c; 
  248.             System.out.println("Enter some words"); 
  249.             do
  250.                 c=(char)br.read(); 
  251.                 System.out.println(c); 
  252.             }while(c!='.'); 
  253.         } 
  254.     } 
  255.  
  256.  
  257.     讀取字符串 
  258.     爲從鍵盤讀取字符串,需要使用BufferedReader類的成員readLine();其基本形式如下: 
  259.     String readLine() throws IOException  
  260.     import java.io.*; 
  261.     class javadir{ 
  262.         public static void main(String args[]) throws IOException { 
  263.             BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
  264.             String s; 
  265.             System.out.println("Enter Strings"); 
  266.             do
  267.                 s=br.readLine(); 
  268.                 System.out.println(s); 
  269.             }while(!s.equals("stop")); 
  270.         } 
  271.     } 
  272.  
  273.      
  274.     使用字符流控制檯輸出 
  275.     在java中向控制檯寫入的更好的方法是使用PrintStream流,它是一個字符流   其常用的一個構造函數如下: 
  276.     PrintStream(OutputStream os,boolean flushonNewline)   
  277.     PrintStream 支持包括Object在內的所有類型的print,println方法,如果變元不是簡單類型,PrintWrite將調用 toString() 方法 
  278.     爲了使用PrintStream向控制檯寫入,需要爲輸出流指定System.out,在每一個println()調用之後刷新流, PrintStream pw=new PrintStream(System.out,true
  279.     import java.io.*; 
  280.     class javadir{ 
  281.         public static void main(String args[]) throws IOException { 
  282.            PrintStream pw=new PrintStream(System.out,true); 
  283.            int i=10
  284.            double d=33.4
  285.            pw.println(i); 
  286.            pw.println(d); 
  287.         } 
  288.     } 
  289.  
  290.  
  291.     使用字符流的文件IO 
  292.      
  293.     使用FileWriter 
  294.     FileWriter創建了一個可以用於寫入文件的writer,它的常用構造函數是: 
  295.     FileWriter(String filename) throws IOException  
  296.     FileWriter(String filename,boolean append) throws IOException  
  297.     import java.io.*; 
  298.     class javadir{ 
  299.         public static void main(String args[]){ 
  300.             String s; 
  301.             FileWriter fw; 
  302.             BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
  303.             try
  304.                 fw=new FileWriter("t.txt"); 
  305.             }catch(IOException exc){ 
  306.                 System.out.println("Can not Open"); 
  307.                 return
  308.             } 
  309.             System.out.println("Enter text('stop' to quit)"); 
  310.             try
  311.                 do
  312.                     s=br.readLine(); 
  313.                     if(s.compareTo("stop")==0break
  314.                     s=s+"\r\n"
  315.                     fw.write(s); 
  316.                 }while(s.compareTo("stop")!=0); 
  317.             }catch(IOException exc){ 
  318.                 System.out.println("Error writing file"); 
  319.             } 
  320.             try
  321.                 fw.close(); 
  322.             }catch(IOException exc){ 
  323.                 System.out.println("Error closing file"); 
  324.             } 
  325.         } 
  326.     } 
  327.  
  328.     使用FileReader 
  329.     FileReader創建了一個用於讀取文件內容的Reader ,它最常用的構造函數是: 
  330.     FileReader(String filename) throws FileNotFoundException  
  331.     import java.io.*; 
  332. class javadir{ 
  333.     public static void main(String args[]) throws Exception { 
  334.         String s; 
  335.         FileReader fr; 
  336.         BufferedReader br; 
  337.         PrintWriter pw=new PrintWriter(System.out,true); 
  338.         try
  339.             fr=new FileReader("t.txt"); 
  340.             br=new BufferedReader(fr); 
  341.         }catch(FileNotFoundException exc){ 
  342.             pw.println("Can not Open"); 
  343.             return
  344.         } 
  345.         try
  346.             while((s=br.readLine())!=null){ 
  347.                 pw.println(s); 
  348.             } 
  349.         }catch(IOException exc){ 
  350.             pw.println("Error reading file"); 
  351.         } 
  352.         try
  353.             fr.close(); 
  354.         }catch(IOException exc){ 
  355.             pw.println("Error "); 
  356.         } 
  357.     } 
  358.  
  359.  
  360.  
  361.     如果是關於字符流讀取的話,用BufferedReader實現 
  362.      
  363.    
  364.          
  365.          
  366.          
  367.          
  368.          
  369.          
  370.          
  371.          
  372.          
  373.          
  374.          
  375.          
  376.          
  377.          
  378.          
  379.          

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章