我正在尝试从.txt
文件中读取数据,我需要忽略以//
开头的任何行或空白行,但是我似乎无法使定界符起作用正确。
public void readVehicleData()
throws FileNotFoundException
{
FileDialog fileBox = new FileDialog(myFrame, "Open", FileDialog.LOAD);
fileBox.setVisible(true);
String filename = fileBox.getFile();
File vehicleData = new File(filename);
Scanner scanner = new Scanner(vehicleData).useDelimiter("\"(,\")?");
while( scanner.hasNext() )
{
String lineOfText = scanner.nextLine();
System.out.println(lineOfText);
}
scanner.close();
}
这是我尝试读取的.txt文件:
// this is a comment, any lines that start with //
// (and blank lines) should be ignored
AA, TF-63403, MJ09TFE, Fiat
A, TF-61273, MJ09TFD, Fiat
A, TF-64810, NR59GHD, Ford
B , TF-68670,MA59DCS, Vauxhall
B, TF-61854, MJ09TFG, Fiat
B, TF-69215, PT09TAW, Peugeot
C, TF-67358, NR59GHM, Ford
答案 0 :(得分:1)
如果我正确理解了您的问题,则无需指定定界符。
Scanner scanner = new Scanner(vehicleData);
while( scanner.hasNext() )
{
String lineOfText = scanner.nextLine();
if(lineOfText.length() == 0 || lineOfText.startsWith("//"))
continue;
System.out.println(lineOfText);
}
scanner.close();