Java FileReader 找不到文件



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

无法读取文件
java.io.FileNotFoundException: Simulation.Configuration (系统找不到指定的文件)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(来源不明)
at 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");
    }

}

}

使用

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

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

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

最新更新