-2

I was trying to read a bunch of JSON files in a Java class. I tried this

InputStream is = Myclass.class.getResourceAsStream("/data");
InputStreamReader in = new InputStreamReader(is);
BufferedReader b = new BufferedReader(in);
Stream<String> lines = bufferedReader.lines();

I got a Stream<String> which contains the a bunch of Strings of the JSON file name. I got all the JSON name strings, but how can I access to each JSON, like transfer each JSON to an object or else operations

Here is my package structure

src
--MyClass.java
data
--One.json
--Two.json
--Three.json
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
lucasliu050
  • 11
  • 1
  • 1
  • 1
    Instead of giving `getResourceAsStream` the path to the directory, give it the path to one of the files. You'll need to do this for each resource. – tgdavies Jun 25 '22 at 00:35
  • yeah, make sense. but I was wondering how I can loop the folder, any Java method to do so – lucasliu050 Jun 25 '22 at 02:05
  • Not as straightforward as you might hope: https://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory Can you merge all your files into a single JSON file with a known name? – tgdavies Jun 25 '22 at 02:40

2 Answers2

2

If I understand correctly you only want to read all the .json files which are inside some folder.

You can use java.nio package to traverse through all the files and achieve the same easily.
Example:
Folder Strucure

src
--java files
data
--subfolder
   --abc.txt 
--another.txt
--one.json
--two.json

To traverse through data folder and fetch only .josn files

String dir =System.getProperty("user.dir")+"/data";
                    
try{
    List<Path> paths = Files.walk(Paths.get(dir),1) //by mentioning max depth as 1 it will only traverse immediate level   
                            .filter(Files::isRegularFile) 
                            .filter(path-> path.getFileName().toString().endsWith(".json")) // fetch only the files which are ending with .JSON
                            .collect(Collectors.toList());
    //iterate all the paths and fetch data from corresnponding file                                 
    for(Path path : paths) {
        //read the Json File . change here according to your logic
        String json = new String(Files.readAllBytes(path));
    }           
}catch (Exception e) {
    e.printStackTrace();
}  
Sayan Bhattacharya
  • 1,365
  • 1
  • 4
  • 14
1

Need to read individual files instead of the directory as 'tgdavies' suggested.

Path dir = Paths.get("data");
    
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.json")) {
    for (Path p : stream) {
        
        BufferedReader buffReader = Files.newBufferedReader(p);
        
        // rest of reading code ...
    }
}

Or reading all lines using java.nio.file.Files

Path dir = Paths.get("data");

try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.json")) {
    for (Path p : stream) {
        
        Stream<String> lines = Files.lines(p);
        String data = lines.collect(Collectors.joining(System.lineSeparator()));
        
        // rest of code logic ...
        lines.close();
    }
}
faisal
  • 44
  • 5