繁体   English   中英

读写文件-Java和android

[英]Reading and writing files - Java and android

我正在尝试将数据保存到android中的文件中,但似乎无法发现自己​​的错误,也正在尝试读取它。

概述:我有2个按钮和editext

我在编辑文本中键入内容,然后按“保存”按钮以保存到文件(save_to_file函数)。 当我按下“ read”按钮(read_file函数)时,我得到类似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));

此外,请在转换期间考虑基础字符集(请参见String-Doc )。 如果文件很大,则不要立即读取缓冲区,并使用StringBuilder连接读数。

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
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM