Desidero leggere un gruppo di file di testo nel pacchetto com.example.resources. Posso leggere un singolo file utilizzando il seguente codice:Accesso ai file in una cartella specifica in classpath utilizzando Java
InputStream is = MyObject.class.getResourceAsStream("resources/file1.txt")
InputStreamReader sReader = new InputStreamReader(is);
BefferedReader bReader = new BufferedReader(sReader);
...
C'è un modo per ottenere l'elenco di file e quindi passare ogni elemento a getResourceAsStream
?
EDIT: Su suggerimento ramsinb ho cambiato il mio codice come segue:
BufferedReader br = new BufferedReader(new InputStreamReader(MyObject.class.getResourceAsStream("resources")));
String fileName;
while((fileName = br.readLine()) != null){
// access fileName
}
Desidero accedere ai file in classpath e non da una cartella specifica come C: \\ resources. – Akadisoft
Forse vuoi questo: http://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory – nwaltham
Puoi riutilizzare il codice per questo (dopo piccole modifiche) http: // StackOverflow .com/questions/176527/how-can-i-enumerate-all-classes-in-a-package-and-add-them-to-a-list – CAMOBAP