java的url方式、本地方式获取json文件内容
作者:饮罢千樽雪已老 时间:2023-08-22 18:30:23
因为工作原因需要读取json文件,最先是使用url方式不符合要求pass。又使用本地方式读取。记录一下方便后期查看。
注:因为资料都是从网上摘抄,如有问题请告知我。
1.url方式
/**
* 通过网络访问json并读取文件
* @param url:http://127.0.0.1:80/dashboard/dept_uuid.json
* @return:json文件的内容
*/
public static String loadJson (String url) {
StringBuilder json = new StringBuilder();
try {
URL urlObject = new URL(url);
URLConnection uc = urlObject.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
String inputLine = null;
while ( (inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json.toString();
}
2.本地文件读取
/**
* 通过本地文件访问json并读取
* @param path:E:/svn/05.Hospital/templatedept_uuid.json
* @return:json文件的内容
*/
public static String ReadFile(String path){
String laststr="";
File file=new File(path);// 打开文件
BufferedReader reader=null;
try{
FileInputStream in = new FileInputStream(file);
reader=new BufferedReader(new InputStreamReader(in,"UTF-8"));// 读取文件
String tempString=null;
while((tempString=reader.readLine())!=null){
laststr=laststr+tempString;
}
reader.close();
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOException el){
}
}
}
return laststr;
}
来源:https://blog.csdn.net/haizhihen123/article/details/54912636
标签:java,json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Dwr3.0纯注解(纯Java Code配置)配置与应用浅析二之前端调用后端
2023-08-19 17:32:33
![](https://img.aspxhome.com/file/2023/3/57693_0s.png)
辨析Java中的String与StringBuffer及StringBuilder字符串类
2023-08-21 19:59:50
android使用PullToRefresh实现下拉刷新和上拉加载
2023-08-06 11:06:58
![](https://img.aspxhome.com/file/2023/5/85455_0s.png)
java写入文件的几种方法分享
2023-06-26 15:56:55
Java CompletableFuture实现多线程异步编排
2023-07-22 22:57:02
![](https://img.aspxhome.com/file/2023/0/57710_0s.jpg)
Mybatis工具类JdbcTypeInterceptor运行时自动添加jdbcType属性
2023-08-24 03:49:59
Android中获取资源 id 及资源 id 的动态获取
2023-06-30 04:38:06
Android使用Websocket实现聊天室
2023-07-07 13:01:59
![](https://img.aspxhome.com/file/2023/6/60386_0s.png)
jsp如何获取Session中的值
2023-07-01 05:59:28
java web返回中文乱码问题及解决
2023-08-25 08:23:08
![](https://img.aspxhome.com/file/2023/8/58328_0s.png)
详解Flutter中视频播放器插件的使用教程
2023-06-15 23:47:31
![](https://img.aspxhome.com/file/2023/9/83519_0s.jpg)
详解C++ STL模拟实现forward_list
2023-06-21 02:36:04
![](https://img.aspxhome.com/file/2023/0/60100_0s.jpg)
opencv 做人脸识别 opencv 人脸匹配分析
2023-07-09 06:34:44
![](https://img.aspxhome.com/file/2023/8/101078_0s.jpg)
mybatis-generator自动生成dao、mapping、bean配置操作
2023-08-17 14:05:30
![](https://img.aspxhome.com/file/2023/8/57838_0s.jpg)
java编程下字符串的16位,32位md5加密实现方法
2023-07-29 21:34:28
AndroidStduio3.0 使用gradle将module打包jar文件的方法
2023-07-01 06:57:20
![](https://img.aspxhome.com/file/2023/4/84224_0s.png)
教你用Java在个人电脑上实现微信扫码支付
2023-07-22 20:52:15
![](https://img.aspxhome.com/file/2023/2/57722_0s.png)
Java Volatile关键字你真的了解吗
2023-08-09 20:28:23
![](https://img.aspxhome.com/file/2023/9/58029_0s.png)
Java多线程工具CompletableFuture的使用教程
2023-07-30 20:31:45
![](https://img.aspxhome.com/file/2023/1/57711_0s.jpg)
IDEA如何自动生成serialVersionUID的设置
2023-08-09 04:00:33
![](https://img.aspxhome.com/file/2023/1/57701_0s.png)