2012-12-12 11 views
11

का उपयोग कर क्लासपाथ में विशिष्ट फ़ोल्डर में फ़ाइलों तक पहुंच बनाना मैं com.example.resources पैकेज में टेक्स्ट फ़ाइलों का एक समूह पढ़ना चाहता हूं।जावा

InputStream is = MyObject.class.getResourceAsStream("resources/file1.txt") 
InputStreamReader sReader = new InputStreamReader(is); 
BefferedReader bReader = new BufferedReader(sReader); 
... 

वहाँ एक रास्ता फ़ाइल की सूची और फिर getResourceAsStream के प्रत्येक तत्व पारित करने के लिए है: मैं निम्नलिखित कोड का उपयोग कर एक एकल फाइल पढ़ सकते हैं?

संपादित करें:

BufferedReader br = new BufferedReader(new InputStreamReader(MyObject.class.getResourceAsStream("resources"))); 
String fileName; 
while((fileName = br.readLine()) != null){ 
    // access fileName 
} 
+2

मैं क्लासपाथ में फ़ाइलों तक पहुंच बनाना चाहता हूं और सी: \\ संसाधनों जैसे किसी विशिष्ट फ़ोल्डर से नहीं। – Akadisoft

+1

शायद आप यह चाहते हैं: http://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory – nwaltham

+0

आप इसके लिए कोड का पुन: उपयोग कर सकते हैं (छोटे संशोधन के बाद) http: // stackoverflow .com/प्रश्न/176527/कैसे-कर-i-enumerate-all-classes-in-a-package-and-add-them-a-list – CAMOBAP

उत्तर

9

आप getResourceAsStream पद्धति के लिए एक निर्देशिका में पार कर लेते हैं तो यह निर्देशिका में फ़ाइलों की एक सूची वापस आ जाएगी (या इसके लिए कम से कम एक धारा)।

Thread.currentThread().getContextClassLoader().getResourceAsStream(...) 

मैंने जानबूझकर संसाधन प्राप्त करने के लिए थ्रेड का उपयोग किया क्योंकि यह सुनिश्चित करेगा कि मुझे मूल वर्ग लोडर मिल जाएगा। जावा ईई पर्यावरण में यह महत्वपूर्ण है हालांकि शायद आपके मामले के लिए बहुत अधिक नहीं है।

+0

काम कर रहा है हाय, मैं आपके उत्तर का उपयोग करने की कोशिश कर रहा हूं। क्या आप इसके साथ मेरी मदद कर सकते हैं: http://stackoverflow.com/questions/29430113/accessing-files-in- विशिष्ट- फ़ोल्डर-in-classpath-using-java-works-only-for-sing? – tch

0

आप क्या चाहते हैं thats मुझे लगता है कि:

String currentDir = new java.io.File(".").toURI().toString(); 
// AClass = A class in this package 
String pathToClass = AClass.class.getResource("/packagename).toString(); 
String packagePath = (pathToClass.substring(currentDir.length() - 2)); 

String file; 
File folder = new File(packagePath); 
File[] filesList= folder.listFiles(); 

for (int i = 0; i < filesList.length; i++) 
{ 
    if (filesList[i].isFile()) 
    { 
    file = filesList[i].getName(); 
    if (file.endsWith(".txt") || file.endsWith(".TXT")) 
    { 
     // DO YOUR THING WITH file 
    } 
    } 
} 
+0

आप पैकेज नाम से 'फ़ाइल' ऑब्जेक्ट प्राप्त करने की योजना कैसे बनाते हैं ' स्ट्रिंग'? – CAMOBAP

+0

मैंने फ़ाइल फ़ोल्डर = नई फ़ाइल ("/ com/example/resource") की कोशिश की है और यह NullPointerException – Akadisoft

+0

फेंक रहा है और –

3

This SO thread विस्तार से इस तकनीक पर चर्चा ramsinb सुझाव पर मैं इस प्रकार मेरी कोड बदल दिया है। नीचे एक उपयोगी जावा विधि है जो किसी दिए गए संसाधन फ़ोल्डर से फ़ाइलों को सूचीबद्ध करती है।

/** 
    * List directory contents for a resource folder. Not recursive. 
    * This is basically a brute-force implementation. 
    * Works for regular files and also JARs. 
    * 
    * @author Greg Briggs 
    * @param clazz Any java class that lives in the same place as the resources you want. 
    * @param path Should end with "/", but not start with one. 
    * @return Just the name of each member item, not the full paths. 
    * @throws URISyntaxException 
    * @throws IOException 
    */ 
    String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException { 
     URL dirURL = clazz.getClassLoader().getResource(path); 
     if (dirURL != null && dirURL.getProtocol().equals("file")) { 
     /* A file path: easy enough */ 
     return new File(dirURL.toURI()).list(); 
     } 

     if (dirURL == null) { 
     /* 
     * In case of a jar file, we can't actually find a directory. 
     * Have to assume the same jar as clazz. 
     */ 
     String me = clazz.getName().replace(".", "/")+".class"; 
     dirURL = clazz.getClassLoader().getResource(me); 
     } 

     if (dirURL.getProtocol().equals("jar")) { 
     /* A JAR path */ 
     String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file 
     JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); 
     Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar 
     Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory 
     while(entries.hasMoreElements()) { 
      String name = entries.nextElement().getName(); 
      if (name.startsWith(path)) { //filter according to the path 
      String entry = name.substring(path.length()); 
      int checkSubdir = entry.indexOf("/"); 
      if (checkSubdir >= 0) { 
       // if it is a subdirectory, we just return the directory name 
       entry = entry.substring(0, checkSubdir); 
      } 
      result.add(entry); 
      } 
     } 
     return result.toArray(new String[result.size()]); 
     } 

     throw new UnsupportedOperationException("Cannot list files for URL "+dirURL); 
    }