java读取txt中的内容,批量处理重复的内容,对数据库字段的修改非常有用!

先来一个简单的例子,比如给了我们db table中的几个字段,我们需要拼写sql语句去重复插入。

    给这样一个demo.txt的文本

image.png

执行以下程序后,拼成了可以多条插入的sql语句,方便!

image.png

github地址:https://github.com/furtech/java_utils/blob/master/src/main/java/com/furtech/javautils/TxtHandler.java

源代码如下

package com.furtech.javautils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;

/**
 * @des 输入一段文本,读取文本中的数据,并把它组装成我们需要的格式。简单又方便
 *
 * @author [email protected] | [email protected] | 有问题可以邮箱或者github联系我
 * @date 2019/8/3 11:56
 */
public class TxtHandler {

    private static final Logger logger = LoggerFactory.getLogger(TxtHandler.class);

    public static void main(String[] args) {
        String filePath = "filePath";
        readText(filePath);
    }

    public static void readText(String filePath) {
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            try {
                String txt = " ";
                InputStreamReader is = new InputStreamReader(new FileInputStream(file), "utf-8");
                BufferedReader br = new BufferedReader(is);
                while ((txt=br.readLine())!=null) {
                    String[] ss = txt.split(":");
                    System.out.println("insert into t_demo(name, id) values (" + ss[0] + "," + ss[1] + ")");
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}

你可能感兴趣的:(java读取txt中的内容,批量处理重复的内容,对数据库字段的修改非常有用!)