Saboor880 9 Junior Poster

Hello to all! I have started learning android recently and I am facing a problem in file handling (in internal storage). I wrote a simple program which creates a file and writes some data in that file . I have two buttons first button writes data in the file and creates a toast message saying: "file saved" and second button reads data from the file and creates a toast message "file read". But the problem is that I when i press first button The toast message displays perfectly but I don't know that where the file goes in my device "Nexus 7". When I press second button then it does not read the data from file and no data displays in the text view, but toas message(file read) displays perfectly. I have checked my device download folder and also tried to find my file by file explorer but can't find my file. I am pasting code of my program below.

package com.hatflabs.writingfile;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

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

public class MainActivity extends AppCompatActivity {

    Button b1, b2;
    EditText ed1;
    TextView tv1;
    String data;
    String filename = "my file";


    FileOutputStream outputStream;

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

        // finding views by giving their id's
         b1 = (Button)findViewById(R.id.button);
         b2 = (Button)findViewById(R.id.button2);
         ed1 = (EditText)findViewById(R.id.editText);
        data = ed1.getText().toString();
         tv1 = (TextView)findViewById(R.id.tv);


        // click listener method for button 1
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                try {


                    outputStream =  openFileOutput(filename, Context.MODE_PRIVATE);
                    outputStream.write(data.getBytes());
                    outputStream.close();
                    Toast.makeText(getBaseContext(),"file saved", Toast.LENGTH_SHORT).show();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

        // click listener method for button2
              b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    FileInputStream fin = openFileInput(filename);
                    int c;
                    String temp="";
                    while((c = fin.read())!= -1)
                    {

                        temp = temp + Character.toString((char)c);


                    }


                    tv1.setText(temp);
                    Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();


                }

                catch(Exception e)
                {
                }

            }
        });

    }

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

Please guide me i shall be thankfull to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.