user2587623
user2587623

Reputation: 43

Writing a file to another file

When I tried to run OUT.TXT it was alwasy empty. Can you please assist me in finding out why? Also SPY.LOG lines are not ordinary, can you assit with a way to fix those lines also?

package burak;
import java.io.*;

public class Yucal {
    public static void main(String [] args) {

        String fileName = "spy.log";
        String line;
        try {

            FileReader fileReader =  new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            while((line = bufferedReader.readLine()) != null)
            {
            try{
                  FileWriter fstream = new FileWriter("out.txt");
                  BufferedWriter out = new BufferedWriter(fstream);
                  out.write(line);
                  out.close();
                  }
            catch (Exception e){
                  System.err.println("Error: " + e.getMessage());
                  }
            System.out.printf("%65s\n", line);
            }
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println("Error reading file '" + fileName + "'");           }
    }

Upvotes: 0

Views: 113

Answers (3)

bsd
bsd

Reputation: 2727

        FileReader fileReader =  new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        while((line = bufferedReader.readLine()) != null)
        {
        try{
              >FileWriter fstream = new FileWriter("out.txt");**
              >BufferedWriter out = new BufferedWriter(fstream);**

        <snip>

Everytime you open your file and write one line. Then close it. Next time you open it, you overwrite the previous contents of the file. You should probably move the lines marked with > outside the while loop.

The last line of your file spy.log might be empty.

Upvotes: 1

Sachin Thapa
Sachin Thapa

Reputation: 3719

Additionally move all close statements to finally block.

You might also need to handler some IO exceptions when you close these streams.

Hope this helps.

Upvotes: 0

vels4j
vels4j

Reputation: 11298

Few changes

FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
while ((line = bufferedReader.readLine()) != null) {
    try {
        out.write(line);
        out.write("\n");
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
    System.out.printf("%65s\n", line);
}
out.close();
bufferedReader.close();

The mistake was you've opened FileWriter fstream = new FileWriter("out.txt"); within while loop. It must be outside.

Upvotes: 2

Related Questions