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; } }
java json转二进制数据_Java sqlite转json二进制文件
最新推荐文章于 2024-07-16 13:39:57 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)