-4
public static void main(String[] args) {
    File newTxt = new File("C:/Users/cauan/Desktop/newTxt.txt");

    if(newTxt.exists()) System.out.println("The file already exists!");
    else {
        try{ 
            newTxt.createNewFile();
            FileWriter fw=new FileWriter(newTxt);
            BufferedWriter bw = new BufferedWriter(fw);

            bw.write("This is my Prog");
        }
        catch(Exception e){e.printStackTrace();}
    }        
} 

This is my code.... But i dunno why am i getting an error :/

3
  • 2
    What error are you getting? Commented Apr 2, 2017 at 21:50
  • I don't get an error when I run your program. Please edit your question to include the full text of the error your are getting, including any stack trace being generated. Commented Apr 2, 2017 at 21:56
  • 1
    "I'm trying to create a Java program to create and write a text file, but" But what? The suspense is killing me. Commented Apr 2, 2017 at 21:57

1 Answer 1

0

You need to close the BufferedWriter and FileWriter in order to save the contents of the file.

bw.close();
fw.close();

Complete program link

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.