O
O
ojiekcahdp2021-08-28 22:22:43
Java
ojiekcahdp, 2021-08-28 22:22:43

How to get the file correctly?

I do like this:

File cageFrom = new File(File.separator + "home" + File.separator + "SkyWarsUtils", "Cages");


Why is the error popping up
java.io.FileNotFoundException: /home/SkyWarsUtils/Cages (No such file or directory)
?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Makato, 2021-08-28
@Makato

I figured it out myself an hour ago)
If the files are in the root of the project, remove the slash at the beginning.
You can also take files/streams from resources: + As Alexey already said, Path is more convenient. App.class.getClassLoader().getResource("filename")

A
Arty_Fact, 2021-08-29
@Arty_Fact

It is not clear why you are inserting file.separator, you still have an absolute path. You can write at once: In your case you still call StringBuilder implicitly. But you can do even better - use a relative path: Such a file will refer to the Cages file in the project folder.
File file = new File("/home/SkyWarsUtils/Cages");
File file = new File("./Cages");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question