保存数据到本地文件


  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. UVA 11796 - Dog Distance 向量的应用

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. Codeforces Round #234 (Div. 2):B. Inna and New Matrix of Candies

    B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...

  3. vuex概念总结及简单使用实例

    原文 简书原文:https://www.jianshu.com/p/0546983f5997 大纲 1.什么是Vuex 2.什么是“状态管理模式”? 3.什么情况下应该使用 Vuex? 4.Vuex和 ...

  4. C++ 宏、范型和RTTI 浅析

    [摘要]        RTTI(Run-Time Type Identification)是面向对象程序设计中一种重要的技术. 现行的C++标准对RTTI已经有了明白的支持. 只是在某些情况下出于特 ...

  5. 度量空间(metric space)

    一个度量空间(metric space)由一个有序对(ordered pair)(M,d) 表示,其中 M 是一种集合,d 是定义在 M 上的一种度量,是如下的一种函数映射: d:M×M→R 且对于任 ...

  6. <p><img src="http://img.blog.csdn.net/20150823142545135?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""></p>

    /* 实现功能:用顺序表实现栈的各种操作 编译环境:Windows 64b,vc6.0 日期: 2015/7/20 作者:wtt561111 */ #define stack_max_num 10 # ...

  7. VCS编译仿真警告Warning

    VCS编译仿真警告Warning 问题描述 在较大的SOC集成中,通常使用Perl脚本例化子模块到Top层,然而,有时会出现例化出来的输入端口名没有在Top层定义,而且端口的位宽为1bit,那么,ve ...

  8. 【26.8%】【CF 46D】Parking Lot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. php课程 5-18 数组排序和合并拆分函数有哪些

    php课程  5-18   数组排序和合并拆分函数有哪些 一.总结 一句话总结:分类来记.这些函数自己都可以写,费点时间而已. 1.array_combine()和array_merge()的区别是什 ...

  10. toLocaleDateString()

    在处理时间问题的时候,遇到了一个bug,关于toLocaleDateString()在不同浏览器下的解析结果. 代码如下 浏览器解析结果如下 可以看到谷歌,火狐等浏览器的输出结果是这种格式的 *201 ...