0

I simply want to add some text on my JTextPane and I'm having the following problem:

I am using the file class to load an external textfile:

File file = new File("/my/program/pics/mytext.txt");

but the problem is that I always get FileNotFound exception! After a bit of research though I realised that only absolute filepaths work like for instance "c:\myfiles\mytext.txt"

What's wrong and how can I make it load a file stored in my package? (that is relative position)

Thanks

3 Answers 3

3

To get a classpath resource as InputStream, use Class#getResourceAsStream().

InputStream input = getClass().getResourceAsStream("/my/program/pics/mytext.txt");
Sign up to request clarification or add additional context in comments.

6 Comments

It doesn't make any sense...I just did what you said and the filepath is correct! but for some reason it won't load the file. On the other hand if I copy the file to another location on disk and provide the absolute path it works....!
You told that the file is in a package, so it is in the classpath, right? It is in the my.program.pics package? Or maybe you was referring to the first version of my answer, before I realized that I misunderstood your question because you mentioned "external textfile".
right!Here't the deal: my package is called my.program and then there is another one my.program.pics where I store my pictures etc. The weird thing is that I place icons for my buttons using genBut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/my/program/pics/developer.png"))); but the same path won't work in the File class!
Don't use File to obtain a classpath resource. It's simply the wrong tool. Use Class.getResource() or getResourceAsStream() as I pointed out in my answer.
That's effectively a new question :) Wrap it with InputStreamReader like so new BufferedReader(new InputStreamReader(input, "UTF-8")); where "UTF-8" is the character encoding the file is using.
|
2

If you want to load resources from your class path (in other words, from within your package structure) you would have to use a different strategy.

(Sure, it's still a file on your file system, but when your application is distributed it most likely will not be)

Resources from the class path can be loaded using a class loader, like so:

InputStream stream = getClass().getResourceAsStream("/path/to/your/file/")

This path begins at the root of your class path/package structure.

Comments

0

You should make it work with a relative path. You must consider the "starting folder" as the folder in which the program will execute. For instance, if you are running it from Eclipse, the starting folder is the root folder of your project (the one containing the .project file).

For example, if your file is located in the folder pics in the root folder, use this code :

File file = new File("pics/mytext.txt"); 

1 Comment

This however makes the application dependent on the way how you started it, you'd rather like to avoid this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.