我有下面的代碼,我知道它在Java中工作。我現在正在嘗試做和Android應用程序中的等價物。操作非常簡單:我需要在類中使用方法(在同一個工作區中),我首先需要從我存儲在/res/raw中的文本文件中刪除這些行。android - 從文本文件逐行讀取
基本上我需要調用駐留在Android Activity類的Java類中的此方法。這怎麼可能?
活動課片段(CaptureActivity.java):
setContentView(R.layout.orientation);
try {
SentenceGenerator sentenceGenerator = new SentenceGenerator();
ArrayList<String> firstState = stateGenerator.getState(resultOutput, result, result);
String viewPoint = firstState.get(0);
String object = firstState.get(1);
String subject = firstState.get(2);
Thing subjectInfo = new Thing();
if(resultOutput.equals("Sliema")){
Sliema sliema = new Sliema();
subjectInfo = sliema.getInfo(viewPoint, object, subject);
}
try {
String s = sentenceGenerator.generateSentence("start.txt", "middle.txt", "end.txt", resultOutput, viewPoint, object, subject, subjectInfo.type, subjectInfo.name, subjectInfo.properties, false);
TextView instructionTextView = (TextView) findViewById(R.id.instructionTextView);
instructionTextView.setText(s);
} catch (Exception e) {
}
} catch (ClassNotFoundException e) {
}
的Java類片段(SentenceGenerator.java):
public static ArrayList<String> getLines(String filename) throws Exception {
ArrayList<String> lines = new ArrayList<String>();
FileInputStream fstream = new FileInputStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
lines.add(strLine);
}
in.close();
return lines;
}
感謝您的幫助提前!
顯然文本文件沒有正確訪問。我可以使用此方法訪問存儲在/ res/raw中的.txt文件嗎? – k0rtin 2013-04-30 23:52:30