gpt4 book ai didi

Java删除文本文件中的空行

转载 作者:行者123 更新时间:2023-12-02 06:04:53 28 4
gpt4 key购买 nike

我从文本文件中删除了一些行,工作正常,但空行有问题。那些仍在 .txt 文件中,我不知道如何删除或放置它们,我在谷歌和此处搜索了解决方案,但失败了。有人知道如何删除空白行吗?我尝试过:

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

坦克

    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();


}

}

最佳答案

为什么不在currentLine = currentLine.trim();之后添加以下代码:

if (currentLine.isEmpty())
continue;

关于Java删除文本文件中的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22377964/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com