工具目的:
移除软著摘出代码文件中的空白行。
只针对文本文件,doc文件不支持。
使用方法:
javac RemoveBlankLinesUtil.java
java RemoveBlankLinesUtil YOUR_CODE_FILE
例:java RemoveBlankLinesUtil code.txt
执行完成提示如下,目标文件在当前目录下生成。
Success, check out file:**/YOUR_CODE_FILE_BlankLineRemovedFile
例:
Success, check out file:/home//下载/temp/code.txt_BlankLineRemovedFile
代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class RemoveBlankLinesUtil {
public static void main(String[] args) {
removeBlankLines(args);
}
private static void removeBlankLines(String[] args) {
println("Parsing start");
if (args == null || args.length == 0) {
printHelpMsg();
return;
}
String fileName = args[0];
File file = new File(fileName);
if (!file.exists()) {
println("file:" + fileName + " not exit!");
return;
}
String path = System.getProperty("user.dir");
println("currentPath:" + path);
String outFilePath = path + File.separator + fileName + "_BlankLineRemovedFile";
println("outFilePath:" + outFilePath);
try {
File outFile = new File(outFilePath);
if (outFile.exists()) {
outFile.delete();
}
outFile.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(outFile, true);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (!line.equals("")) {
println("writing msg:" + line);
fileOutputStream.write((line + "\r\n").getBytes());
} else {
println("skip blank line");
}
}
fileOutputStream.flush();
fileOutputStream.close();
bufferedReader.close();
println("Success, check out file:" + outFilePath);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
println("ERROR");
} catch (IOException e) {
e.printStackTrace();
println("ERROR");
}
}
private static void println(String msg) {
System.out.println(msg + "");
}
private static void printHelpMsg() {
String msg = "ERROR, You can uses 'java RemoveBlankLinesUtil FILE_PATH to remove blank lines in target file'";
println(msg);
}
}