我有一个扫描仪来读取 .csv 文件。
该文件与 .java 文件位于同一目录中,但似乎找不到该文件。
我能做些什么来解决这个问题?
Scanner scanner = new Scanner(new File("database.csv"));
编辑:抱歉忘了提到我需要使用 Scanner 包,因为在下一行中我使用了分隔符。
Scanner scanner = new Scanner(new File("database.csv"));
scanner.useDelimiter(",|\r|\n");
我也在 IntelliJIDEA 工作
所以这是完整的代码
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.*;
public class City
{
public String name; // The name of the city
public String cont; // The continent of the city
public int relTime; // Time relative to Hobart (eg. -14 for New York)
public boolean dst; // Does the city use DST?
public boolean valid; // Does the city exist?
Date currDate;
City(){}; // Default constructor
City(String name, String cont, int relTime)
{
this.name = name;
this.cont = cont;
this.relTime = relTime;
valid = verify();
if(valid)
{
currDate = new Date(System.currentTimeMillis() + (3600000 * relTime));
}
}
City(String name, String cont, int relTime, int dstStartDay, int dstEndDay)
{
this.name = name;
this.cont = cont;
this.relTime = relTime;
valid = verify();
if(valid)
{
currDate = new Date(System.currentTimeMillis() + (3600000 * relTime));
// Is DST in effect?
if(currDate.after(new Date(currDate.getYear(), 3, dstStartDay, 2, 0)) &&
currDate.before(new Date(currDate.getYear(), 11, dstEndDay, 2, 0)))
{
// It is... so
relTime--;
}
}
}
private boolean verify()
{
valid = false;
try
{
Scanner scanner = new Scanner(new File("\\src\\database.csv"));
scanner.useDelimiter(",|\r|\n");
while(scanner.hasNext())
{
String curr = scanner.next();
String next = new String();
if(scanner.hasNext())
next = scanner.next();
if(curr.contains(cont) && next.contains(name))
return true;
}
scanner.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
return false;
}
}