Java FileReader无法找到文件

时间:2014-02-08 02:31:12

标签: java

我似乎在阅读文件时遇到问题,并且它与源文件位于同一目录中。我正在使用eclipse。我想知道它为什么找不到文件。

无法读取文件
java.io.FileNotFoundException:Simulation.Configuration(系统找不到指定的文件)
在java.io.FileInputStream.open(本机方法)
在java.io.FileInputStream。(未知来源)
在java.io.FileInputStream。(未知来源)
在java.io.FileReader。(未知来源)
在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");
    }


}

}

2 个答案:

答案 0 :(得分:1)

使用

new InputStreamReader(Simulator.class.getResourceAsStream("Simulation.Configuration"));

答案 1 :(得分:-1)

您可以在运行时返回程序目录,这样可以使操作系统独立。

System.out.printf("%s", System.getProperty("user.dir"));