如何在Android中的文件中追加数据 [英] How to append data in a file in android

查看:236
本文介绍了如何在Android中的文件中追加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何将数据追加到Android中已经有数据的文件中?

Can anyone tell me how to append data to a file that already has data in Android?

我写了一些代码,但是没有用.这是我的活动:

I wrote some code but it's not working. This is my activity:

package updatefile.developer.com.updatefiledemo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.nio.Buffer;

public class MainActivity extends ActionBarActivity {
    Button create,update;
    String data = "This is a sample";
    File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        create = (Button)findViewById(R.id.btn_create);
        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File folder = new File("/sdcard/demo");
                folder.mkdirs();
                file = new File("/sdcard/demo/demotext.txt");
                try {
                    FileOutputStream fileinput = new FileOutputStream(file);
                    PrintStream printstream = new PrintStream(fileinput);
                    printstream.print(data+"\n");
                    fileinput.close();
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }
        });

        update = (Button)findViewById(R.id.btn_update);
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (Exception e) {
                        Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                    }
                }

                try {
                    OutputStreamWriter file_writer = new OutputStreamWriter(new FileOutputStream(file,true));
                    BufferedWriter buffered_writer = new BufferedWriter(file_writer);
                    buffered_writer.write("This is a appended text");
                    buffered_writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

推荐答案

FileOutputStream fileinput = new FileOutputStream(file,true);

这是一个简单的错误,因为未将 append 标志设置为 true .只需将其设置为true.

It was a simple mistake, because of not setting the append flag to true. Just set it to true.

默认行为是覆盖文件内容:)

The default behavior is to overwrite the file content :)

这篇关于如何在Android中的文件中追加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆