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.

Well, I don't think you are writing to cr. You create it, and then close it, but nothing is written to it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.