Frage

I delete some lines from an text file that works fine but I have an problem with blank lines. Those still inside the .txt file and I don't know how to remove or put those up I searched for an solution on google and here but I failed. Have anybody an idea how I can remove blank lines? I tried it with:

 currentLine.trim().length() == 0 ); but still with out success

Tanks

    public static String COMMENT_LINE = "--.*";
    public static String CREATE_BUFFERPOOL = "CREATE BUFFERPOOL.*";
    public static String GRANT_USE = "GRANT USE.*";
    public static String CONNECT_TO = "CONNECT TO.*";
    public static Logger log = Logger.getLogger(Main.class);



    //CHANGE PATH
    public static String INPUT_FILE_PATH = "C://Users//dpa//Desktop//BW//bwcsvtest.txt";
    public static String OUTPUT_FILE_PATH "C://Users//dpa//Desktop//BW//BWFormated.txt";
    public static String TRANSFORM_FILE_PATH = "C://Users//dpa//Desktop//BW//BWtransformed.txt";
    public static String CSV_FILE_PATH = "C://Users//dpa//Desktop//BW//result.csv";

    public static void main(String[] args) throws IOException {
        //log.debug("Formating File");
        formatTxt(INPUT_FILE_PATH,OUTPUT_FILE_PATH);
        log.debug("Formating File complete");
        //CsvTransformer csvTransformer = new CsvTransformer(OUTPUT_FILE_PATH,TRANSFORM_FILE_PATH);
        //csvTransformer.parseCSVInput();
        //csvTransformer.writeDataToCsv(CSV_FILE_PATH);

    }

    public static void formatTxt(String inputFilePath, String outputFilePath) throws IOException {
        File inputFile = new File(inputFilePath);
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));

        File tempFile = new File(outputFilePath);
        BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
        String currentLine;
        while ((currentLine = reader.readLine()) != null) {
            currentLine = currentLine.trim();
            if (currentLine.matches(COMMENT_LINE)) {
                log.debug(currentLine);
                log.debug("Commentline deleted");
                continue;
            }
            if (currentLine.matches(CREATE_BUFFERPOOL)) {
                log.debug(currentLine);
                log.debug("CREATE BUFFERPOOL deleted");
                continue;
            }
            if (currentLine.matches(GRANT_USE)) {
                log.debug(currentLine);
                log.debug("GRANT USE deleted");
                continue;
            }
            if (currentLine.matches(CONNECT_TO)) {
                log.debug(currentLine);
                log.debug("CONNECT TO deleted");
                continue;           
            }

            writer.write(currentLine.replace("\t", ""));
            writer.newLine();

        }
        reader.close();
        writer.close();


    }

}
War es hilfreich?

Lösung

Why not add, just after currentLine = currentLine.trim();, this code:

if (currentLine.isEmpty())
    continue;

Andere Tipps

currentLine = currentLine.trim();
if (!currentLine .equals("")) // don't write out blank lines
    {
        writer.write(currentLine , 0, currentLine .length());
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top