题主, 因入职新公司, 表设计混乱, 不得不手动写一个小脚本,获取所有字段后,重新写入至新表中;
思路
顺序如下
原sql
具体, 获取行 , 根据行开头的" ,"截取内容, 重新输入到txt, 中就可以了;
代码
这里就写下 java8 的如何读取和写入吧
package xxx.xxx.xx;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.nio.charset.Charset;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.*;import java.util.stream.Collectors;import java.util.stream.Stream;/** * @author :wuxw * @ClassName : test * @description: TODO * @date :2019/3/22 11:54 * @version: 1.0 */public class test { public static void main(String[] args){ String filePath = "e:/textDb.txt"; ListlineLists = readTextFile(filePath); // 将每行内容分割 最后输出到文本 Map tempMap = new HashMap (); lineLists.forEach(i ->{ String[] temp = i.split("\\s+"); ColumnBean tempBean = new ColumnBean(); if(temp.length>0){ tempBean.setCode(temp[1]); tempBean.setCtype(temp[2]); tempBean.setIsEmpty(temp[3]); if(temp.length == 6 ){ tempBean.setCommont(temp[5]); } if(temp.length == 7 ){ tempBean.setNote(temp[6]); } if(!tempMap.containsKey(temp[1])){ tempMap.put(temp[1],tempBean); } } }); for(ColumnBean c : tempMap.values()){ System.out.println(c.toString()+"----------"); } List mapKeyList = new ArrayList (tempMap.values()); mapKeyList.forEach(i -> { System.out.println("--------"+i.toString()); }); beanToTxt("e:/beanDb3.txt",mapKeyList); } /** * @Author wuxw * @Description 行内容版 - 将字符串内容写入文本 * @Date 17:12 2019/3/22 * @Param [filePath, content] * @return void **/ public static void contentToTxt(String filePath, String content) { try{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath),true)); writer.write(content); writer.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("文件写入完毕,文件路径:"+filePath); } /** * @Author wuxw * @Description 数组循环写入文本 * @Date 17:12 2019/3/22 * @Param [filePath, listStr] * @return void **/ public static void contentToTxt(String filePath, List listStr) { try{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath),true)); listStr.forEach(str ->{ try { if(str.startsWith(" `")) { writer.newLine(); writer.write(str); } } catch (IOException e) { e.printStackTrace(); } }); writer.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("文件写入完毕,文件路径:"+filePath); } /** * @Author wuxw * @Description bean转成行输出至txt * @Date 17:11 2019/3/22 * @Param [filePath, listStr] * @return void **/ public static void beanToTxt(String filePath, List listStr) { try{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath),true)); listStr.forEach(str ->{ try { writer.newLine(); writer.write(str.toString()); } catch (IOException e) { e.printStackTrace(); } }); writer.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("文件写入完毕,文件路径:"+filePath); } /** * @Author wuxw * @Description 根据文件路径读取文件内容 * @Date 17:10 2019/3/22 * @Param [file] * @return java.util.List **/ public static List readTextFile(String file){ //读取文件 List lineLists = null; try{ lineLists = Files.lines(Paths.get(file), Charset.defaultCharset()) .flatMap(line -> Arrays.stream(line.split("\n"))) .collect(Collectors.toList()); } catch (IOException e) { //Your exception handling here } return lineLists; }}
/** * @author :wuxw * @ClassName : ColumnBean * @description: TODO * @date :2019/3/22 15:12 * @version: 1.0 */@Datapublic class ColumnBean { private String code; private String ctype; private String isEmpty; private String commont; private String note; public String toString(){ return code+" "+ctype+" "+isEmpty+" "+commont+" "+note; }}