从Java中的文件夹中读取一堆JSON文件
我试图在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
需要按建议的“ tgdavies”读取单个文件而不是目录。
或使用java.nio.file.files读取所有行
Need to read individual files instead of the directory as 'tgdavies' suggested.
Or reading all lines using java.nio.file.Files
如果我正确理解,您只想读取某些文件夹中的所有
.json
文件。您可以使用
java.nio
软件包来遍历所有文件并轻松实现相同的内容。示例:
文件夹结构
通过
data
文件夹和获取.josn
文件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
To traverse through
data
folder and fetch only.josn
files