简体   繁体   中英

No such file or directory error even the file exists in Java

I am a newbie to Java and I wish to run the library JGibbLDA . I did as required by the document, enter the root directory of JGibbLDA-v.1.0 and input the command:

java -mx512M -cp bin:lib/args4j-2.0.6.jar jgibblda.LDA -est -alpha 0.5 -beta 0.1 -ntopics 100 -niters 1000 -savestep 100 -twords 20 -dfile models/data/data.txt

But an error occurs:

Read Dataset Error: /models/data/data.txt (No such file or directory)
java.io.FileNotFoundException: /models/data/data.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at jgibblda.LDADataset.readDataSet(LDADataset.java:154)
at jgibblda.Model.initNewModel(Model.java:476)
at jgibblda.Estimator.init(Estimator.java:45)
at jgibblda.LDA.main(LDA.java:49)
Fail to read training data!

I don't know why there is a / before the directory in the error log message. But the models/data/data.txt really exist and I can enter models/data at the root directory properly. The related code of the library is as below:

public static LDADataset readDataSet(BufferedReader reader, Dictionary dict){
    try {
        //read number of document
        String line;
        line = reader.readLine();
        int M = Integer.parseInt(line);
        System.out.println("NewM:" + M);

        LDADataset data = new LDADataset(M, dict);
        for (int i = 0; i < M; ++i){
            line = reader.readLine();

            data.setDoc(line, i);
        }

        return data;
    }
    catch (Exception e){
        System.out.println("Read Dataset Error: " + e.getMessage());
        e.printStackTrace();
        return null;
    }
}

I have also tried ../../models/data/data.txt but also failed. Do you know how to solve this problem? Thank you in advance!

In method Model.initNewModel(LDACmdOption option) the filename for the dataset is constructed as

dir + File.separator + dfile

If dir is empty the filename will start from the root directory. To avoid this behavior you can use the option -dir to specify the data directory.

Running it as

java -mx512M -cp bin:lib/args4j-2.0.6.jar jgibblda.LDA -est -alpha 0.5 \
   -beta 0.1 -ntopics 100 -niters 1000 -savestep 100 -twords 20 \
   -dir /home/youruser/project/models/data/ \
   -dfile data.txt

would read the file data.txt from directory /home/youruser/project/models/data/ . All generated files would also be stored in /home/youruser/project/models/data/ .

The program is looking for an absolute path and you are passing a relative path.

The error says:

/models/data/data.txt (No such file or directory)
^
|--- This is the root of your filesystem

Then you must pass the full path, try with full path (for example /home/youruser/project/models/data/data.txt):

java -mx512M -cp bin:lib/args4j-2.0.6.jar jgibblda.LDA -est -alpha 0.5 -beta 0.1 -ntopics 100 -niters 1000 -savestep 100 -twords 20 -dfile FULL_PATH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM