简体   繁体   中英

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.

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

Here is Write and Read methods. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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