Java sqlite转json二进制文件

时间:2025-03-28 18:15:09
package db2file;


import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;


import ;
import ;
import ;


public class DB2file {
public static void main(String[] args) {
String dbFile = "C:\\Users\\Administrator\\Desktop\\";
File file = new File(dbFile);


if (()) {
try {
("");
Connection conn = ("jdbc:sqlite:"
+ dbFile);


Statement stat = ();
/*** 省 ******/
ResultSet rs = ("select * from province");
JSONObject obj = new JSONObject();
JSONArray provinces = new JSONArray();
while (()) {
JSONObject province = new JSONObject();
("name", ("prov_name"));
String prov_code = ("prov_code");
("code", prov_code);


/*** 市 ******/
JSONArray citys = new JSONArray();
Statement stat2 = ();
ResultSet rs2 = stat2
.executeQuery("select * from city where parent_code= '"
+ prov_code + "'");
while (()) {
JSONObject city = new JSONObject();
("name", ("city_name"));
("code", ("city_code"));
(city);
}
();
("city", citys);
(province);
/*** 市 ******/
}
("province", provinces);

();
();
(());
String path = "d:\\";
outputFile(path, ());
("--------------");
String dataString=readFileBy(path);
(dataString);
ProvinceEntity provinceEntity=(ProvinceEntity) (dataString, );
((3).(2).name);

} catch (ClassNotFoundException e) {
();
} catch (SQLException e) {
();
} catch (JSONException e) {
();
}


}
}


/** 输出文件 */
public static void outputFile(String path, String data) {
File fileJson = new File(path);


FileWriter fileWritter;
try {
if (!()) {
();
}else {
();
}
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(fileJson, true), "UTF-8");
(data);
();


} catch (IOException e) {
();
}


}


/** 读取文件 */
public static String readFileBy(String fileName) {
File file = new File(fileName);
String data = null;
try {
if (() && ()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), "UTF-8");// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = ()) != null) {
// (lineTxt);
data = lineTxt;
}
();
return data;
} else {
("找不到指定的文件");
}
} catch (Exception e) {
();
}
return null;
}


}