2012-08-26 39 views
2

我已經下載了SSJ庫,一個用於隨機模擬的Java庫。其中一個文件需要打開* .dat文件。java.io.FileNotFoundException,爲什麼?

我試圖運行下載的文件,並且dat文件也存在,但我每次都得到FileNotFoundException。

這裏的源代碼:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.StringTokenizer; 
import umontreal.iro.lecuyer.randvar.ExponentialGen; 
import umontreal.iro.lecuyer.rng.MRG32k3a; 
import umontreal.iro.lecuyer.rng.RandomStream; 
import umontreal.iro.lecuyer.simevents.Event; 
import umontreal.iro.lecuyer.simevents.Sim; 
import umontreal.iro.lecuyer.simprocs.Resource; 
import umontreal.iro.lecuyer.simprocs.SimProcess; 
import umontreal.iro.lecuyer.stat.Tally; 

public final class Jobshop { 
    int nbMachTypes;  // Number of machine types M. 
    int nbTaskTypes;  // Number of task types N. 
    double warmupTime;  // Warmup time T_0. 
    double horizonTime; // Horizon length T. 
    boolean warmupDone; // Becomes true when warmup time is over. 
    Resource[] machType; // The machines groups as resources. 
    Jobshop.TaskType[] taskType; // The task types. 
    RandomStream streamArr = new MRG32k3a(); // Stream for arrivals. 
    BufferedReader input; 

    public Jobshop() throws IOException { readData(); } 

    // Reads data file, and creates machine types and task types. 
    void readData() throws IOException { 
// input = new BufferedReader (new FileReader ("Jobshop.dat")); 
    input = new BufferedReader (new FileReader ("JobShop.dat")); 
    StringTokenizer line = new StringTokenizer (input.readLine()); 
    warmupTime = Double.parseDouble (line.nextToken()); 
    line = new StringTokenizer (input.readLine()); 
    horizonTime = Double.parseDouble (line.nextToken()); 
    line = new StringTokenizer (input.readLine()); 
    nbMachTypes = Integer.parseInt (line.nextToken()); 
    nbTaskTypes = Integer.parseInt (line.nextToken()); 
    machType = new Resource[nbMachTypes]; 
    for (int m=0; m < nbMachTypes; m++) { 
    line = new StringTokenizer (input.readLine()); 
    String name = line.nextToken(); 
    int nb = Integer.parseInt (line.nextToken()); 
    machType[m] = new Resource (nb, name); 
    } 
    taskType = new Jobshop.TaskType[nbTaskTypes]; 
    for (int n=0; n < nbTaskTypes; n++) 
    taskType[n] = new Jobshop.TaskType(); 
    input.close(); 
} 


class TaskType { 
    public String  name;  // Task name. 
    public double  arrivalRate; // Arrival rate. 
    public int  nbOper;  // Number of operations. 
    public Resource[] machOper; // Machines where operations occur. 
    public double[] lengthOper; // Durations of operations. 
    public Tally  statSojourn; // Stats on sojourn times. 

    // Reads data for new task type and creates data structures. 
    TaskType() throws IOException { 
    StringTokenizer line = new StringTokenizer (input.readLine()); 
    statSojourn = new Tally (name = line.nextToken()); 
    arrivalRate = Double.parseDouble (line.nextToken()); 
    nbOper = Integer.parseInt (line.nextToken()); 
    machOper = new Resource[nbOper]; 
    lengthOper = new double[nbOper]; 
    for (int i = 0; i < nbOper; i++) { 
     int p = Integer.parseInt (line.nextToken()); 
     machOper[i] = machType[p-1]; 
     lengthOper[i] = Double.parseDouble (line.nextToken()); 
    } 
    } 

    // Performs the operations of this task (to be called by a process). 
    public void performTask (SimProcess p) { 
    double arrivalTime = Sim.time(); 
    for (int i=0; i < nbOper; i++) { 
     machOper[i].request (1); p.delay (lengthOper[i]); 
     machOper[i].release (1); 
    } 
    if (warmupDone) statSojourn.add (Sim.time() - arrivalTime); 
    } 
} 

public class Task extends SimProcess { 
    Jobshop.TaskType type; 

    Task (Jobshop.TaskType type) { this.type = type; } 

    public void actions() { 
    // First schedules next task of this type, then executes task. 
    new Jobshop.Task (type).schedule (ExponentialGen.nextDouble 
      (streamArr, type.arrivalRate)); 
    type.performTask (this); 
    } 
} 

Event endWarmup = new Event() { 
    public void actions() { 
    for (int m=0; m < nbMachTypes; m++) 
     machType[m].setStatCollecting (true); 
    warmupDone = true; 
    } 
}; 

Event endOfSim = new Event() { 
    @Override 
    public void actions() { Sim.stop(); } 
}; 

public void simulateOneRun() { 
    SimProcess.init(); 
    endOfSim.schedule (horizonTime); 
    endWarmup.schedule (warmupTime); 
    warmupDone = false; 
    for (int n = 0; n < nbTaskTypes; n++) { 
    new Jobshop.Task (taskType[n]).schedule (ExponentialGen.nextDouble 
     (streamArr, taskType[n].arrivalRate)); 
    } 
    Sim.start(); 
} 

public void printReportOneRun() { 
    for (int m=0; m < nbMachTypes; m++) 
    System.out.println (machType[m].report()); 
    for (int n=0; n < nbTaskTypes; n++) 
    System.out.println (taskType[n].statSojourn.report()); 
} 

static public void main (String[] args) throws IOException { 
    Jobshop shop = new Jobshop(); 
    shop.simulateOneRun(); 
    shop.printReportOneRun(); 
} 
} 

和這裏的輸出:

Exception in thread "main" java.io.FileNotFoundException: JobShop.dat (O sistema não conseguiu localizar o ficheiro especificado) 
at java.io.FileInputStream.open(Native Method) 
at java.io.FileInputStream.<init>(FileInputStream.java:138) 
at java.io.FileInputStream.<init>(FileInputStream.java:97) 
at java.io.FileReader.<init>(FileReader.java:58) 
at Jobshop.readData(Jobshop.java:31) 
at Jobshop.<init>(Jobshop.java:26) 
at Jobshop.main(Jobshop.java:133) 
Java Result: 1 

就如何解決它的任何線索?

在此先感謝。

+1

我會說,這是不好的做法,實際上是在構造函數中運行代碼。您應該使用構造函數來初始化數據。 – maba

+1

你有沒有嘗試給該.DAT文件的絕對路徑,看看它是否仍然會拋出一個錯誤? – Vikdor

+0

通過設置它的絕對路徑很好,謝謝,不知道我是如何忘記嘗試! –

回答

3

文件被引用的方式,它期望從您運行應用程序的位置找到該文件。它似乎無法在那裏找到它。

+0

這個 - 確保工作目錄包含這個文件。 – Dan

+0

該文件位於/ src/dir中。我想這是工作目錄,不是嗎? –

+1

如果您正在從Eclipse等IDE中運行,請將該文件放在src之外的主項目目錄中。如果你從命令行運行它,然後把它放在你運行java命令的目錄中。 –

2

確保您對於指定當前工作目錄(其中運行java命令的目錄)的路徑.dat文件

0

你的路徑可能是錯誤的:

input = new BufferedReader (new FileReader ("JobShop.dat")); 
0

由於我正在使用NetBeans IDE運行項目,該文件應該添加到NetBeansProjects目錄中的主項目目錄中。

當我創造它的源代碼包裏面,我不得不添加打開文件等時,它的路徑:

input = new BufferedReader (new FileReader ("src/JobShop.dat")); 
相關問題