通过java程序,移除空白行-软著代码工具

工具目的:

移除软著摘出代码文件中的空白行。
只针对文本文件,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);
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值