此数组是代码:的Android阅读文本文件(.txt)转换成字符串
void CreateWordList()
{
Toast.makeText(getBaseContext(), "Creating Word List...", Toast.LENGTH_SHORT).show();
InputStream is = getResources().openRawResource(R.raw.pass);
BufferedReader lines = null;
try {
lines = new BufferedReader(new InputStreamReader(is, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ArrayList<String> list = new ArrayList<String>();
String line = null;
try {
while((line = lines.readLine()) !=null)list.add(line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wordlist = (String[]) list.toArray();
if (wordlist[1] == null)
{
Toast.makeText(getBaseContext(), "ERROR: Word List = null", Toast.LENGTH_SHORT).show();
}
}
我在“line = lines.readLine();
”,上面写着“型IOException的未处理的异常”,所以我周围有一个错误它与try/catch。
而且我还有一个错误在“BufferedReader lines = new BufferedReader(new InputStreamReader(is, "UTF-8"));
”,上面写着“未处理的异常类型UnsupportedEncodingException”,所以我用的try/catch包围它。
现在,当我运行应用程序崩溃...
我在做什么错了?
如何读取文本文件并将每行添加到字符串数组?
PS:我已经搜查,发现其他类似的问题和答案,但没有帮助我...
lines.readLine()不会返回null,如果文件完成,它会返回-1 –
混淆 - 我看不到你的代码中有一个try/catch。我怀疑编译。请发布您的真实代码。 –
我加了try/catch对不起,我会编辑问题 – user2635745