2011-03-04 145 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;