从Java中的文件夹中读取一堆JSON文件

发布于 2025-02-10 05:17:04 字数 570 浏览 3 评论 0原文

我试图在Java类中阅读一堆JSON文件。 我尝试了此操作

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

,我得到了一个stream&lt; string&gt;,其中包含JSON文件名的一堆字符串。我得到了所有的JSON名称字符串,但是如何访问每个JSON,例如将每个JSON转移到一个对象,或者

在这里操作是我的软件包结构

src
--MyClass.java
data
--One.json
--Two.json
--Three.json

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2025-02-17 05:17:05

需要按建议的“ tgdavies”读取单个文件而不是目录。

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 ...
    }
}

或使用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();
    }
}

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();
    }
}
乖乖哒 2025-02-17 05:17:04

如果我正确理解,您只想读取某些文件夹中的所有.json文件。

您可以使用java.nio软件包来遍历所有文件并轻松实现相同的内容。
示例:
文件夹结构

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

通过data文件夹和获取.josn文件

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();
}  

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();
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文