我试图能够在资产文件夹的文件中搜索字符串。getassets().open 说它未定义。我在某处读到这种方式有效。这是我的代码:
import java.io.File;
import java.util.Scanner;
import android.content.Context;
import android.util.Log;
public class Search {
public static void LoadStuff(String Name) {
Scanner reader = null;
try {
reader = new Scanner(new File(getAssets().open("myFile.txt")));
} catch (Exception e) {
Log.d("damn", "FAIL");
}
if(reader != null)
Load(reader);
}
private static void Load(Scanner reader) {
while (reader.hasNext()) {
String result = reader.next();
if (result.equals("water")) {
while (result != "KEYEND") {
int index = reader.nextInt();
Log.d("Index", String.valueOf(index));
}
}
}
reader.close();
}
}