我正在编写一个简单的函数来删除csv文件中的所有空行:
import java.io.*;
import java.util.*;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.PrintWriter;
public class WhiteSpace{
static List<String> AggAndGroupIndices = new LinkedList<String>();
public static void main(String[] args){
System.out.println("Pre-whitespace");
removeWhiteSpace("myFile");
System.out.println("Post-whitespace");
}
//////////////////////////////////////////////////////////////////////////////
public static void removeWhiteSpace(String csv_filename){
System.out.println("Whitespace removal activated");
Scanner file;
PrintWriter writer;
try {
file = new Scanner(new File(csv_filename));
writer = new PrintWriter("WhiteSpaceRemoval.csv");
System.out.println(file.hasNext());
System.out.println("About to enter while loop");
while (file.hasNext()) {
System.out.println("In while loop");
String line = file.nextLine();
if (!line.isEmpty()) {
writer.write(line);
writer.write("\n");
}
}
System.out.println("While loop complete");
file.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
这应该相对简单,但是,只要使用非标准字符(如é或Ç),它就会中断。即,以下csv文件:
province,name,month
Quebec,Franois,February
完美无缺,但
province,name,month
Quebec,François,February
根本不打印到WhiteSpaceRemoval.csv。此外,根据我的检查,根本没有输入while循环,甚至没有输入第一个有效行。这让我感到困惑,我真的迷失了可能出错的地方。有谁知道这可能是什么?在Linux上运行。
答案 0 :(得分:0)
如果你想过滤空行,你可以:
public List<String> filterNonEmptyLinesOnFile(String fileName) {
List<String> list = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
list = stream.filter(line -> !line.isEmpty())
.collect(Collectors.toList());
} catch (IOException e) { e.printStackTrace();
} finally { return list; }
}