-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 :/

Cauan KMS
  • 33
  • 6
  • 2
    What error are you getting? – prasanth Apr 02 '17 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. – azurefrog Apr 02 '17 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. – Andy Turner Apr 02 '17 at 21:57

1 Answers1

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

Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27