0

I am trying to experiment with the whole idea of creating two text files then write to one of them, rename the second file with the first file and then deleting the first file. I have a school project where i need to apply this concept. So, before i actually applied the concept in my project, I tried to experiment with a rough code. Now, everything works fine except that the second file doesn't contain the data from the first file. How do i fix this problem?

This is my class which is called potpie:-

package project4;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class potpie {

    PrintWriter out,cr;

    File file1 = new File("trial.txt");
    File file2 = new File("item.txt");

    public void createfile() throws IOException
    {
        out = new PrintWriter(new FileWriter(file1,true));

        out.println("User1" + "639755");

        cr = new PrintWriter(new FileWriter(file2,true));
        cr.close();
        out.close();
        file1.delete();
        file2.renameTo(file1);

}

}

I am getting blank output in my trial.txt file and i don't know where i made the mistake. Please help me out.

2
Contributors
1
Reply
8
Views
2 Years
Discussion Span
Last Post by rubberman
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.