如何在Android应用程序中更新txt文件?
我是 Java Android 开发的初学者。我使用的是 Eclipse SDK 3.6.1 版本。我正在尝试创建一个 txt 文件并写入日期/时间和字符串。但我无法更新 txt 文件,我只看到一行。有我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logfile);
working();
viewing ();
}
public void working() {
// try to write the content
try {
// open myfilename.txt for writing
FileOutputStream out =
openFileOutput("file.txt",Context.MODE_APPEND);
// write the contents on mySettings to the file
String time = DateFormat.getDateTimeInstance().format(new Date());
String abs = "action";
String mySettings = time+" -- "+abs+"\n";
out.write(mySettings.getBytes());
// close the file
out.close();
} catch (java.io.IOException e) {
//do something if an IOException occurs.
e.printStackTrace();
}
}
public void viewing (){
TextView rodyk = (TextView)findViewById(R.id.textas);
try {
// open the file for reading
InputStream instream = openFileInput("file.txt");
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
// read every line of the file into the line-variable, on
// line at the time
while (( line = buffreader.readLine()) != null) {
// do something with the settings from the file
rodyk.setText(line);
}
}
// close the file again
instream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我使用Context.MODE_APPEND,但仍然无法更新txt文件。我只看到一排。
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying to create a txt file and write a date/time and string. But I can't update txt file, I see only one row. There is my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logfile);
working();
viewing ();
}
public void working() {
// try to write the content
try {
// open myfilename.txt for writing
FileOutputStream out =
openFileOutput("file.txt",Context.MODE_APPEND);
// write the contents on mySettings to the file
String time = DateFormat.getDateTimeInstance().format(new Date());
String abs = "action";
String mySettings = time+" -- "+abs+"\n";
out.write(mySettings.getBytes());
// close the file
out.close();
} catch (java.io.IOException e) {
//do something if an IOException occurs.
e.printStackTrace();
}
}
public void viewing (){
TextView rodyk = (TextView)findViewById(R.id.textas);
try {
// open the file for reading
InputStream instream = openFileInput("file.txt");
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
// read every line of the file into the line-variable, on
// line at the time
while (( line = buffreader.readLine()) != null) {
// do something with the settings from the file
rodyk.setText(line);
}
}
// close the file again
instream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I use Context.MODE_APPEND, but still can't update txt file. I see only one row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此代码不会将您读取的行附加到该 TextView 中已有的文本中。它将把文本更改为您最近阅读的行。因此,您应该只能在 TextView 中看到 file.txt 最后一行的内容。
此外,每当您重新安装应用程序时(因为您在创建和测试期间可能会做很多事情),您的 file.txt 可能会被新文件替换。
This code won't append the line you read to the text already in that TextView. It will change the text to the most recent line you read. Hence you should only see the contents of the last line of file.txt in your TextView.
Also whenever you reinstall the application (as you may do a lot during its creation and testing) your file.txt may be getting replaced by a new file.
您可以在没有上下文的情况下编写,如下所示:
它有效。
You can write without the Context Like this:
It works.
不要使用文本视图;在代码中使用编辑文本。
Don't use text view; use Edit Text in your code.