我想知道如何逐个字符地从资源中读取文本文件。
例如,如果我有这个文件“text.txt”并且里面有“12345”,我想逐一阅读所有数字。
我已经找到了这个,但我找不到任何解决方案。
感谢。
答案 0 :(得分:0)
使用getAssets().open("name.txt")
检索InputStream
上的assets/name.txt
,然后根据需要阅读。
答案 1 :(得分:0)
感谢CommonsWare的回答:)除了我回复Eric的链接之外,我确实添加了你的代码,结果就是结果(完全正常):
AssetManager manager = getContext().getAssets();
InputStream input = null;
try {
input = manager.open("test.txt");
} catch (IOException e1) {
Log.d("ERROR DETECTED", "ERROR WHILE TRYING TO OPEN FILE");
}
try {
char current;
while (input.available() > 0) {
current = (char) input.read();
Log.d("caracter", ""+current);
}
} catch (IOException e) {
e.printStackTrace();
}
感谢您的帮助:)
编辑:下一个代码将读取所有文件行,而上述不是:
AssetManager manager = getContext().getAssets();
InputStream input = null;
InputStreamReader in = null;
try {
input = manager.open("teste.txt");
in = new InputStreamReader(input);
} catch (IOException e1) {
Log.d("ERROR DETECTED", "ERROR WHILE TRYING TO OPEN FILE");
}
try {
char current;
while (in.ready()) {
current = (char) in.read();
Log.d("caracter", ""+current);
}
} catch (IOException e) {
e.printStackTrace();
}