读写文件 - Java和android

时间:2015-05-10 17:42:32

标签: java android file

我正在尝试将数据保存到android中的文件中,而我似乎无法发现我的错误,我正在努力阅读它。

概述: 我有2个按钮和editext

我在编辑文本中输入内容,然后按"保存"按钮保存到文件(save_to_file函数)。 当我按下"阅读"按钮(read_file函数)我得到类似B [] @ 3213的东西 我按照here

的android教程进行了操作

我的代码:

package com.keddy.filetesting;

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

import java.io.FileInputStream;
import java.io.FileOutputStream;


public class MainActivity extends Activity {

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


    @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);
    }

    public void save_to_file(View view){
        try{
            String filename = "Myfile.txt";
            EditText buffer= (EditText)findViewById(R.id.editText);
            String pureText= buffer.getText().toString();
            pureText += '\n';
            FileOutputStream fos = openFileOutput(filename , Context.MODE_APPEND);
            fos.write(pureText.getBytes());
            fos.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void read_file(View view){
        try
        {
            String filename = "Myfile.txt";
            FileInputStream fis = openFileInput(filename);
            int len = fis.read();
            byte[] buff = new byte[len];
            fis.read(buff,0,len);
            EditText changeText = (EditText)findViewById(R.id.editText);
            changeText.setText(buff.toString());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

我似乎无法发现自己​​的错误,因为我非常仔细地阅读了教程,可能是我的问题在于阅读吗?

2 个答案:

答案 0 :(得分:2)

读取没有问题 - 你只是调用缓冲区对象本身的toString()方法。这不会输出其内容,而是输出内存地址。

尝试致电:

changeText.setText(new String(buff));

此外,在转换过程中考虑底层字符集(请参阅String-Doc)。如果您的文件很大,请不要立即读取缓冲区并使用StringBuilder连接读数。

答案 1 :(得分:0)

public void save_to_file() throws Exception
{
    FileOutputStream fos = null;

    try
    {
        String filename = "Myfile.txt";

        EditText buffer= (EditText)findViewById(R.id.editText);

        String pureText= buffer.getText().toString()

        pureText += "\n"; // double-quote

        FileOutputStream fos = openFileOutput(filename , Context.MODE_APPEND);

        fos.write(pureText);

        // call 'flush' and 'close' in the finally clause
    }
    catch(Exception e)
    {
        throw e;
        //e.printStackTrace();
    }
    finally
    {
        if(fos != null)
        {
            try
            {
                fos.flush();
            }
            catch(Exception e)
            {
                throw e;
            }
            finally
            {
                try
                {
                    fos.close();
                }
                catch(Exception e)
                {
                    throw e;
                }
            }
        }
    }
}

public void read_file() throws Exception
{
    FileReader fileReader = null;

    try
    {
        String filename = "Myfile.txt";

        fileReader = new FileReader( openFileInput(filename) );

        StringWriter stringWriter = new StringWriter();
        int len = -1;

        char[] buff = new char[512];

        while((len = fix.read(buff)) != -1)
            stringWriter.write(buff,0,len);

        String fileContents = stringWriter.toString();

        EditText changeText = (EditText)findViewById(R.id.editText);

        changeText.setText(fileContents);
    }
    catch(Exception e)
    {
        throw e;
        //e.printStackTrace();
    }
    finally
    {
        if(fileReader != null)
        {
            try
            {
                fileReader.close();
            }
            catch(Exception e)
            {
                throw e
            }
        }
    }
}