NotGI
NotGI

Reputation: 468

Reading and writing files - Java and android

I am trying to save data to a file in android and I can't seem to spot my mistake , I am trying to also read it.

Overview: I got 2 button and editext

I type something in the edit text and then press "save" button to save to file (save_to_file function). When i press the "read" button (read_file function) I get something like B[]@3213 I followed the android tutorial from here

My code :

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

}

i can't seem to spot my mistake since i followed the tutorial very carefully, could it be that my problem is in reading?

Upvotes: 0

Views: 63

Answers (2)

mrres1
mrres1

Reputation: 1155

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

Upvotes: 0

funglejunk
funglejunk

Reputation: 288

There is no problem with reading - you are just calling the toString()-method of the buffer-object itself. This will not output its content, but its memory address.

Try calling:

changeText.setText(new String(buff));

Furthermore, consider the underlying charset during conversion (see String-Doc). If your file is rather big, read the buffer not at once and use a StringBuilder to concatenate the readings.

Upvotes: 2

Related Questions