Search code examples
javalinuxterminalfedorajavac

Best File Location to put a file that I need to read and write to on linux


In my code, I read and write to a file called "Global Bookings ID.txt".

And when I run the code in a Windows environment on Netbeans IDE, it works just fine.

BufferedReader GBIread = new BufferedReader(new FileReader(
                new File("resources\\GlobalBookingID.txt")));

Here "resources" is parallel to the src folder as seen:

screenshot from windows

But keeping the code and file path the same in Linux:

screenshot from linux

The code can not find the file. Below is how I compile and run the code

[cst2550@localhost src] javac cst2550CW/GymServer.java
[cst2550@localhost src] java cst2550CW/GymServer

Also it did occur to me to change the file path from "resources\GlobalBookingID.txt" to "resources/GlobalBookingID.txt" to account for the different way Linux handles files. but it still does not work.

What's going wrong?


Solution

  • The first thing you want to avoid when writing portable software is \\ as a separator in file paths. Use java.io.File.separatorChar instead.