隨筆-26  評(píng)論-13  文章-46  trackbacks-0
          Java 中對(duì)文件的讀寫操作之比較
          一.在 JDK 1.0 中,通常是用 InputStream & OutputStream 這兩個(gè)基類來(lái)進(jìn)行讀寫操作的。
          InputStream 中的 FileInputStream 類似一個(gè)文件句柄,通過(guò)它來(lái)對(duì)文件進(jìn)行操作,類似的,在
          OutputStream 中我們有 FileOutputStream 這個(gè)對(duì)象。

          用FileInputStream 來(lái)讀取數(shù)據(jù)的常用方法是:
          FileInputStream fstream = new FileInputStream(args[0]);
          DataInputStream in = new DataInputStream(fstream);
          用 in.readLine() 來(lái)得到數(shù)據(jù),然后用 in.close() 關(guān)閉輸入流。
          完整代碼見(jiàn) Example 1。

          用FileOutputStream 來(lái)寫入數(shù)據(jù)的常用方法是:
          FileOutputStream out out = new FileOutputStream("myfile.txt");    
          PrintStream p = new PrintStream( out );
          用 p.println() 來(lái)寫入數(shù)據(jù),然后用 p.close() 關(guān)閉輸入。
          完整代碼見(jiàn) Example 2。


          二.在 JDK 1.1中,支持兩個(gè)新的對(duì)象 Reader & Writer, 它們只能用來(lái)對(duì)文本文件進(jìn)行操作,而
          JDK1.1中的 InputStream & OutputStream 可以對(duì)文本文件或二進(jìn)制文件進(jìn)行操作。

          用FileReader 來(lái)讀取文件的常用方法是:
          FileReader fr = new FileReader("mydata.txt");
          BufferedReader br = new BufferedReader(fr);
          用 br.readLing() 來(lái)讀出數(shù)據(jù),然后用br.close() 關(guān)閉緩存,用fr.close() 關(guān)閉文件。
          完整代碼見(jiàn) Example 3。

          用 FileWriter 來(lái)寫入文件的常用方法是:
          FileWriter fw = new FileWriter("mydata.txt");
          PrintWriter out = new PrintWriter(fw);  
          在用out.print 或 out.println 來(lái)往文件中寫入數(shù)據(jù),out.print 和 out.println的唯一區(qū)別是后者寫
          入數(shù)據(jù)或會(huì)自動(dòng)開(kāi)一新行。寫完后要記得 用out.close() 關(guān)閉輸出,用fw.close() 關(guān)閉文件。   
          完整代碼見(jiàn) Example 4。

          -------------------------------------------------------------- following is the source code of examples------------------------------------------------------

          Example 1:
          // FileInputDemo
          // Demonstrates FileInputStream and DataInputStream
          import java.io.*;

          class FileInputDemo {
            
          public static void main(String args[]) {
              
          // args.length is equivalent to argc in C
              if (args.length == 1{
                
          try {
                  
          // Open the file that is the first command line parameter
                  FileInputStream fstream = new FileInputStream(args[0]);
                  
          // Convert our input stream to a DataInputStream
                  DataInputStream in = new DataInputStream(fstream);
                  
          // Continue to read lines while there are still some left to read
                  while (in.available() !=0{
                    
          // Print file line to screen
                    System.out.println (in.readLine());
                  }

                  
          in.close();
                }
           catch (Exception e) {
                  System.err.println(
          "File input error");
                }

              }

              
          else
                System.
          out.println("Invalid parameters");
            }

          }


          Example 2:
          // FileOutputDemo
          // Demonstration of FileOutputStream and PrintStream classes
          import java.io.*;

          class FileOutputDemo 
          {    
            
          public static void main(String args[])  {              
            FileOutputStream 
          out// declare a file output object
              PrintStream p; // declare a print stream object

          try {
            
          // connected to "myfile.txt"
                out = new FileOutputStream("myfile.txt");
                
          // Connect print stream to the output stream
                p = new PrintStream( out );
                p.println (
          "This is written to a file");
                p.close();
              }
           catch (Exception e) {
                System.err.println (
          "Error writing to file");
              }

            }

          }


          Example 3:
          // FileReadTest.java
          // User FileReader in JDK1.1 to read a file 
          import java.io.*;

          class FileReadTest {      
            
          public static void main (String[] args) {
              FileReadTest t 
          = new FileReadTest();
              t.readMyFile();
          }
           
              
            
          void readMyFile() 
              String record 
          = null;
              
          int recCount = 0
              
          try 
          FileReader fr 
          = new FileReader("mydata.txt");
                 BufferedReader br 
          = new BufferedReader(fr);
                 record 
          = new String();
                 
          while ((record = br.readLine()) != null{
                   recCount
          ++;
                   System.
          out.println(recCount + "" + record); 
          }

          br.close();
          fr.close(); 
               }
           catch (IOException e) 
                   System.
          out.println("Uh oh, got an IOException error!");
                   e.printStackTrace();
               }

          }
           
            
          }
              

          Example 4:
          // FileWriteTest.java
          // User FileWriter in JDK1.1 to writer a file 
          import java.io.*;

          class FileWriteTest {      
            
          public static void main (String[] args) {
              FileWriteTest t 
          = new FileWriteTest();
              t.WriteMyFile();
          }
           
              
            
          void WriteMyFile() 
              
          try 
          FileWriter fw 
          = new FileWriter("mydata.txt");
          PrintWriter 
          out = new PrintWriter(fw);    
          out.print(“hi,this will be wirte into the file!”);   
          out.close();
          fw.close();
               }
           catch (IOException e) 
                   System.
          out.println("Uh oh, got an IOException error!");
                   e.printStackTrace();
               }

          }
           
            
          }
              
          posted on 2005-06-23 17:46 似水流年 閱讀(907) 評(píng)論(2)  編輯  收藏 所屬分類: Java

          評(píng)論:
          # re: Java 中對(duì)文件的讀寫操作之比較 2007-09-19 17:08 | f
          # re: Java 中對(duì)文件的讀寫操作之比較 2007-09-19 17:08 | f

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 繁峙县| 双柏县| 巴马| 金堂县| 日喀则市| 新丰县| 乐业县| 密云县| 贵州省| 太谷县| 盐城市| 德庆县| 江达县| 运城市| 大兴区| 台山市| 汪清县| 黄冈市| 贵港市| 阜宁县| 泸水县| 桐城市| 北辰区| 岳阳县| 凤阳县| 荔浦县| 浦江县| 阆中市| 二手房| 郓城县| 秦安县| 抚远县| 保靖县| 盐亭县| 白银市| 方正县| 新密市| 浙江省| 徐汇区| 客服| 长顺县|