我想从 CSV 文件中恢复一个对象。我需要知道扫描仪是否有 2 个下一个值:scanner.hasNext()
问题是我的访问构造函数需要 2 个参数,我需要确保我的 csv 文件中至少还有 2 个参数。
这是相关代码:
/**
* method to restore a pet from a CSV file.
* @param fileName the file to be used as input.
* @throws FileNotFoundException if the input file cannot be located
* @throws IOException if there is a problem with the file
* @throws DataFormatException if the input string is malformed
*/
public void fromCSV(final String fileName)
throws FileNotFoundException, IOException, DataFormatException
{
FileReader inStream = new FileReader(fileName);
BufferedReader in = new BufferedReader(inStream);
String data = in.readLine();
Scanner scan = new Scanner(data);
scan.useDelimiter(",");
this.setOwner(scan.next());
this.setName(scan.next());
while (scan.hasNext()) {
Visit v = new Visit(scan.next(), scan.next());
this.remember(v);
}
inStream.close();
}
提前致谢