我有一个来自Java书籍示例的原始代码。该程序只是读取一个文本文件(“clients.txt”)。但是,我想将“clients.txt”修改为“clients.csv”,并让程序读取“.csv”文件。我已经按照这种格式解析与数据“”作为分隔符,并添加/更新两行代码在ReadTextFile.java
文件下只:使用扫描仪类读取CSV文件错误
新增/更新的代码:
input = new Scanner(new File("clients.csv"));
input.useDelimiter(",");
认为这是从.txt
文件到.csv
文件的简单修改,我得到了NoSuchElementException
或File improperly formed
打印消息。
我知道我还可以使用其他类,例如BufferedReader
和/或CsvReader
类,但只是想知道为什么input.useDelimiter()
方法在这里不起作用。
=====================
错误消息
Process started >>>
<<< Process finished. (Exit code 0)
java ReadTextFileTest
Process started >>>
Account First Name Last Name Balance
File improperly formed.
<<< Process finished. (Exit code 1)
============ =========
ReadTextFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalStateException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ReadTextFile
{
private Scanner input;
public void openFile()
{
try
{
// Orig code: input = new Scanner(new File("clients.txt")
input = new Scanner(new File("clients.csv")); // added/updated
input.useDelimiter(","); // added
}
catch (FileNotFoundException fileNotFoundException)
{
System.err.println("Error opening file.");
System.exit(1);
}
}
public void readRecords()
{
AccountRecord record = new AccountRecord();
System.out.printf("%-10s%-12s%-12s%10s\n", "Account",
"First Name", "Last Name", "Balance");
try
{
while (input.hasNext())
{
record.setAccount(input.nextInt()); // read account number
record.setFirstName(input.next()); // read first name
record.setLastName(input.next()); // read last name
record.setBalance(input.nextDouble()); // read balance
System.out.printf("%-10d%-12s%-12s%10.2f\n",
record.getAccount(), record.getFirstName(),
record.getLastName(), record.getBalance());
}
}
catch (NoSuchElementException elementException)
{
System.err.println("File improperly formed.");
input.close();
System.exit(1);
}
catch (IllegalStateException stateException)
{
System.err.println("Error reading from file.");
System.exit(1);
}
}
public void closeFile()
{
if (input != null)
input.close();
}
}
=====================
ReadTextFileTest.java
public class ReadTextFileTest
{
public static void main(String args[])
{
ReadTextFile application = new ReadTextFile();
application.openFile();
application.readRecords();
application.closeFile();
}
}
=====================
AccountRecord.java
public class AccountRecord
{
private int account;
private String firstName;
private String lastName;
private double balance;
public AccountRecord()
{
this(0, "", "", 0.0);
}
public AccountRecord(int acct, String first, String last, double bal)
{
setAccount(acct);
setFirstName(first);
setLastName(last);
setBalance(bal);
}
public void setAccount(int acct) { account = acct; } public int getAccount() { return account; }
public void setFirstName(String first)
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String last)
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setBalance(double bal)
{
balance = bal;
}
public double getBalance()
{
return balance;
}
}
==== =======================
clients.txt(原始文件)
100 Bob Jones 24.98
200 Steve Doe -345.67
300 Pam White 0.00
400 Sam Stone -42.16
500 Sue Rich 224.62
100 Bob Jones -4.98
200 Steve Doe 45.67
===============================================
clients.csv(从“.TXT”更改为“.CSV”文件),当它试图读取第一余额
100,Bob,Jones,24.98
200,Steve,Doe,-345.67
300,Pam,White,0.00
400,Sam,Stone,-42.16
500,Sue,Rich,224.62
100,Bob,Jones,-4.98
200,Steve,Doe,45.67
谢谢彼得。我使用了你的建议“Pattern delimiter = Pattern.compile(”,| \ r \ n | \\ s“);”现在代码就可以工作。 非常感谢您的帮助。 –