-2

I'm just learning how to use Java, and I have an assignment that asks us to create a file, yet Java is telling me that the file doesn't exist and I don't know why. I'm supposed to make sure my file exists before proceeding with anything else. This is what I have so far:

import java.io.*; 
import java.util.Scanner;

public class Party {
    public static void main(String [] args) throws IOException {
        File file = new File("party.txt");

        if (!file.exists()) {
            System.out.println("The file party.txt doesn't exist.");
        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    Make sure you file is located in the project tree, or just provide an absolute path. – adxl Mar 04 '20 at 22:14
  • 1
    Hint: print the absolute path of the file to see where it is supposed to be `System.out.println("The file doesn't exist: " + file.getAbsolutePath());` - when printing error message, add useful (debug) information to it – user85421 Mar 04 '20 at 22:24

1 Answers1

0

You can provide the complete file path:

File file = new File("C:/users/.../party.txt");

Or you can use relative paths. Let's say you have your Party class inside a sketchbook package. Your relative path would be:

File file = new File("src/sketchbook/party.txt");
Mateus Bandeira
  • 417
  • 2
  • 5