Obtain project directory from any PC

1

I want to open a txt file contained in my project from any PC.

I have a problem when opening a txt file that is in the resources folder of my project. The point is that I can open the file if I specify the path of it from my hard drive, but it will not work if the project is executed on another computer.

This is how I open it:

String directorio =
"D:\EscritorioWindows\Proyecto\src\recursos\records.txt";
records = new File(directorio);
  

So, I would like to know how to open a resource from my project   from any PC.

Any suggestions are grateful. Greetings!

    
asked by GonzaloM 03.11.2018 в 23:52
source

2 answers

1

Try changing the path to something like this: "\ Project \ src \ resources \ records.txt"

    
answered by 04.11.2018 в 00:23
1

To obtain the directory path of your project in any pc you can do it using any of these options.

1) Using System.getProperty("user.dir") + "\"

If the route you want to obtain is

D:\EscritorioWindows\Proyecto\src\recursos\records.txt

would be obtained in this way:

String directorio = System.getProperty("user.dir") + "\src\recursos\records.txt"

2) Using new File (".").getAbsolutePath()

If the route you want to obtain is

D:\EscritorioWindows\Proyecto\src\recursos\records.txt

It would be obtained in this way:

String directorio = new File (".").getAbsolutePath() + "\src\recursos\records.txt"

3) or using a variant of the previous one, using new File ("<Ruta de archivo dentro de tu proyecto>").getAbsolutePath()

If the route you want to obtain is

D:\EscritorioWindows\Proyecto\src\recursos\records.txt

It would be obtained in this way:

String directorio = new File ("src/recursos/records.txt").getAbsolutePath();
    
answered by 05.11.2018 в 22:50