0

I have created a .txt file in my Eclipse Java project, and I want to find out the path to it so I can use it for a Scanner. I do not want to find out the path on my local drive, as I will be planning to share the program to someone else, and they will have a different folder structure, rather a path that can be used on anybodies machine.

Here is the code:

this.file = new File("<insert path here>");
MR JACKPOT
  • 206
  • 1
  • 3
  • 15
  • I think this question can help you:
    [https://stackoverflow.com/q/21060992/7405620](https://stackoverflow.com/q/21060992/7405620)
    – majid zareei Jul 19 '18 at 14:46
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jul 19 '18 at 14:52
  • Do you want to get the path programmatically? – user12346352 Jul 19 '18 at 14:59
  • @user12346352 I'm trying to find a good way of explaining it... is there a way to get a path that can be used no matter who has the source code (So not starting from my C: drive ) ? – MR JACKPOT Jul 19 '18 at 15:01
  • Have you considered using the `args` parameter of your main method so you can simply be given the absolute path from the command line? – nitind Jul 19 '18 at 19:13

3 Answers3

0

you can use :

= new File("Build Path"); (your .java file exist in your build path)

The build path is used for building your application. It contains all of your source files and all Java libraries that are required to compile the application.

Zeuzif
  • 318
  • 2
  • 14
0

In eclipse, the default behavior is for the Java system property user.dir to be set to the project directory. This is what dictates where the "root" of File operations is. So if you created a file test.txt in the root project directory, you should be able to access it with new File("test.txt").

However, as Andrew Thompson mentioned in his comment, the more correct method would be using embedded resources.

Jeff Brower
  • 594
  • 3
  • 13
0

Try one of These:

1.

System.getProperty("user.dir");

2.

File currentDirFile = new File(".");
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0, helper.length() - currentDirFile.getCanonicalPath().length());//this line may need a try-catch

I have not tested it just found while googling

user12346352
  • 176
  • 2
  • 20