保存数据到本地文件


  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. 重构——DataTable转泛型

         泛型简单介绍         泛型能够最大限度的重用代码.保护类型的安全.提高性能.         泛型最常见的用途是创建集合类         泛型数据类型中使用的信息可在执行时通过反射 ...

  2. <九度 OJ>题目1012:畅通project

    题目描写叙述: 某省调查城镇交通状况,得到现有城镇道路统计表.表中列出了每条道路直接连通的城镇.省政府"畅通project"的目标是使全省不论什么两个城镇间都能够实现交通(但不一定 ...

  3. sublime text2 基本配置及结合Python 环境

    参考: http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html http://www.zhihu.com/question/ ...

  4. 5DXTPlayer串口调试小结

    小结 过程总是艰难. 首先是没有准备好. 没有安装vs2012,安装的时候,出现各种状况,因为先安装的2013高版本,造成12安装不正确,程序编译出问题.没有办法,只好卸载vs2012,2013及其各 ...

  5. Ubuntu UEFI 模式下安装基本原则

    https://help.ubuntu.com/community/UEFI Introduction The Extensible Firmware Interface (EFI) or its v ...

  6. [PReact] Use Link State to Automatically Handle State Changes

    Storing and updating values inside a component’s local state (known as controlled components) is suc ...

  7. Setup iOS Development Environment.

    Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...

  8. 爬虫抓取页面数据原理(php爬虫框架有很多 )

    爬虫抓取页面数据原理(php爬虫框架有很多 ) 一.总结 1.php爬虫框架有很多,包括很多傻瓜式的软件 2.照以前写过java爬虫的例子来看,真的非常简单,就是一个获取网页数据的类或者方法(这里的话 ...

  9. angular表单的使用实例

    原文 https://www.jianshu.com/p/da1fd5396798 大纲 1.模板驱动表单的创建 2.响应式表单的创建 3.模板驱动型表单的自定义指令 4.响应式表单的自定义指令 5. ...

  10. mybatis-generator + mysql/ptsql

    用了mybatis-generator,我就不再想用注解了,这与我之前说的注解与XML并用是矛盾的,知识嘛,本来就是多元化的,今天喜欢这个,明天喜欢那个,哈哈,看了mybatis-generator下 ...