如何在Android应用程序中更新txt文件?

发布于 2024-10-17 14:53:42 字数 1942 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城半夏 2024-10-24 14:53:42
 rodyk.setText(line);

此代码不会将您读取的行附加到该 TextView 中已有的文本中。它将把文本更改为您最近阅读的行。因此,您应该只能在 TextView 中看到 file.txt 最后一行的内容。

此外,每当您重新安装应用程序时(因为您在创建和测试期间可能会做很多事情),您的 file.txt 可能会被新文件替换。

 rodyk.setText(line);

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.

哭泣的笑容 2024-10-24 14:53:42
FileOutputStream out =
    openFileOutput("file.txt",Context.MODE_APPEND);

您可以在没有上下文的情况下编写,如下所示:

FileOutputStream out =
    openFileOutput("file.txt",MODE_APPEND);

它有效。

FileOutputStream out =
    openFileOutput("file.txt",Context.MODE_APPEND);

You can write without the Context Like this:

FileOutputStream out =
    openFileOutput("file.txt",MODE_APPEND);

It works.

再可℃爱ぅ一点好了 2024-10-24 14:53:42

不要使用文本视图;在代码中使用编辑文本。

EditText et=(EditText)findviewbyId(R.id.et);
...
...
...
et.setText(line);

Don't use text view; use Edit Text in your code.

EditText et=(EditText)findviewbyId(R.id.et);
...
...
...
et.setText(line);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文