2011-03-04 187 views
1

我試圖從一個文件讀取,取出每一行的長度,將長度寫入另一個文件,然後打印第二個文件以查看寫入結果,但是當我打開第二個文件的結果並不完全是我想要的。運行此代碼後,文件中有許多數字:在java中寫入和讀入文件

String line = null; 

    boolean flag = false; 

    BufferedReader bf = new BufferedReader(new FileReader("c:\\lm_giga_5k_nvp_2gram.arpa")); 

    BufferedWriter index = new BufferedWriter(new FileWriter("c:\\index.txt")); 
    int l; 

    int counter=0; 

    while ((line = bf.readLine()) != null) 

    { 

     l=line.length(); 

     index.write(l + "\n"); 

    } 

    BufferedReader bf1 = new BufferedReader(new FileReader("c:\\index.txt")); 

    String line1=null; 

    while ((line1 = bf1.readLine()) != null) 


    { 
     System.out.println(line1); 


    } 

    bf.close(); 

    bf1.close(); 

請幫我使用此示例。我關閉了索引,但仍然有同樣的問題。

注意:不要注意arpa文件,您可以改爲對txt文件進行圖像處理。

+0

是你的家庭作業? – 2011-03-04 07:40:29

+0

當您完成閱讀和寫作時,您是否應該關閉文件?什麼是'你想要的東西'?你說你想從第一個文件到第二個文件打印每一行的長度。這就是你得到的。 – 2011-03-04 07:44:20

+0

你得到的輸出是什麼? – CloudyMarble 2011-03-04 07:56:53

回答

2

你應該在不同的地方打開之前關閉index.txt;或至少flush它:

... 
index.close(); 

BufferedReader bf1 = new BufferedReader(new FileReader("c:\\index.txt")); 

String line1=null; 
... 
+0

好吧,現在我關閉索引,但仍然有相同的問題 – user642564 2011-03-04 07:57:41

+1

你能告訴我們一些輸出? – trojanfoe 2011-03-04 08:29:09

+0

我得到了我的問題,我必須使用index.newLine();轉到下一行而不是使用「\ n」。 – user642564 2011-03-05 06:14:56

0

這裏是寫和讀方法。您可以自定義它們以適應您的需求。

public boolean writePublic(String strWrittenString, String writeFileName, String writeEncoding, boolean appendString) { 

    try { 
     //System.out.println("Writing to file named " + writeFileName + " ..."); 
     Writer out = new OutputStreamWriter(new FileOutputStream(writeFileName, appendString), writeEncoding); 
     try { 
      out.write(strWrittenString); 

     } finally { 
      out.close(); 
     } 

     //System.out.println("Writing to file named " + writeFileName + "- success."); 
     return true; 
    } catch (IOException ioe) { 
     System.out.println("file named " + writeFileName + "-Failed. cause: " + ioe.getMessage()); 
     return false; 
    } catch (Exception e23) { 
     System.out.println("file named " + writeFileName + "-Failed. cause: " + e23.getMessage()); 
     return false; 
    } 

} 

和閱讀方法:

public static String readWithoutEncoding(String readFileName) { 
    StringBuilder text = new StringBuilder(); 
    try { 
     //System.out.println("Reading from file named " + readFileName + " ..."); 

     String NL = System.getProperty("line.separator"); 
     Scanner scanner = new Scanner(new FileInputStream(readFileName)); 
     try { 
      while (scanner.hasNextLine()) { 
       text.append(scanner.nextLine() + NL); 
      } 
     } finally { 
      scanner.close(); 
     } 
    // System.out.println("Text read in: " + text); 
    //System.out.println("Reading from file named " + readFileName + "- success."); 
    } catch (IOException ioe) { 
     System.out.println("file named " + readFileName + "-Failed. cause: " + ioe.getMessage()); 
    } catch (Exception e23) { 
     System.out.println("file named " + readFileName + "-Failed. cause: " + e23.getMessage()); 


    } finally { 
     return text.toString(); 
    } 

} 

另外不要忘記將所需的import語句都含有上述方法的Java類的開頭:

import java.io.*; 
import java.util.Scanner;