如何读取d盘目录保存在list中且显示父子关系
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何读取d盘目录保存在list中且显示父子关系相关的知识,希望对你有一定的参考价值。
参考技术A 有没有那么多.给你个少的:importjava.io.BufferedReader;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;publicclassCopyFilepublicstaticvoidmain(String[]args)throwsFileNotFoundException,IOExceptionCopy("D://a.java","D://text.txt");//第一个是目标文件的目录,第二个是你想拷贝的目录publicstaticvoidCopy(StringF,StringD)throwsIOException,FileNotFoundExceptionStringfile=F;BufferedReaderin=newBufferedReader(newInputStreamReader(newFileInputStream(file),"GBK"));PrintWriterw=newPrintWriter(newOutputStreamWriter(newFileOutputStream(D),"utf-8"));//.readLine();每次读取一行,到\n,\r为止//如果到文件末尾,返回nullStringstr;while((str=in.readLine())!=null)System.out.println(str);w.write(str+"\n");in.close();w.close();追问保存在list集合中,不是拷贝到另一个目录
读取省市区json文件,插入数据库表,表中保存父子节点对应关系
java读取省市区json文件, 入库表t_city(code,val,fathercode), 保存父子节点
package manager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
public class CityNodeSqlGen
public static void main(String[] args)
String f = "I:/myData/qq_data/3442175060/FileRecv/cities1.js";
// String f = "I:/myData/qq_data/3442175060/FileRecv/bj.js";
File file = new File(f);
JSONArray jsonArray = JSONUtil.readJSONArray(file, CharsetUtil.charset("UTF-8"));
// System.out.println(json.toString());
List<CityNode> list = JSONUtil.toList(jsonArray, CityNode.class);
// System.out.println(JSONUtil.toJsonStr(cn));
pro(list);
private static void pro(List<CityNode> list)
String sql = null;
List<String> result = new ArrayList<>();
for (CityNode t_city : list)
String code = t_city.getCode();
String val = t_city.getValue();
sql = "insert into t_city(code,val,fathercode) values('" + code + "','" + val + "','" + 0 + "');";//省
// System.out.println(sql);
result.add(sql);
List<CityNode> children = t_city.getChildren();
for (CityNode city : children)
String code1 = city.getCode();
String val1 = city.getValue();
sql = "insert into t_city(code,val,fathercode) values('" + code1 + "','" + val1 + "','" + code
+ "');";//市
// System.out.println(sql);
result.add(sql);
List<CityNode> children2 = city.getChildren();
if (CollUtil.isNotEmpty(children2))
for (CityNode area : children2)
String code2 = area.getCode();
String val2 = area.getValue();
sql = "insert into t_city(code,val,fathercode) values('" + code2 + "','" + val2 + "','"
+ code1 + "');";//区
// System.out.println(sql);
result.add(sql);
File file = new File("I:\\\\项目代码和文档\\\\加油站项目\\\\t_city2.sql");
FileUtil.writeLines(result, file, "utf-8");
package manager;
import java.util.List;
import lombok.Data;
@Data
public class CityNode
private String code;
private String value;
private String label;
private List<CityNode> children;
以上是关于如何读取d盘目录保存在list中且显示父子关系的主要内容,如果未能解决你的问题,请参考以下文章
使用 MagicalRecord 从 JSON 中保存父子关系