0

I am in a Linux Envirnomnet. I am also using Netbeans. Here is my code below:

import java.io.*;


public class myFirstJavaProgram {

    public static void main(String[] args) {
      File file = new File("home/gk/Hello1.txt");
      // creates the file
      file.createNewFile();
      // creates a FileWriter Object
    }
}

3 Answers3

2

You forgot a slash before home. It is looking for a folder that most likely does not exist inside the classpath.

EDIT After you pointed out the exception you were receiving I realized that a checked exception is not being handled. You need to catch the possible IOException or include the exception in the method signature.

NickGerleman
  • 106
  • 3
  • What exception are you getting? – NickGerleman Jun 25 '14 at 04:39
  • Description Resource Path Location Type Unhandled exception type IOException myFirstJavaProgram.java /myFirstJavaProgram/src line 22 Java Problem – Gavindra Kalikapersaud Jun 25 '14 at 04:40
  • 1
    Oh whoops, I see. Java has a system called checked Exceptions. You need to say that the method throws the exception or try to catch it. See http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation – NickGerleman Jun 25 '14 at 04:46
1
import java.io.*;

/**
 *
 * @author Ashwin Parmar
 */
public class myFirstJavaProgram  {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            File file = new File("/home/gk/Hello1.txt");
            file.createNewFile();
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }
        // creates a FileWriter Object
    }
}

When dealing with any File IO action in Java, it is always best to use a try/catch loop

Ashwin Parmar
  • 3,025
  • 3
  • 26
  • 42
0

Error in your path. This home/gk/Hello1.txt should be /home/gk/Hello1.txt

Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41