简体   繁体   English

在java中写入和读取文件

[英]write and read in file in 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.我关闭了索引,但仍然有同样的问题。

Notice: Don't pay attention to arpa file you can image a txt file instead.注意:不要关注 arpa 文件,您可以将其镜像为 txt 文件。

You should close index.txt before opening in a different place;在其他地方打开之前,您应该关闭 index.txt; or at least flush it:或至少flush它:

... 
index.close();

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

String line1=null;
...

Here is Write and Read methods.这里是 Write 和 Read 方法。 You can customize them to fit your needs.您可以自定义它们以满足您的需求。

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;
    }

}

And Read method:和读取方法:

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

}

Also don't forget to place needed Import statements at the beginning of Java class containing above methods:另外不要忘记在包含上述方法的 Java 类的开头放置所需的 Import 语句:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM