本文介绍了用Java写和读文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试从文件中读取,取每一行的长度,将长度写入另一个文件,然后打印第二个文件以查看写入结果,但是当我打开第二个文件时,结果却不然正是我想要的东西.运行此代码后,文件中有很多数字:

I'm trying to read from a file, take the length of the each line, write the length into another file and then print the second file to see the writing result, but when I open the second file the result is not exactly the thing that I want. There are many numbers in the file after running this code:

    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();

请使用此示例帮助我.我关闭了索引,但是仍然有同样的问题.

Please help me using this example. I close the index, but still have the same problem.

注意:不要关注arpa文件,而可以将txt文件图像化.

Notice: Don't pay attention to arpa file you can image a txt file instead.

推荐答案

在打开其他位置之前,应先关闭index.txt.或至少flush

You should close index.txt before opening in a different place; or at least flush it:

...
index.close();

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

String line1=null;
...

这篇关于用Java写和读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:50