zoukankan      html  css  js  c++  java
  • 如何读写json文件

     

    代码如下: 
    Java代码  
    1. import java.io.BufferedReader;  
    2. import java.io.File;  
    3. import java.io.FileReader;  
    4. import java.io.FileWriter;  
    5. import java.io.IOException;  
    6. import java.io.PrintWriter;  
    7. import org.json.JSONException;  
    8. import org.json.JSONObject;  
    9.   
    10. public class readAndWriteJson {  
    11.   
    12.     /** 
    13.      * @param args 
    14.      * @throws JSONException 
    15.      * @throws IOException 
    16.      */  
    17.     public static void main(String[] args) throws JSONException, IOException {  
    18.         // TODO Auto-generated method stub  
    19.   
    20.         // String s = ReadFile("./src/test.json");  
    21.         // System.out.println(s);  
    22.   
    23.         JSONObject jsonObject = new JSONObject();  
    24.         jsonObject.put("1", "一");  
    25.         jsonObject.put("2", "二");  
    26.         jsonObject.put("3", "三");  
    27.         jsonObject.put("4", "四");  
    28.         jsonObject.put("5", "五");  
    29.         jsonObject.put("6", "六");  
    30.         jsonObject.put("7", "⑦");  
    31.         System.out.println(jsonObject);  
    32.   
    33.         writeFile("./src/test.json", jsonObject.toString());  
    34.     }  
    35.   
    36.     public static void writeFile(String filePath, String sets)  
    37.             throws IOException {  
    38.         FileWriter fw = new FileWriter(filePath);  
    39.         PrintWriter out = new PrintWriter(fw);  
    40.         out.write(sets);  
    41.         out.println();  
    42.         fw.close();  
    43.         out.close();  
    44.     }  
    45.   
    46.     public static String ReadFile(String path) {  
    47.         File file = new File(path);  
    48.         BufferedReader reader = null;  
    49.         String laststr = "";  
    50.         try {  
    51.             reader = new BufferedReader(new FileReader(file));  
    52.             String tempString = null;  
    53.             while ((tempString = reader.readLine()) != null) {  
    54.                 laststr = laststr + tempString;  
    55.             }  
    56.             reader.close();  
    57.         } catch (IOException e) {  
    58.             e.printStackTrace();  
    59.         } finally {  
    60.             if (reader != null) {  
    61.                 try {  
    62.                     reader.close();  
    63.                 } catch (IOException e1) {  
    64.                 }  
    65.             }  
    66.         }  
    67.         return laststr;  
    68.     }  
    69. }  
  • 相关阅读:
    就打排序算法总结
    php 垃圾回收机制写时复制和引用计数
    zend studio 使用断点调试
    SiteServer 迁移至 Windows 2008 R2 问题汇总
    [项目改造中的点滴]C#中IDataReader和DataSet的区别与使用场景
    顺序分支知识总结
    我的第一篇博客
    [原创]删除GRUB引导恢复Windows引导,不用下载任何工具
    在C++builder中使用正则表达式,非boost库,简单!~
    SQL 存储过程优化经验
  • 原文地址:https://www.cnblogs.com/runerering/p/6033704.html
Copyright © 2011-2022 走看看