Thursday 24 November 2011

How to read a file (the caller class is in a jar and the file to read is on the same jar)

Note: With Spring I haven't found out a way to do it...

ClassPathResource, FileSystemResource and UrlResource were big failures....

Filestructure:

  • micharg/Caller.java
  • foo.xml

Code:

package micharg;

public class Caller {

 ...
InputStream in = getClass().getResourceAsStream("/foo.xml");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
StringBuilder xml = new StringBuilder();
String line;
String NEW_LINE = System.getProperty("line.separator");
while ((line = input.readLine()) != null)
  xml.append(line + NEW_LINE);
input.close();
System.out.println(xml.toString());
 ...

References

StackOverflow

No comments:

Post a Comment