嗨朋友们,我是语义网的新手,遇到了一些困难。我已经向 DBPedia 查询并获得了 Paris 的属性/值对。我想要做的是将查询结果写入 rdf 文件。下面是我的代码:
import java.io.BufferedWriter;
import java.io.FileWriter;
import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.ResultSetFactory;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.query.ResultSetRewindable;
public class ParisTest {
public static void main(String[] args) {
ParameterizedSparqlString qs = new ParameterizedSparqlString(""
+ " SElECT ?property ?value where {{ <http://dbpedia.org/resource/Paris> ?property ?value } UNION "
+ " {?property ?value <http://dbpedia.org/resource/Paris>}}");
QueryExecution exec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", qs.asQuery());
ResultSetRewindable result = ResultSetFactory.makeRewindable(exec.execSelect());
try (BufferedWriter bw = new BufferedWriter(new FileWriter("QueryResults.rdf"))) {
while (result.hasNext()) {
System.out.println((result.next().get("property").toString()));
System.out.println((result.next().get("value").toString()));
System.out.println(ResultSetFormatter.asText(result));
bw.write((result.next().get("property").toString()));
bw.write((result.next().get("value").toString()));
}
bw.flush();
bw.close();
} catch (Exception e) {
}
}
}
它在控制台中完美打印结果,但不会将其写入文件。我对 BufferedWriter 和 FileWriter 将创建 .rdf 文件的理解是否正确?如果可能的话,你能帮我解决这个问题吗?