如何读取和写入文件的文本

时间:2016-03-04 05:35:46

标签: java android

我想编写一个程序,它获取3个文本框值和1个全局变量保存在文本文档中,但是我想要附加到文本文档,而不是每次都重写它!这是我到目前为止所拥有的;

public class Proceed extends Activity {

    // Defining edittext variables and where they will be used
    EditText edittext;
    EditText edittext1;
    EditText edittext2;

    // Getting global variable to get price data from past activity
    String PriceResult = Main2Activity.GSTFinal;

    // Defining textview to show data
    TextView tvResult2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proceed);

        // assigning variables from their respective EditText and textview box
        edittext = (EditText) findViewById(R.id.Name);
        edittext1 = (EditText) findViewById(R.id.Phone);
        edittext2 = (EditText) findViewById(R.id.Address);
        tvResult2 = (TextView) findViewById(R.id.tvResult2);
        // Assigning tvresult what string to show
        tvResult2.setText(PriceResult + "");
        //
    }

    public void save(View view) {
        String n1 = edittext.getText().toString();
        String n2 = edittext1.getText().toString();
        String n3 = edittext2.getText().toString();
        String n4 = tvResult2.getText().toString();
        String Writetotxtfile = n1 + "," + n2 + "," + n3 + "," + n4;
        String filename = "myfile";
        FileOutputStream outputStream;

        try {
            outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
            outputStream.write(Writetotxtfile.getBytes());
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

3 个答案:

答案 0 :(得分:1)

对于阅读文件

public String readFromFile() {
            File sdcard = Environment.getExternalStorageDirectory();
            File file = new File(sdcard,".YOURAPP/myFile.txt");
            StringBuilder text = new StringBuilder();
            try {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;

                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }
                br.close();
            }
            catch (IOException e) {
                //You'll need to add proper error handling here
            }
            return text.toString();
        }

对于写文件

 public void writeFile(String data){
        try {
            File logFile = new File(Environment.getExternalStorageDirectory().toString(), ".YOURAPP/myFile.txt");
            File dirFile = new File(Environment.getExternalStorageDirectory().toString(), ".YOURAPP");
            if(!logFile.exists()) {
                dirFile.mkdir();
                logFile.createNewFile();
            }
            BufferedWriter output = new BufferedWriter(new FileWriter(logFile));
            output.write(data);
            output.close();
        }catch (Exception e){
            System.out.print("Exception e"+e);
        }
    }

对于删除文件

 public void deleteFile(){

        File logFile = new File(Environment.getExternalStorageDirectory().toString(), ".YOURAPP/myFile.txt");
        File dirFile = new File(Environment.getExternalStorageDirectory().toString(), ".YOURAPP");
        if(logFile.exists()) {
            logFile.delete();
            dirFile.delete();

        }
    }

添加对清单的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

而不是

outputStream = openFileOutput(filename, Context.MODE_PRIVATE);

使用

outputStream = new FileOutputStream(filename, true);

这将确保您附加到文件,而不是截断它。

答案 2 :(得分:0)

您要做的是

  

追加

FileOutputStream有一个Constructor用于追加。

  

创建一个文件输出流以写入由。表示的文件   指定的File对象。

FileOutputStream(File file, boolean append)

因此在实例化FileOutputStream时使用该构造函数。

相关问题