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
CodePudding user response:
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();
}
}
CodePudding user response:
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();
}