fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. File newTxt = new File("C:/Users/dslat/Desktop/somefile.txt");
  11.  
  12. if (newTxt.exists())
  13. System.out.println("The file already exists!");
  14. else {
  15. try {
  16. newTxt.createNewFile();
  17. FileWriter fw = new FileWriter(newTxt);
  18.  
  19. bw.write("This is my Prog 2");
  20.  
  21. bw.close();
  22. fw.close();
  23.  
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }
Success #stdin #stdout #stderr 0.04s 4386816KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
java.io.IOException: No such file or directory
	at java.io.UnixFileSystem.createFileExclusively(Native Method)
	at java.io.File.createNewFile(File.java:1012)
	at Main.main(Main.java:16)