我有一个在eclipse上运行的程序(程序从文本文件中读取)运行正常。但是,当它被编译并在命令行上运行时,它无法找到我正在阅读的文本文件。能够使用java读取命令文件上的文件
private void openfile()
{
try
{
file = new Scanner(new File("file.txt"));
}
catch(Exception e)
{
System.out.println("i hate command prompt");
}
private void readfile()
{
while(file.hasNext())
{
map_name = file.nextLine().split("\\s+");
}
}
private void closefile()
{
file.close();
}
任何人都可以解释我怎么能避免由File文件指定该
该文本文件与您的班级在同一目录中吗? –