在android中将多个字符串写入文件

时间:2014-12-08 22:30:51

标签: java android eclipse

我希望使用android将多个字符串写入文件以进行存储。是否可以使用.getText将所有字符串添加到toString,如果是这样的话。我期待创建一些编辑文本字段并获取这些字段中包含的信息并将它们写入文件,然后在另一个活动中读取该文件。任何帮助将不胜感激。

如果有任何帮助,这是我的代码:

package app.project.newapp;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class AddActivity extends Activity {

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

    public void saveClick(String filename, EditText v){

        EditText name = (EditText) findViewById(R.id.namet);
        EditText tel = (EditText) findViewById(R.id.emerg_tel);
        EditText cond = (EditText) findViewById(R.id.conditions);
        EditText other = (EditText) findViewById(R.id.other);

        String userDetails = "Hello_File";
        String string = "";

        FileOutputStream fos = null;
        try {
            fos = openFileOutput(userDetails, Context.MODE_PRIVATE);
        } catch (FileNotFoundException e){
            e.printStackTrace();
        }
        try {
            string=name.getText().toString();
            string=tel.getText().toString();
            string=cond.getText().toString();
            string=other.getText().toString();
            fos.write(string.getBytes());
        } catch (IOException e){
            e.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e){
            e.printStackTrace();
        }

            try {
                BufferedReader inputReader = new BufferedReader(new InputStreamReader(
                        openFileInput("hello_file")));
                String inputString;
                StringBuffer stringBuffer = new StringBuffer();
                while ((inputString = inputReader.readLine()) != null) {
                    stringBuffer.append(inputString + "\n");
                }

                } catch(IOException e) {
                    e.printStackTrace();
                }//end catch

        }

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

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

在第二次尝试中尝试使用saveClick方法:

try {
    string=name.getText().toString() + "\n"
        + tel.getText().toString() + "\n"
        + cond.getText().toString() + "\n"
        + other.getText().toString();
    fos.write(string.getBytes());
} catch(IOException e) {
    e.printStackTrace();
}