怎么用idea建立java并打成jar包运行,简易版

前言:
接到一个需求要向postgresql数据库中导入手机号数据,手机号给的文件时明文,要改成密文再加到自己的数据库中。

  1. 数据量几万,如果用insert太耗资源,可以使用copy命令,命令使用方法

一、准备

idea新建一个java项目
https://jingyan.baidu.com/article/48b558e3f8f6637f39c09a44.html
打jar包
https://zhinan.sogou.com/guide/detail/?id=316512831667

环境准备:

  1. 登录postgres服务器,进命令行客户端psql -Uibank -d ibank,也可以用软件如DBeaver连接
  2. 运行添加保存手机号表sql语句
  3. CREATE TABLE ibank.phone ( phoneNum varchar(64) NOT NULL );

二、代码

怎么用idea建立java并打成jar包运行,简易版_第1张图片
DealMobilePhone代码:

public class DealMobilePhone {
    private static void transferFile(String srcFileName, String destFileName) throws Exception{
        //换行符
        String line_separator = System.getProperty("line.separator");
        System.out.println("ok");
        //读取输入文件
        FileInputStream fis = new FileInputStream(srcFileName);
        DataInputStream in = new DataInputStream(fis);
	        BufferedReader d = new BufferedReader(new InputStreamReader(in, "UTF-8"));
	String line = null;
	int l = 0;
	StringBuffer content = new StringBuffer();
	long timeStart = System.currentTimeMillis();
	System.out.println("开始读取文件,时间:" + new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));
	while ((line = d.readLine()) != null)
	{
	    String newLine = "";
	    //加密方法,这里就用base64替代了
    newLine = Base64.encodeBase64String(line.getBytes());
	content.append(newLine + line_separator);
	l++;
	if (l % 200000 == 0) {
	    System.out.println("已替换" + l + "行,时间" + new 	SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));
	}
	}
	System.out.println("内容替换结束:" + (System.currentTimeMillis() - timeStart));
	d.close();
	in.close();
	fis.close();
	long writeStart = System.currentTimeMillis();
	Writer ow = new OutputStreamWriter(new FileOutputStream(destFileName), "utf-8");
	ow.write(content.toString());
	ow.close();
	System.out.println("内容写入结束:" + (System.currentTimeMillis() - writeStart));
	}
	public static void main(String[] args) throws Exception {
    String srcFileName = args[0];//"C:\\Users\\lenovo\\Desktop\\mine\\a.txt";//args[0];
    String destFileName = args[1];//"C:\\Users\\lenovo\\Desktop\\mine\\b.txt";//args[1];
    transferFile(srcFileName, destFileName);

    System.out.println("ok");
}
}

运行jar包:

  1. 将jar包和手机号txt文件a.txt传到服务器,自己定义目录,如mkdir -p /data/jar
  2. cd /data/jar
  3. java -jar mobileNumber.jar a.txt b.txt
  4. head -3 b.txt查看运行结果
  5. psql -U用户名登录
  6. copy phone from b.txt;

你可能感兴趣的:(linux,jar,java,postgresql,linux)