保存数据到本地文件


  1. private void saveDataToFile(String fileName,String data) {
  2. BufferedWriter writer = null;
  3. File file = new File("d:\\"+ fileName + ".json");
  4. //如果文件不存在,则新建一个
  5. if(!file.exists()){
  6. try {
  7. file.createNewFile();
  8. } catch (IOException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. //写入
  13. try {
  14. writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
  15. writer.write(data);
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }finally {
  19. try {
  20. if(writer != null){
  21. writer.close();
  22. }
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. System.out.println("文件写入成功!");
  28. }

取数据


  1. private String getDatafromFile(String fileName) {
  2. String Path="d:\\" + fileName+ ".json";
  3. BufferedReader reader = null;
  4. String laststr = "";
  5. try {
  6. FileInputStream fileInputStream = new FileInputStream(Path);
  7. InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
  8. reader = new BufferedReader(inputStreamReader);
  9. String tempString = null;
  10. while ((tempString = reader.readLine()) != null) {
  11. laststr += tempString;
  12. }
  13. reader.close();
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. } finally {
  17. if (reader != null) {
  18. try {
  19. reader.close();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. return laststr;
  26. }

java 保存和读取本地json文件的更多相关文章

  1. 读取本地json文件,并转换为dictionary

    // 读取本地JSON文件 - (NSDictionary *)readLocalFileWithName:(NSString *)name { // 获取文件路径 NSString *path = ...

  2. jQuery ajax读取本地json文件

    jQuery ajax读取本地json文件 json文件 { "first":[ {"name":"张三","sex": ...

  3. JavaScript读取本地json文件

    JavaScript读取本地json文件 今天调试了一上午,通过jQuery读取本地json文件总是失败,始终找不出原因,各种方法都试了 开始总以为是不是json格式的问题.高了半天不行 后来读了一个 ...

  4. 读取本地json文件,转出为指定格式json 使用Base64进行string的加密和解密

    读取本地json文件,转出为指定格式json   引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件StreamReader sr = ...

  5. android 读取本地json文件 解决显示乱码显示

    1.读取本地JSON ,但是显示汉字乱码 public static String readLocalJson(Context context,  String fileName){         ...

  6. 第三天,爬取伯乐在线文章代码,编写items.py,保存数据到本地json文件中

        一. 爬取http://blog.jobbole.com/all-posts/中的所有文章     1. 编写jobbole.py简单代码 import scrapy from scrapy. ...

  7. Java读取本地json文件

    背景 之前一直在弄一个Java爬虫,将爬取的信息保存到了数据库中.但这毕竟是一个课程设计,在设计前端GUI,展示数据的时候最开始是直接通过select语句从数据库中查找的,但我担心交给老师后,老师还要 ...

  8. 读取本地Json文件

    //读取Json文件  地区 //将文件拖到本地  获取json数据 //获取json文件路径 NSString *pathArea=[[NSBundle mainBundle] pathForRes ...

  9. [转]World Wind Java开发之五——读取本地shp文件

    World Wind Java 使用IconLayer图层类表现点和多点数据,使用RenderableLayer图层表现线和面数据,一个图层只能对应一组shape文件.World Wind Java首 ...

随机推荐

  1. net基础题

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有成员, 在类的内部才可以访问. protected : 保 ...

  2. JS错误记录 - 记录上次登陆的用户名

    <script> //步骤 1.submit => 用户名存进cookie 2. onload => 从cookie读取用户名 window.onload = function ...

  3. ab压测返回结果解析

    Server Software:        Apache/2.2.25 (服务器软件名称及版本信息)Server Hostname:        localhost (服务器主机名)Server ...

  4. iOS_03_为什么选择ios开发

    为什么选择ios开发 为什么要选择移动开发 * 手机将是人类最离不开的设备之一,硬件软件参数也越来越强,应用需求量剧增. * 移动互联(就是将移动通信和互联网二者结合起来)发展迅速,各大公司都对移动互 ...

  5. WCF 设计和实现服务协定(01)

    作者:jiankunking 出处:http://blog.csdn.net/jiankunking WCF 术语: • 消息 – 消息是一个独立的数据单元,它可能由几个部分组成,包含消息正文和消息头 ...

  6. POJ 2823 Sliding Window 线段树

    http://poj.org/problem?id=2823 出太阳啦~^ ^被子拿去晒了~晚上还要数学建模,刚才躺在床上休息一下就睡着了,哼,还好我强大,没有感冒. 话说今年校运会怎么没下雨!!!说 ...

  7. Engine工具栏按钮的使用详解

    转自原文 Engine自定义控件实现toolbar功能 Engine提供的工具条能够轻易实现各种操作,非常方便,可是不好的地方就是太死板了,toolbar的图标都不能改.因此需要自己做按钮做控件去实现 ...

  8. 【35.02%】【codeforces 734A】Vladik and flights

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 终端复用工具tmux的使用

    tmux的作用在于终端复用. 1. 在server上启动一个bash.并在里面执行tmux 2. 通过ssh远程登录server,执行tmux attach,就会切换到server上的那个bash中, ...

  10. dmalloc用法快速入门

    原文链接 常用内存泄露检测手段有 1 mtrace 2 memwatch 3 mpatrol 4 dmalloc 5 dbgmem 6 valgrind 7 Electric Fence dmallo ...