0

Is there a way to read a text file which is inside a package. Lets say that I want to access a file called "myTextFile.txt", which is in a package called "a".

I want to access it from a class called "MyClass" which is in the same package. What would be the path to "myTextFile.txt"? And would I be able to use bufferedreader like this: BufferedReader in = new BufferedReader(new FileReader(PATH));

Mario Super
  • 1
  • 1
  • 2
  • 1
    What do you mean by "a file inside a package"? My first guess is that you are storing text files in the same directory as your .java source files. This is a Bad Idea(TM) because it doesn't seem like you are organizing your files very well. I strongly suggest that you store your files in their own directory. – Code-Apprentice Oct 28 '12 at 00:41
  • Its just temporary, mainly for debugging purposes. Thanks for your suggestion. – Mario Super Oct 28 '12 at 00:43
  • If this is for testing, then create a separate folder/directory for testing resources. That way you can use them again in the future. – Code-Apprentice Oct 28 '12 at 00:46

6 Answers6

0

It sounds like you need to use one of the Class.getResource() or ClassLoader.getResource() methods:

URL url = MyClass.class.getResource("myTextFile.txt"); 
URL url = MyClass.class.getClassLoader().getResource("myTextFile.txt");

or

InputStream in = MyClass.class.getResourceAsStream("myTextFile.txt"); 
InputStream in = MyClass.class.getClassLoader().getResourceAsStream("myTextFile.txt");

See this question for a comparison, and an explanation of absolute versus relative paths.

Then you could do

new BufferedReader(new InputStreamReader(in));
Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
0

InputStream is = new BufferedInputStream( getClass(). getClassLoader(). getResourceAsStream("/a/myTextFile.txt"));

Should do the trick. Note please the structure of the arg, the root of the package is represented by "/" and so are your packages (i.e. directories)

Bruno Vieira
  • 3,884
  • 1
  • 23
  • 35
  • It gives me an "Invalid character constant" error. Any idea why? – Mario Super Oct 28 '12 at 00:45
  • you are using backslash "\" right? Not the slash "/" is that right? – Bruno Vieira Oct 28 '12 at 00:47
  • I tried both, / is giving me "Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )" and \ is giving me this error "The constructor FileInputStream(InputStream) is undefined" – Mario Super Oct 28 '12 at 00:50
  • It was meant to be an example of getting a InputStream from a file inside your package. I'll edit the post. The funny thing is that the "\" should be trigging this error, not the "/". – Bruno Vieira Oct 28 '12 at 00:52
0

Where is your project compiled to? Any paths will be relative to the program's location, or the full path if you specify, not the class. Once you get that path you can put it where PATH is.

However, is there any reason you're putting that file inside a package? Shouldn't it go in some sort of resources directory?

GraphicsMuncher
  • 4,583
  • 4
  • 35
  • 50
0

GetResource on Class works well. ResourceLocator in Spring is also a very flexible option as well.

chrislhardin
  • 1,747
  • 1
  • 28
  • 44
0

You can get always get resources via ClassLoader.getResourceAsStream() (or getResource()). They can also be accessed via Class.getResourceAsStream() (instead of ClassLoader). You could either use relative or absolute names (starting with /).

rvcoutinho
  • 326
  • 1
  • 10
-1

Just input the absoulte file path. i.e. your lcoal machine path. Should work.

sa_nyc
  • 971
  • 1
  • 13
  • 23