Jump to content

Hi.

 

I am currently working on a small program where I need to save some information in .txt files. I have earlier used methods like

        Scanner diskScanner = new Scanner(new File("rawData.txt"));        PrintStream diskWriter = new PrintStream("cookedData.txt");

to read and write to the files rawData.txt and cookedData.txt.

Since there is no path assigned, these are then stored in the root-folder of the program, alongside the bin and src folder.

 

I want to make a folder called data inside the root-folder, so that this program will work on any machine.

I know I can simply specify the folder in which the files are, but then that needs to be changed for every individual computer that launches the program.

A method like this does it easily:

PrintStream diskWriter = new PrintStream ("c:/Users/Public/Documents/cookedData.txt");

 

 

but is there a way to tell it to access the folder data in the root folder, so that it will always find the folder, no matter what machine it is installed on.

 

Thanks.

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
https://linustechtips.com/topic/465235-readwrite-to-files-in-root-folder-with-java/
Share on other sites

Link to post
Share on other sites

./

Should specify the root folder.

Eg.

PrintStream diskWriter = new PrintStream ("./cookedData.txt");

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

./

Should specify the root folder.

Eg.

PrintStream diskWriter = new PrintStream ("./cookedData.txt");

 

Thanks. Just confirmed this, and it works!

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

Thanks. Just confirmed this, and it works!

 

@Nineshadow

Hang on, now I am having troubles again.

 

In the projects folder I have the src folder and the bin folder. I have now added my data-folder, which contains the file rules.txt.

 

I have tried the following to access it, but I get a FileNotFountException:

        File file = new File( "./data/rules.txt");

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

 

@Nineshadow

Hang on, now I am having troubles again.

 

In the projects folder I have the src folder and the bin folder. I have now added my data-folder, which contains the file rules.txt.

 

I have tried the following to access it, but I get a FileNotFountException:

        File file = new File( "./data/rules.txt");

Can you take a screenshot of how your things are laid out?

Or just type the tree structure of your java project's folder.

The "./" specifies the root folder of the class that's being compiled most of the time.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Can you take a screenshot of how your things are laid out?

Or just type the tree structure of your java project's folder.

The "./" specifies the root folder of the class that's being compiled most of the time.

63294.jpg

 

The project is called cah, inside the workspace folder. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

The project is called cah, inside the workspace folder. 

Try

 File file = new File( "../data/rules.txt");

You have two of "." , so you basically look in the root folder(which should be "bin" , or "src") of your root folder.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Try

 File file = new File( "../data/rules.txt");

You have two of "." , so you basically look in the root folder(which should be "bin" , or "src") of your root folder.

 

This still gives me the same error with the directory I posted.

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

This still gives me the same error with the directory I posted.

 

Embarrasing. I have been pasting the data folder into my Eclipse directory, but I am compiling in Netbeans.......... 

I will test with the actual directory now

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

On the same matter:

does anyone know if I can read from a specific line in a .txt file with the Scanner method.

If you create a for loop that counts to the correct line number and write String targetString = Scanner.readLine() every time (or maybe it's readNextLine() idkI haven't used scanners in a while), then when it reaches the end, the value will be the value on that line.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×