scala :How to read file in jar

I have directory structure like this

  • src

    • main
      • resources
        • text.txt
      • scala
        • hello
          • world.scala
    • test
      • same as main folder
  • pom.xml

When in IDE (Intellij10), I could access it with relative path ("src/main/resource/text.txt") but it seems I can not do that when I compile in jar. How to read that file ?

also, I found that test.txt is copy into root of jar. Is this normal behavior ? Since I fear this will be clash with other resources file in src/test/resources.

thanks

Answers


From http://www.java-forums.org/advanced-java/5356-text-image-files-within-jar-files.html -

Once the file is inside the jar, you cannot access it with standard FileReader streams since it is treated as a resource. You will need to use Class.getResourceAsStream().

The test.txt being copied into the root is not normal behavior and is probably a setting with your IDE.


Need Your Help

Can jboss snmp adapter(snmp agent) be extended to support my defined MIB?

jboss jmx snmp

I have our own MIB files, and I know in many cases, I have to write snmp agent to support my own MIB.

Injecting Spring @Resource into a Servlet

java spring tomcat dependency-injection annotations

I have the following piece of code, which I modeled on this answer:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.