0
我需要将文本行写入文件,然后逐行读取它们。 所以我用这些代码写入文件:将文本文件的行读取到字符串数组中
String string = "Hello world55555555555!" + "\r\n";
FileOutputStream outputStream;
try {
outputStream = getActivity().openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return true;
它似乎正常工作。但是当我阅读时会出错!我试图通过这些代码读取它:
FileInputStream inputStream = null;
BufferedReader reader;
String[] text = new String[0];
List <String> words = new ArrayList <String>();
String line;
try {
inputStream = getActivity().openFileInput(filename);
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) {
words.add(line);
}
text = new String[words.size()];
text = words.toArray(text);
} catch (IOException e) {
//You'll need to add proper error handling here
}
tv.setText(text[0]);
Toast.makeText(getActivity(), text[1],
Toast.LENGTH_SHORT).show();
}
有什么意见!请!
任何时候你“出现错误” [请告诉我们错误信息](https://secure.phabricator.com/book/phabflavor/article/please_please_please/)。 –