java json转二进制数据_Java sqlite转json二进制文件

该Java程序从SQLite数据库中检索数据,将其转换为JSON格式,并保存为二进制文件。首先,它连接到SQLite数据库,查询省市区数据,然后将结果转换为JSONObject和JSONArray。最后,数据被写入一个名为'citydata.json'的文件,并读取回验证内容。
摘要由CSDN通过智能技术生成

package db2file; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class DB2file { public static void main(String[] args) { String dbFile = "C:\\Users\\Administrator\\Desktop\\citys.db"; File file = new File(dbFile); if (file.exists()) { try { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:" + dbFile); Statement stat = conn.createStatement(); /*** 省 ******/ ResultSet rs = stat.executeQuery("select * from province"); JSONObject obj = new JSONObject(); JSONArray provinces = new JSONArray(); while (rs.next()) { JSONObject province = new JSONObject(); province.put("name", rs.getString("prov_name")); String prov_code = rs.getString("prov_code"); province.put("code", prov_code); /*** 市 ******/ JSONArray citys = new JSONArray(); Statement stat2 = conn.createStatement(); ResultSet rs2 = stat2 .executeQuery("select * from city where parent_code= '" + prov_code + "'"); while (rs2.next()) { JSONObject city = new JSONObject(); city.put("name", rs2.getString("city_name")); city.put("code", rs2.getString("city_code")); citys.put(city); } rs2.close(); province.put("city", citys); provinces.put(province); /*** 市 ******/ } obj.put("province", provinces); rs.close(); conn.close(); System.out.println(obj.toString()); String path = "d:\\citydata.json"; outputFile(path, obj.toString()); System.err.print("--------------"); String dataString=readFileBy(path); System.out.println(dataString); ProvinceEntity provinceEntity=(ProvinceEntity) JsonUtils.jsonToObject(dataString, ProvinceEntity.class); System.out.println(provinceEntity.province.get(3).city.get(2).name); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } } /** 输出文件 */ public static void outputFile(String path, String data) { File fileJson = new File(path); FileWriter fileWritter; try { if (!fileJson.exists()) { fileJson.createNewFile(); }else { fileJson.delete(); } OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream(fileJson, true), "UTF-8"); osw.write(data); osw.close(); } catch (IOException e) { e.printStackTrace(); } } /** 读取文件 */ public static String readFileBy(String fileName) { File file = new File(fileName); String data = null; try { if (file.isFile() && file.exists()) { // 判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), "UTF-8");// 考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { // System.out.println(lineTxt); data = lineTxt; } read.close(); return data; } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { e.printStackTrace(); } return null; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值