Java read file classpath




















Here is working sample code I prepared, after days wasting my development time, I found the complete end-to-end solution, hope this helps to save your time. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to really read text file from classpath in Java Ask Question.

Asked 12 years, 3 months ago. Active 1 year ago. Viewed k times. Not a user variable. Add a comment. Active Oldest Votes. Therefore you don't need a leading slash. So for example, take this code: package dummy; import java. Test Results: true true. Jon Skeet Jon Skeet 1. You mixed relative and absolute paths up. All other paths are relative to the package of the class on which you call getResourceAsStream — Aaron Digulla.

No, you broke my example. I'll edit the comments to make them clearer, but the point is that using ClassLoader all paths are assumed to be absolute already. There's nothing for them to be relative to. Also do not use Java. It wont work on windows. Pradhan: No, you shouldn't be using File. Separator - because you're not asking for a file , you're asking for a resource. It's important to understand that the abstraction involved isn't the file system.

If you're specifying an absolute resource, then any call using the same classloader will do the same thing. Show 12 more comments. I can't find a decent , explicit and straightforward way on how to use it in the Internet : — user Works like a charm. The provided one liner is all you need.

Use the stream parsing from other examples if you don't know how to get a string from the stream. I had a bit of trouble figuring out exactly what to do with the resource variable as well. I've edited the answer with a bit more detail — DavidZemon. I was already using Spring and trying the "pure java" way. It was killing me, the differences between getResource, getResourceAsStream, etc, with no good working examples.

This is a perfect example of encapsulation, so I don't have to care. Be careful, if you package your project into a jar you should use an InputStream. If you use a File it work in your IDE but will fail if you test it from the jar. If you really need a File try with stackoverflow.

Show 1 more comment. Charset; import java. Files; import java. To read into a single String try. Best solution for me, as it doesn't need any dependencies, like Spring or Commons IO. This will fail if your resource file is inside a jar, e.

In that case you'll need to use something like Spring 's StreamUtils. A relative path might work better in this case. Chaitanya: Can you run the example from John Skeet's answer?

Here are a few good examples for getResourceAsStream — drorw. Keval Trivedi Keval Trivedi 1, 2 2 gold badges 12 12 silver badges 28 28 bronze badges. CDT The Properties. This is the "try with resources" syntax introduced with Java 7.

DrewWills yes, we know; the comments were regarding the original code posted which did not use the Java 7 try-with-resources construct — obataku. Show 3 more comments. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Linked 1. Related InputStream ;. InputStreamReader ;. URISyntaxException; import java. Charset; import java. Files; import java. Paths; import java. URISyntaxException ;. Charset ;. Files ;. Paths ;. List ;. InputStream; import org. IOUtils ;. URL; import org. IOUtils; import org.



0コメント

  • 1000 / 1000