Figen Güngör
Figen Güngör

Reputation: 12559

How can i find the path of my file in my Java project file?

I put a file inside my Java project file and i want to read it but how can i find the path name with Java.

Here i put it in C driver but i just want to find path by just writing the name of file. Is there a function for it?

   FileInputStream fstream1 = new FileInputStream("C:/en-GB.dic");

Upvotes: 1

Views: 424

Answers (2)

RP-
RP-

Reputation: 5837

Place it in your classpath, If it is a web application WEB-INF/classes/yourfile.ext, if it is a standalone application place it in bin directory of your application (default class directory is bin).

Then you could read by using one of the following ways.

InputStream in = this.getClass().getClassLoader().getResourceAsStream("yourfile.ext");

Or

InputStream in = this.getClass().getResourceAsStream("/yourfile.ext");

You can read online for the differences between above two approaches.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691625

If the file is inside the jar file generated for your project (or in the classpath used by your project, generally), under the package com.foo.bar, you can load it using

SomeClassOfYourProject.class.getResourceAsStream("/com/foo/bar/en-GB.dic");

If it's not in the classpath, and you launch the application (using java.exe) from the directory c:\baz, and the file is under c:\baz\boom\, the you can load it using

new FileInputStream("boom/en-GB.dic");

Upvotes: 3

Related Questions