前置准备
1、电脑需要安装并配置JAVA开发环境
2、电脑需要下载安装JAR包转EXE工具
3、需要了解最基础的JAVA开发知识和,MAVEN项目构建知识
一、创建一个MAVEN工程
本次示范的DEMO功能要实现的功能非常简单,将一个文件夹下的文件的内容挨个复制,合并到一个文本中。
全部代码如下:
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws InterruptedException {
while (true) {
try {
String inputFolder;
Scanner sc = new Scanner(System.in);
if (args.length >= 1) {
inputFolder = args[0];
} else {
print("请输入需要合并的文本所在文件夹的目录(如 C:\\Users\\admin\\Desktop\\log)", Ansi.Color.CYAN);
inputFolder = sc.next();
}
File folder;
while (true) {
folder = new File(inputFolder);
if (!folder.exists() || !folder.isDirectory()) {
print("输入的不是一个文件夹路径或该文件夹不存在,请重新输入", Ansi.Color.RED);
inputFolder = sc.next();
} else {
break;
}
}
File output = new File(folder.getParent() + File.separator + "output.txt");
if (!output.exists()) {
output.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(output));
File[] fs = folder.listFiles();
for (int i = 0; i < fs.length; i++) {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fs[i]), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
}
bw.close();
print("合并完毕,合并的文本所在目录:" + output.getAbsolutePath(), Ansi.Color.GREEN);
} catch (IOException o) {
print("发生io异常,已终止程序", Ansi.Color.RED);
o.printStackTrace();
Thread.sleep(10000);
return;
}
}
}
public static Ansi formatStr(String content, Ansi.Color colour) {
return Ansi.ansi().fg(colour).a(content);
}
public static void print(String content, Ansi.Color colour) {
AnsiConsole.systemInstall();
System.out.println(formatStr(content, colour));
AnsiConsole.systemUninstall();
}
}
二、MAVEN打包配置
DEMO中引入了第三方的jar包,我们等下转成exe需要我们的jar包是带了完整库的,否则在别的环境运行会因为没有库报错,所以maven需要配置把第三方jar包一块打进去。
POM文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.52pojie</groupId>
<artifactId>merge</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main</mainClass> <!-- 此处为主入口main的路径-->
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!--自己的主类路径-->
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
|