先记录一个工具类,该类是将大文本文件分割,可以外部指定分割文件的大小
SplitFile.Java
package com.tencent.splitlarge.file;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class SplitFile {
public static void readFileByLines(String fileReadName,List<Integer> list) {
if(list == null || list.size() < 1){
throw new RuntimeException("输入的参数不正确");
}
String filePath = null;
if(fileReadName.contains("/")){
filePath = fileReadName.substring(0,fileReadName.lastIndexOf("/")+1);
}
if(fileReadName.contains("\\")){
filePath = fileReadName.substring(0,fileReadName.lastIndexOf("\\")+1);
}
//需要切分的文件份数
int size = list.size();
BufferedReader reader = null;
long count = 0;
String fileName = filePath + getDateString() + "_";
//1-20,21 - 51,
try {
FileWriter writer = new FileWriter(fileName + 1 + ".txt", true);
reader = new BufferedReader(new FileReader(fileReadName));
String lineTxt = null;
while ((lineTxt = reader.readLine()) != null) {
count ++;
writer.write(lineTxt);
writer.write("\r\n");
if(size > 1 && count >= list.get(0)){
writer.close();
writer = new FileWriter(fileName + 2 + ".txt", true);
}
if(size > 2 && count >= list.get(1) + list.get(0)){
writer.close();
writer = new FileWriter(fileName + 3 + ".txt", true);
}
if(size > 3 && count >= list.get(2) + list.get(1) + list.get(0)){
writer.close();
writer = new FileWriter(fileName + 4 + ".txt", true);
}
if(size > 4 && count >= list.get(3) + list.get(2) + list.get(1) + list.get(0)){
writer.close();
writer = new FileWriter(fileName + 5 + ".txt", true);
}
}
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
private static String getDateString(){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
return dateFormat.format(new Date());
}
public static void main(String[] args) {
String filePath = "D:\\test\\yy.txt";
ArrayList<Integer> list = new ArrayList<>();
list.add(100000);
list.add(100000);
list.add(100000);
list.add(100000);
list.add(100000);
readFileByLines(filePath,list);
}
}
在idea上点击file
打开项目结构:
选择Artifacts,点击+新增一个
按图中选择点击
选择main主类,指定启动类,按图中选择,将MANIFEST.MR文件的路径由java改为resources目录下面
选择main启动类
我的启动类如下,这个类,定义了一些由外部输入的参数,使用scanner来获取
package com.tencent.splitlarge.file;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ApplicationMain {
public static void main(String[]args){
if(args.length <= 0 ){
scannerInput();
}
killProcess();
}
private static void killProcess() {
Runtime rt = Runtime.getRuntime();
Process p = null;
try {
rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");
System.exit(0);
System.out.println("退出成功!");
} catch (IOException e) {
System.out.println("退出失败!");
}
}
private static void scannerInput() {
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("请输入需要分割文件的路径(输入exit为退出):");
String filePath = scanner.nextLine();
if(filePath.equals("exit")){
killProcess();
}
ArrayList<Integer> list = new ArrayList<>();
System.out.println("请输入需要分割文件的大小,使用逗号隔开");
String countString = scanner.nextLine();
String[] split = countString.split(",");
for (String s : split) {
int count = Integer.parseInt(s);
list.add(count);
}
try {
SplitFile.readFileByLines(filePath,list);
System.out.println("分割成功,生成文件和父文件目录同级.");
} catch (Exception e) {
System.out.println("分割文件失败!");
}
}
}
}
选择本工程名称的jar,右键,新建一个directory,取名称为libs
将项目中上面出现的所有的jar全部拖拽带libs目录中去
完成之后的效果如下
再点击apply和ok,保存
选择途中的点击
出现下图,点击rebuild,就生成了jar了
生成jar的位置在该项目的out文件夹中,生成的MANIFEST.MR文件在resources目录下
将jar文件copy出来,放在任意文件夹中,在同级目录下,新建.bat结尾的文件,名称任意
在新建的bat文件,写入下面的内容
splitlarge.jar 为你生成的jar的名称
java -jar splitlarge.jar
pause
以上都完成之后,直接双击bat脚本文件
就运行了你生成的jar,输入参数,就可以执行你的代码了