1

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.

1
  • Shouldnt you have closed the files when you're done reading and writing to them? What is 'the exact thing that you want'? You say that you wanted to print the length of each line from the first file to the second file. That's what you got. Commented Mar 4, 2011 at 7:44

2 Answers 2

3

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;
...
Sign up to request clarification or add additional context in comments.

1 Comment

i got my problem,i must use index.newLine(); to go to next line instead of using "\n".
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.