我有以下代码,我知道它适用于Java。我现在正在尝试在Android应用程序中进行等效操作。操作非常简单:我需要在类中(在同一个工作区中)使用一个方法,我首先需要从文本文件中取出行,我存储在 / res / raw 中
基本上我需要从Android Activity Class中调用驻留在Java类中的这个方法。这怎么可能?
Activity Class片段(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 Class片段(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;
}
提前感谢您的帮助!
答案 0 :(得分:1)
更改以下代码:
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
lines.add(strLine);
}
到
Scanner br = new Scanner(new InputStreamReader(in));
while (br.hasNext()) {
String strLine = br.nextLine();
lines.add(strLine);
}
那应该有用。 希望这有帮助!