0

This is my current code

    public static void readText() throws FileNotFoundException {
    Scanner scan = new Scanner(new File("D:\\BookList\\src\booklist\\Booklist.txt"));
    while(scan.hasNextLine()){
        String line = scan.nextLine();
        System.out.println(line);
    }
}

it works fine put it specifies the d drive(it is stored on a usb) and no other folders.

Users could save it anywhere, and the filepath would theoretically be different every time, so my question is how do i start from the Booklist project folder that the program exists in, vs the drive of the comptuer, folders leading up to the project folder(if any), and then in the project folder, to find my file for my program to use.

  • What you want is a relative path instead of an absolute path: [How does Java resolve a relative path in new File()?](http://stackoverflow.com/questions/21060992/how-does-java-resolve-a-relative-path-in-new-file). – MikaelF Feb 11 '17 at 02:29

1 Answers1

0

I'm not sure about your case, but I know in windows batch/cmd that if one were to run a file and have it use the current directory as a variable for file paths you type "cd" (without quotations) and that then specifies the current directory. I do remember it being possible to adjust parameters of the "cd" command so that it returns just the drive letter not the whole path.

I'm sure something of that nature is available for your case.

technotron101
  • 29
  • 1
  • 1
  • 7