这是代码:
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:我已经搜索并找到了其他类似的问题和答案,但这对我没有帮助......