【发布时间】:2014-03-05 15:46:00
【问题描述】:
我似乎在读取文件时遇到问题,它与源文件位于同一目录中。我正在使用日食。我想知道为什么它找不到文件。
无法读取文件
java.io.FileNotFoundException: Simulation.Configuration(系统找不到指定的文件)
在 java.io.FileInputStream.open(Native Method)
在 java.io.FileInputStream.(Unknown Source)
在 java.io.FileInputStream.(Unknown Source)
在 java.io.FileReader.(Unknown Source)
在 hw1.Simulator.main(Simulator.java:24)
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Simulates the interaction between objects
*/
public class Simulator {
public static void main(String[] args) {
ArrayList<Geotask> geotasks = new ArrayList<Geotask>();
int dimensionX = 0;
int dimensionY = 0;
int numberOfMobileObjects = 0;
MobileObject[] MoblieObjects = new MobileObject[numberOfMobileObjects];
int durationOfSimulation = 0;
try {
Scanner in = new Scanner( new FileReader("Simulation.Configuration"));
while (in.hasNext()) {
String simulation = in.next();
switch (simulation) {
case ("dimensionX:"):
dimensionX = in.nextInt();
break;
case ("dimensionY:"):
dimensionY = in.nextInt();
break;
case ("numberOfMoileObjects:"):
numberOfMobileObjects = in.nextInt();
break;
case ("WarningGeotask:"):
Geotask wTask = new WarningGeotask(in.nextInt(),
in.nextInt());
geotasks.add(wTask);
break;
case ("CounterGeotask:"):
Geotask cTask = new CounterGeotask(in.nextInt(),
in.nextInt());
geotasks.add(cTask);
break;
case ("PopulationMonitoringGeotask:"):
Geotask pTask = new PopulationMonitoringGeotask(
in.nextInt(), in.nextInt(), in.nextInt());
geotasks.add(pTask);
break;
case ("durationOfsimulation:"):
durationOfSimulation = in.nextInt();
break;
}
}
in.close();
System.out.println("unable to close configuration file!!!");
Ground ground = new Ground(dimensionX, dimensionY);
MobileObject mobile = null;
for (Geotask tsk : geotasks) {
ground.addGeotask(tsk);
}
for (int i = 0; i < numberOfMobileObjects; i++) {
mobile = new MobileObject(i, i % dimensionX, i % dimensionY, 1,
i % 8, ground);
MoblieObjects[i] = mobile;
for (Geotask tsk : geotasks) {
if (mobile.getCurrentX() == tsk.getX()
&& mobile.getCurrentY() == tsk.getY()) {
tsk.moveIn(mobile);
}
}
}
for (int i = 0; i < durationOfSimulation; i++) {
for (MobileObject obj : MoblieObjects) {
obj.move();
System.out.println("" + obj.getID() + " ( "
+ obj.getCurrentX() + ", " + obj.getCurrentY()
+ " )");
}
}
for (Geotask tsk : geotasks) {
tsk.printType();
}
} catch (FileNotFoundException e) {
System.out.println("Could not read the file");
}catch(NullPointerException e)
{
System.out.println("Null point exception");
}catch(IllegalArgumentException e)
{
System.out.println("Illegal Argument Exception");
}catch(IllegalStateException e)
{
System.out.println("Illegal State Exception");
}
}
}
【问题讨论】:
-
为什么您认为该文件需要与您的课程的源代码位于同一文件夹中?
-
你在什么环境下运行你的程序?日食?
-
它与我的来源在同一个文件夹中。爪哇
标签: java