读取和写入文件-Java和android

读取和写入文件-Java和android,java,android,file,Java,Android,File,我试图用android将数据保存到一个文件中,但我似乎无法发现我的错误,我也试图读取它 概述: 我有两个按钮和编辑文本 我在编辑文本中键入一些内容,然后按“保存”按钮保存到文件(保存到文件功能)。 当我按下“读取”按钮(读取文件功能)时,我得到了类似于B[]@3213的结果 我从一开始就学习了android教程 我的代码: package com.keddy.filetesting; import android.app.Activity; import android.content.Con

我试图用android将数据保存到一个文件中,但我似乎无法发现我的错误,我也试图读取它

概述: 我有两个按钮和编辑文本

我在编辑文本中键入一些内容,然后按“保存”按钮保存到文件(保存到文件功能)。 当我按下“读取”按钮(读取文件功能)时,我得到了类似于B[]@3213的结果 我从一开始就学习了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();
        }
    }

}

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

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

请尝试拨打:

changeText.setText(new String(buff));

此外,考虑转换过程中的潜在字符集(见)。如果您的文件很大,请不要立即读取缓冲区,并使用a来连接读数。

感谢您的帮助和友好,我一直在为此绞尽脑汁,我是Java新手。
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
            }
        }
    }
}