[英]Android read / write text files
The question is very simple: I have those who methods in my class. 问题很简单:我的班上有那些方法的人。 This one saves the data in a text file: 这将数据保存在一个文本文件中:
//Store the data in a text file
public void saveScores(String testo, TextView text) {
try {
FileOutputStream fileout = getActivity().openFileOutput("flagQuiz.txt", getActivity().MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(testo);
outputWriter.close();
text.setText(testo);
} catch (Exception e) {
e.printStackTrace();
}
}
This method instead reads the data from a text file. 相反,此方法从文本文件读取数据。
//Load data from text file
public void loadScores() {
try {
FileInputStream fileIn = getActivity().openFileInput("flagQuiz.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[100];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
} catch (Exception e) {
e.printStackTrace();
}
}
From what I can gauge the first method works pretty well because it doens't raise exceptions and the text.setText(testo);
据我所知,第一种方法效果很好,因为它不会引发异常,而且text.setText(testo);
不会引发异常text.setText(testo);
works fine. 工作正常。
PROBLEM 问题
I cannot read the text file when the app opens (onCreate). 应用程序打开(onCreate)时,我无法读取文本文件。
As you can see here there is a null pointer exception, which means I guess that the file is not saved or I am typing a wrong path for the InputStream maybe. 如您所见,这里有一个空指针异常,这意味着我想文件没有保存,或者我可能为InputStream输入了错误的路径。
Any suggestion about this? 有什么建议吗? I am going to write this very little text file on the internal storage. 我将在内部存储器上写这个很小的文本文件。
The complete log can be found here: link . 完整的日志可以在这里找到: link 。
below code will read data from .txt file on resume. 下面的代码将从简历上的.txt文件读取数据。
public class MainActivity extends Activity {
private final static String NOTES = "notes.txt";
private EditText editText;
private Button Btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editor);
Btn = (Button) findViewById(R.id.close);
Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
public void onResume(){ //this method will read data from .txt file
super.onResume();
try {
InputStream in = openFileInput(NOTES);
if (in != null) {
InputStreamReader tmp = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(tmp);
String str;
StringBuffer buf = new StringBuffer();
while ((str = reader.readLine()) != null) {
buf.append(str + "\n");
}
in.close();
editText.setText(buf.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Throwable t) {
Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
}
}
An example of use of OpenFileInput
: 使用OpenFileInput
的示例:
FileInputStream in = openFileInput("filename.txt");
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.