将应用数据保存到文本文件

时间:2014-05-21 08:52:01

标签: java android eclipse

您好我已经在eclipse中创建了一个Android应用程序,我希望将应用程序中的数据保存到我已安装应用程序的平板电脑上存储的文本文件中。我尝试了几种不同的方法来做到这一点但到目前为止都没有成功。到目前为止我能够保存应用程序,但我不知道它保存到哪里,大多数网站显示保存到外部和内部但不解释这些文件保存到哪里,或我如何检索它们。有没有人知道任何有用的网站,这将有助于此。 该应用程序包含三个需要用户输入的表单,然后希望用户能够保存这些表单,以便他们可以通过电子邮件发送给他们。 这是我的代码之一的副本

package com.ifmltd.ifmapp;

public class Checklist extends Activity {

private final static String STORETEXT = "storetext.txt";
private EditText txtEditor;
private TextView txtViewer;
private CheckBox cBoxClick;
private Spinner spinSelect;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.checklist);
    Home();
    Save();

    txtViewer = (TextView) findViewById(R.id.txtTitle);
    txtViewer = (TextView) findViewById(R.id.txtVehicle);
    txtViewer = (TextView) findViewById(R.id.txtInterior);
    txtViewer = (TextView) findViewById(R.id.txtExterior);
    txtViewer = (TextView) findViewById(R.id.txtEngine);
    txtViewer = (TextView) findViewById(R.id.txtOther);
    txtViewer = (TextView) findViewById(R.id.txtDefects);
    txtViewer = (TextView) findViewById(R.id.txtName);
    txtViewer = (TextView) findViewById(R.id.txtDefectInfo);
    txtViewer = (TextView) findViewById(R.id.txtAddCom);
    txtViewer = (TextView) findViewById(R.id.txtFaults);
    txtEditor = (EditText) findViewById(R.id.etxtAddCom);
    txtEditor = (EditText) findViewById(R.id.etxtDefects);
    spinSelect = (Spinner) findViewById(R.id.spinName);
    spinSelect = (Spinner) findViewById(R.id.spinVehicle);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxFLevel);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxWWasher);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxSWheel);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxBrakes);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxClutch);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxHorn);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxHeater);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxWLights);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxMirrors);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxTires);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxExhaust);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxLights);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxExLeaks);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxBody);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxOil);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxCool);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxBelts);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxEnLeaks);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxBolts);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxWTri);
    cBoxClick = (CheckBox) findViewById(R.id.cBoxFire);

}

public void saveClicked(View v) {
    try {
        OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
                STORETEXT, 0));
        out.write(txtEditor.getText().toString());
        out.write(txtViewer.getText().toString());
        out.write(cBoxClick.getText().toString());
        out.write(spinSelect.getSelectedView().toString());
        out.close();

        Toast

        .makeText(this, "Your contents have been saved.", Toast.LENGTH_LONG)
                .show();
    }

    catch (Throwable t) {

        Toast

        .makeText(this, "Error: " + t.toString(), Toast.LENGTH_LONG).show();

    }
}

public void readFileInEditor() {
    try {

        InputStream in = openFileInput(STORETEXT);
        if (in != null) {
            InputStreamReader tmp = new InputStreamReader(in);
            BufferedReader reader = new BufferedReader(tmp);

            String str;
            StringBuilder buf = new StringBuilder();

            while ((str = reader.readLine()) != null) {
                buf.append(str + "n");
            }

            in.close();
            txtEditor.setText(buf.toString());
        }
    }

    catch (java.io.FileNotFoundException e) {
    }

    catch (Throwable t) {
        Toast.makeText(this, "Error: " + t.toString(), Toast.LENGTH_LONG)
                .show();

    }
}

private void Home() {
    Button btnHome = (Button) findViewById(R.id.btnHome);
    btnHome.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Checklist.this, Index.class));
        }
    });
}

这显示文本正在保存我只是不知道它保存到哪里而且我无法访问它

1 个答案:

答案 0 :(得分:0)

你有两个选择在android系统中读写文件,一个是将它存储在内部存储中(存储在内部存储中的文件只能由创建它的应用程序读取,没有其他文件管理器可以访问除非电话已植根,否则第二个是外部存储(这是您通常存储图片,音乐,视频等的地方,这可能是您的外部SD卡空间,也可能是手机的外部存储空间)。我为这两个选项提供了两个不同的代码片段。

写入内部存储空间

String msg = "This message will be written in internal storage."; // this message will be written in the file
try {
        FileOutputStream fileoutputstream = openFileOutput("example.txt",
                Context.MODE_PRIVATE); //here context.MODE_PRIVATE means the file will be stored in internal storage and example.txt is the file you want to create it with the name
        BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
                fileoutputstream);
        bufferedoutputstream.write(msg.getBytes());
        Toast.makeText(this, "file written", Toast.LENGTH_SHORT).show();
        bufferedoutputstream.close();
    } catch (FileNotFoundException e) {
        Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(this, "IOexception", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

请记住,这只是使用此技术的简单示例,即使文件存在,也会每次再次创建文件example.txt。

从内部文件中读取

try {
        FileInputStream fileinputstream = openFileInput("example.txt");
        BufferedReader bufferedreader = new BufferedReader(
                new InputStreamReader(fileinputstream));
        String fileContents = bufferedreader.readLine();
        Toast.makeText(this, fileContents, Toast.LENGTH_LONG).show();
    } catch (FileNotFoundException e) {
        Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(this, "IOexception", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

写入外部存储空间

String msg = "This file is to be stored in the external storage."; // this message will be written in the file
File externalpath = Environment.getExternalStorageDirectory(); // this line of code gets the external storage directory which usually is sdcard/, extSdcard/ or mnt/
File newfile = new File(externalpath, "example.txt");
//I have just created the file in the base of external storage but you can also create new folder in the external storage and then create the file in it.

    try {
        FileOutputStream fileoutputstream = new FileOutputStream(newfile);
        BufferedWriter bufferedwrite = new BufferedWriter(
                new OutputStreamWriter(fileoutputstream));
        bufferedwrite.write(msg.getBytes());
        Toast.makeText(this, "file written", Toast.LENGTH_SHORT).show();
        bufferedwrite.close();
        fileoutputstream.close();
    } catch (FileNotFoundException e) {
        Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(this, "IOexception", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

从外部存储中读取

File externalpath = Environment.getExternalStorageDirectory();
File newfile = new File(externalpath, "example.txt");
//You need to modify above line appropriately if you have stored the file in some directory or other place.

    FileInputStream fileinputstream;
    try {
        fileinputstream = new FileInputStream(newfile);
        BufferedReader bufferedreader = new BufferedReader(
                new InputStreamReader(fileinputstream));
        String filecontents = bufferedreader.readLine();
        Toast.makeText(this, filecontents, Toast.LENGTH_SHORT).show();
        bufferedreader.close();
        fileinputstream.close();
    } catch (FileNotFoundException e) {
        Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(this, "IOexception", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

注意:上面提到的代码只是例如它可能不是最有效的工作方式,因此您可能需要添加更多的逻辑检查和错误处理。

编辑:不要忘记在Manifest.xml文件中添加必要的权限。在这种情况下,我会添加

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