2 回答

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以通過(guò)僅獲取InputStream提供者AssetManager.open()并將其傳遞給較新的RDFParserAPI來(lái)簡(jiǎn)化您的代碼,例如
InputStream inputStream = AssetManager.open("sample_3.ttl");
Model model = ModelFactory.createDefaultModel();
RDFParser.create().source(inputStream).lang(Lang.TTL).parse(model);
這避免了不必要的讀入和寫回文件。
然而,它確實(shí)需要使用該jena-arq庫(kù)以及使用最新版本的 Jena(您似乎正在使用 Jena 2 的某些變體,您將需要 Jena 3.7 或更高版本才能使上述內(nèi)容正常工作)

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
我找到了答案。問(wèn)題是 android studio 中的資產(chǎn)文件無(wú)法在 android studio 中讀取。它們必須轉(zhuǎn)換為 FileOutputStream,即使在使用 .ttl 文件并在烏龜中讀取它們時(shí)也是如此。這是代碼的示例:
String filePath = context.getFilesDir() + File.separator + "my_turtle.ttl";
File destinationFile = new File(filePath);
FileOutputStream outputStream = new FileOutputStream(destinationFile);
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("sample_3.ttl");
byte[] buffer = new byte[1024];
int length = 0;
while((length = inputStream.read(buffer)) != -1){
outputStream.write(buffer,0,length);
}
inputStream.close();
outputStream.close();
Model model = null;
model = FileManager.get().loadModel(filePath,"TTL");
Query query = QueryFactory.create(stringQuery);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();
while(results.hasNext()) {
QuerySolution soln = results.nextSolution();
Literal answerLiteral = soln.getLiteral("abstract");
answer = answerLiteral.toString();
System.out.println(answer);
}
if(!answer.equals("")){
this.answer = answer;
return answer;
}
else{
return "I could not find an answer";
}
添加回答
舉報(bào)