Using filesystem in Android

You can use Android’s filesystem the same as you use it in your life. Creating,editing,deleting,reading and etc.

I want to remember that filesystem is based on Linux. Android allows to use full advantages of java.io package when you are working with it without limitations.

What type of “files”you can access?

  • files,directories,etc
  • raw files which are stores as resources
  • compiled custom xml files

Note: you can use filesystem under the data path for the application in which you are working and external filesystem for example SDCard’s filesystem.

Here I am going to show example which works with filesystem under application. This example will show how to create file,save data and read data from this file.

Context has method which returns FileOutputStream it is called openFileOutput. This method we will use for accessing to our new file:

outputStream = openFileOutput("some.txt",Context.MODE_PRIVATE);

first parameter is file name and second is access mode. As I showed in previous post access mode allows to specify who will have access to this file. Here I used MODE_PRIVATE for accessing only from this application.
Note: don’t forget to flush and close your output stream.

And when I need to read I also need to use Context,but now other method openFileInput which returns FileInputStream:

inputStream = openFileInput("some.txt");

parameter is file name which need to open.
Note: don’t forget to close your input stream.
When I have both these streams I can easily write data and read it from file.

Also if you want to get your file you should look at:

data/data/your_package/files/your_file_name

filesystem

Okay. Let’s look to the example:

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;public class Main extends Activity{private static String LOG_APP_TAG = "tag";private EditText saveText;private EditText loadText;private Button saveBtn;private Button loadBtn; @Override  public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); saveText = (EditText) findViewById(R.id.saveField); saveBtn = (Button) findViewById(R.id.saveBtn); loadText = (EditText) findViewById(R.id.loadField); loadBtn = (Button) findViewById(R.id.loadBtn); saveBtn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){FileOutputStream outputStream = null;try{outputStream = openFileOutput("some.txt",Context.MODE_PRIVATE);outputStream.write(saveText.getText().toString().getBytes())} catch(IOException e){Log.e(LOG_APP_TAG,e.getMessage())} finally{if (outputStream != null){try{outputStream.flush();outputStream.close()} catch (IOException e){Log.e(LOG_APP_TAG,e.getMessage())}}}}}); loadBtn.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){FileInputStream inputStream = null;try{inputStream = openFileInput("some.txt");byte[] reader = new byte[inputStream.available()];while (inputStream.read(reader) != -1){}loadText.setText(new String(reader))} catch(IOException e){Log.e(LOG_APP_TAG,e.getMessage())} finally{if (inputStream != null){try{inputStream.close()} catch (IOException e){Log.e(LOG_APP_TAG,e.getMessage())}}}}}); }}

As I said it is very easy to use filesystem in Android.
The main problem is:“How to get right stream object?”. After this example I think it is not a problem.

Hope it was useful article for you.

Code from this article

 

Help to make better articles,leave a feedback.

Share

No related posts.

3 comments to Using filesystem in Android

  • Where is my file written to when using the NetBeans IDE and anroid-sdk-windows v5? I have searched my machine but it is not to be found. Stepping through my code produces no error so I am thinking it should be some where.

    ex.

    Button nextScreen = (Button) findViewById(R.id.btnSaveGPSrulename);
    nextScreen.setOnClickListener(new View.OnClickListener(){
    public void onClick(View view){
    try{
    FileOutputStream fos = openFileOutput(“myfile.txt”,MODE_PRIVATE);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    osw.write(“Office rule1~-23.9876 98.1234″);
    osw.flush();// ensure that everything is really written out and close
    osw.close();
    }
    catch (FileNotFoundException e){
    e.printStackTrace();
    }
    catch (IOException e2){
    e2.printStackTrace();
    }
    finally{
    Intent myIntent = new Intent(view.getContext(),MainActivity.class);
    startActivityForResult(myIntent,0);
    }
    }
    });

  • gautham

    hay i had a doubt ..I was trying to create a new Dir in data/data/your_package/files/ like
    data/data/your_package/files/mydir1 using makdir();which i did …but i dont get how do i write file into this dir(mydir) .I tried using openFileOutput() but it would not take path separators ..so every time i try creatin a file in this dir ..its drops the file in data/data/your_package/files/ and not in data/data/your_package/files/mydir …pls help..

    regards
    gautham

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.