java 保存和读取本地json文件
保存数据到本地文件
-
private void saveDataToFile(String fileName,String data) {
-
BufferedWriter writer = null;
-
File file = new File("d:\\"+ fileName + ".json");
-
//如果文件不存在,则新建一个
-
if(!file.exists()){
-
try {
-
file.createNewFile();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
//写入
-
try {
-
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
-
writer.write(data);
-
} catch (IOException e) {
-
e.printStackTrace();
-
}finally {
-
try {
-
if(writer != null){
-
writer.close();
-
}
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
System.out.println("文件写入成功!");
-
}
取数据
-
private String getDatafromFile(String fileName) {
-
-
String Path="d:\\" + fileName+ ".json";
-
BufferedReader reader = null;
-
String laststr = "";
-
try {
-
FileInputStream fileInputStream = new FileInputStream(Path);
-
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
-
reader = new BufferedReader(inputStreamReader);
-
String tempString = null;
-
while ((tempString = reader.readLine()) != null) {
-
laststr += tempString;
-
}
-
reader.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
} finally {
-
if (reader != null) {
-
try {
-
reader.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
return laststr;
-
}
java 保存和读取本地json文件的更多相关文章
- 读取本地json文件,并转换为dictionary
// 读取本地JSON文件 - (NSDictionary *)readLocalFileWithName:(NSString *)name { // 获取文件路径 NSString *path = ...
- jQuery ajax读取本地json文件
jQuery ajax读取本地json文件 json文件 { "first":[ {"name":"张三","sex": ...
- JavaScript读取本地json文件
JavaScript读取本地json文件 今天调试了一上午,通过jQuery读取本地json文件总是失败,始终找不出原因,各种方法都试了 开始总以为是不是json格式的问题.高了半天不行 后来读了一个 ...
- 读取本地json文件,转出为指定格式json 使用Base64进行string的加密和解密
读取本地json文件,转出为指定格式json 引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件StreamReader sr = ...
- android 读取本地json文件 解决显示乱码显示
1.读取本地JSON ,但是显示汉字乱码 public static String readLocalJson(Context context, String fileName){ ...
- 第三天,爬取伯乐在线文章代码,编写items.py,保存数据到本地json文件中
一. 爬取http://blog.jobbole.com/all-posts/中的所有文章 1. 编写jobbole.py简单代码 import scrapy from scrapy. ...
- Java读取本地json文件
背景 之前一直在弄一个Java爬虫,将爬取的信息保存到了数据库中.但这毕竟是一个课程设计,在设计前端GUI,展示数据的时候最开始是直接通过select语句从数据库中查找的,但我担心交给老师后,老师还要 ...
- 读取本地Json文件
//读取Json文件 地区 //将文件拖到本地 获取json数据 //获取json文件路径 NSString *pathArea=[[NSBundle mainBundle] pathForRes ...
- [转]World Wind Java开发之五——读取本地shp文件
World Wind Java 使用IconLayer图层类表现点和多点数据,使用RenderableLayer图层表现线和面数据,一个图层只能对应一组shape文件.World Wind Java首 ...
随机推荐
- net基础题
1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保 ...
- JS错误记录 - 记录上次登陆的用户名
<script> //步骤 1.submit => 用户名存进cookie 2. onload => 从cookie读取用户名 window.onload = function ...
- ab压测返回结果解析
Server Software: Apache/2.2.25 (服务器软件名称及版本信息)Server Hostname: localhost (服务器主机名)Server ...
- iOS_03_为什么选择ios开发
为什么选择ios开发 为什么要选择移动开发 * 手机将是人类最离不开的设备之一,硬件软件参数也越来越强,应用需求量剧增. * 移动互联(就是将移动通信和互联网二者结合起来)发展迅速,各大公司都对移动互 ...
- WCF 设计和实现服务协定(01)
作者:jiankunking 出处:http://blog.csdn.net/jiankunking WCF 术语: • 消息 – 消息是一个独立的数据单元,它可能由几个部分组成,包含消息正文和消息头 ...
- POJ 2823 Sliding Window 线段树
http://poj.org/problem?id=2823 出太阳啦~^ ^被子拿去晒了~晚上还要数学建模,刚才躺在床上休息一下就睡着了,哼,还好我强大,没有感冒. 话说今年校运会怎么没下雨!!!说 ...
- Engine工具栏按钮的使用详解
转自原文 Engine自定义控件实现toolbar功能 Engine提供的工具条能够轻易实现各种操作,非常方便,可是不好的地方就是太死板了,toolbar的图标都不能改.因此需要自己做按钮做控件去实现 ...
- 【35.02%】【codeforces 734A】Vladik and flights
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 终端复用工具tmux的使用
tmux的作用在于终端复用. 1. 在server上启动一个bash.并在里面执行tmux 2. 通过ssh远程登录server,执行tmux attach,就会切换到server上的那个bash中, ...
- dmalloc用法快速入门
原文链接 常用内存泄露检测手段有 1 mtrace 2 memwatch 3 mpatrol 4 dmalloc 5 dbgmem 6 valgrind 7 Electric Fence dmallo ...