使用Java/Jena API打开文件。

huangapple go评论59阅读模式
英文:

To open file in Java/jena API

问题

Hi我目前正在学习Java和Jena API上的语义网我在尝试打开与我的类位于同一目录中的文件时遇到了错误

我的代码

public class Tuto7 extends Object {

    static final String inputFileName = "vc-db-1.rdf";

    public static void main(String args[]) throws FileNotFoundException {
        // 创建一个空模型
        BasicConfigurator.configure();
        Model model = ModelFactory.createDefaultModel();

        // 使用FileManager查找输入文件
        InputStream in = FileManager.get().open(inputFileName);
        System.out.println(in);
        if (in == null) {
            throw new IllegalArgumentException("文件:" + inputFileName + "未找到");
        }
        System.out.println("打开文件后");
        // 读取RDF/XML文件
        model.read(in, "");

        // 选择所有具有VCARD.FN属性的资源
        ResIterator iter = model.listResourcesWithProperty(VCARD.FN);
        if (iter.hasNext()) {
            System.out.println("数据库中包含以下vcards:");
            while (iter.hasNext()) {
                System.out.println("  " + iter.nextResource()
                    .getRequiredProperty(VCARD.FN)
                    .getString());
            }
        } else {
            System.out.println("数据库中未找到任何vcards");
        }
    }
}

错误信息:`Exception in thread "main" java.lang.IllegalArgumentException: 文件vc-db-1.rdf未找到`

注意该文件与代码位于同一目录中
英文:

Hi I'm currently learning semantic web on java and Jena API.
I have an error to open a file which is in the same directory with my class

My code :

public class Tuto7 extends Object {
static final String inputFileName = "vc-db-1.rdf";
public static void main(String args[]) throws FileNotFoundException {
// create an empty model
BasicConfigurator.configure();
Model model = ModelFactory.createDefaultModel();
// use the FileManager to find the input file
InputStream in = FileManager.get().open(inputFileName);
System.out.println( in );
if ( in == null) {
throw new IllegalArgumentException("File: " + inputFileName + " not found");
}
System.out.println("After open file");
// read the RDF/XML file
model.read( in , "");
// select all the resources with a VCARD.FN property
ResIterator iter = model.listResourcesWithProperty(VCARD.FN);
if (iter.hasNext()) {
System.out.println("The database contains vcards for:");
while (iter.hasNext()) {
System.out.println("  " + iter.nextResource()
.getRequiredProperty(VCARD.FN)
.getString());
}
} else {
System.out.println("No vcards were found in the database");
}
}
}

The error : Exception in thread "main" java.lang.IllegalArgumentException: File: vc-db-1.rdf not found

NB : The file is in the same directory

答案1

得分: 0

已经解决通过

static final String inputFileName = "/Users/macbook/Documents/IntelliJ/projet2020/webSemantique/src/tp1/vc-db-1.rdf";

谢谢

英文:

Already resolved by
<code>
static final String inputFileName ="/Users/macbook/Documents/IntelliJ/projet2020/webSemantique/src/tp1/vc-db-1.rdf";

</code>
Thanks

huangapple
  • 本文由 发表于 2020年10月26日 07:02:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64529694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定