0
votes

I seem to be having a problem reading the file and it is in the same directory as the source. I am using eclipse. I was wondering why it can't find the file.

Could not read the file
java.io.FileNotFoundException: Simulation.Configuration (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileReader.(Unknown Source)
at 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
Why do you think the file needs to be in the same folder as the source code for your class?Sotirios Delimanolis
What environment are you running your program in? Eclipse?Weibo Li
it is in the same Folder as my source. Javadrhunn

2 Answers

1
votes

Use

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

You can return the directory of the program at run-time, which makes it OS independent.

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