java - Access files and folders in Jar files -


there many similar questions not duplicate. similar "can't access files in jar file" there slight difference. lot of experimentation , forum reading, managed access contents of file , display them within jar file. problem if put file inside folder program no longer work though works when run class file. problem because when make games , other programs having different folders organisation "maps" , "spritesheets". made experimental program route out problem , here code it:

        try {               inputstream = getclass().getresourceasstream("folder\\test.txt");               inputstreamreader isr = new inputstreamreader(is);               bufferedreader br = new bufferedreader(isr);               string line;               while ((line = br.readline()) != null)  {                 system.out.println(line);               }              br.close();              isr.close();              is.close();         } catch (exception ex) {} 

i didn't in catch section because had used find area problem. here directory structure:

jarfiletext   classes     com       sayedhajaj         folder           test.txt         jarfiletext.class         test.txt    source      com        sayedhajaj          jarfiletext.java 

when try access copy of text file not in folder, works fine. however, if try access copy in "folder", null returned.

this solution:

        try {               inputstream = getclass().getresourceasstream("folder/test.txt");               inputstreamreader isr = new inputstreamreader(is);               bufferedreader br = new bufferedreader(isr);               string line;               while ((line = br.readline()) != null)  {                 system.out.println(line);               }              br.close();              isr.close();              is.close();         } catch (exception ex) {} 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -