【发布时间】:2014-06-24 22:47:01
【问题描述】:
我确信我错过了一些非常愚蠢的东西,但由于某种原因,即使我的 input.txt 文件位于包中,我也会收到“未处理的异常类型 FileNotFoundException”错误。任何帮助都会很棒!
扫描仪代码:
File file = new File("input.txt");
Scanner inputFile = new Scanner(file);
Company c = new Company();
String input = inputFile.nextLine();
完整的类代码:
package SimpleJavaAssignment;
import java.io.File;
import java.util.*;
public class Company
{
ArrayList<Department> deptList = new ArrayList<Department>();
public Department checkDepartment(String name)
{
for(Department dept: deptList)
{
if(dept.getName().equals(name))
{
return dept;
}
}
Department d = new Department(name);
deptList.add(d);
return d;
}
public static void main(String[] args)
{
System.out.println ("This program will compile and display the stored employee data.");
File file = new File("input.txt");
Scanner inputFile = new Scanner(file);
Company c = new Company();
String input = inputFile.nextLine();
while(inputFile.hasNextLine() && input.length() != 0)
{
String[] inputArray = input.split(" ");
Department d = c.checkDepartment(inputArray[3]);
d.newEmployee(Integer.parseInt(inputArray[2]), inputArray[0] + " " + inputArray[1], d);
input = inputFile.nextLine();
}
System.out.printf("%-15s %-15s %-15s %-15s %n", "DEPARTMENT", "EMPLOYEE NAME", "EMPLOYEE AGE",
"IS THE AGE A PRIME");
for(Department dept:c.deptList)
{
ArrayList<Employee> empList = dept.getEmployees();
for(Employee emp: empList)
{
emp.printInfo();
}
}
}
}
【问题讨论】:
-
把它放在你的 src 文件夹之外
-
如果我把它放在源文件夹之外,我是否不必在整个路径中编码,当我共享程序时会改变?
-
凯文,这可能会有所帮助:stackoverflow.com/questions/16313260/…。
-
您指的是没有路径的裸文件名,因此程序将在 当前工作目录 中查找 不一定 相同作为jar文件所在的目录。这取决于你如何调用你的 java 程序。