Pr.Pg Next Pg

FileInputStream and FileOutputStreams tutorials

 

Output Stream

  • An output stream that writes bytes to a file. If the output file exists, it can be replaced or appended to. If it does not exist, a new file will be created.

  • This stream is not buffered. Most callers should wrap this stream with a BufferedOutputStream.

  • Use FileWriter to write characters, as opposed to bytes, to a file.

 

File Input Stream

  • An input stream that reads bytes from a file.

  • This stream is not buffered. Most callers should wrap this stream with a BufferedInputStream.

  • Use FileReader to read characters, as opposed to bytes, from a file.

 

Appliation Example

  • We will now illustrate the example of IO streams using an application.

  • The application will work the same way as the SharedPreference i.e the layout is the same.

  • Below is the java code of the activity.

 

FileIOStream.java

 

package com.example.helloandroid;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

import android.app.Activity;

import android.content.Context;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

 

public class IOStream extends Activity implements OnClickListener {

Button bload;

Button bsave;

EditText etData;

TextView tvData;

String filename="MyFile";

FileOutputStream fos;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.sharedprefs);

setupVars();

}

private void setupVars() {

// TODO Auto-generated method stub

bload =(Button)findViewById(R.id.bLoadData);

bsave = (Button)findViewById(R.id.bSaveData);

etData = (EditText)findViewById(R.id.etData);

tvData = (TextView)findViewById(R.id.tvData);

bload.setOnClickListener(this);

bsave.setOnClickListener(this);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch(v.getId()){

case R.id.bSaveData:

String stringData = etData.getText().toString();

try {

fos = openFileOutput(filename, Context.MODE_PRIVATE);

fos.write(stringData.getBytes());

fos.close();

tvData.setText("Created");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

case R.id.bLoadData:

String returnedData="Data not available";

FileInputStream fin=null;

try {

fin = openFileInput(filename);

byte[] ourByteArray = newbyte[fin.available()];

while(fin.read(ourByteArray)!=-1){

returnedData = new String(ourByteArray);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

fin.close();

tvData.setText(returnedData);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

break;

}

}

}

 

 

  • We have made a string which contains the name of the file in which we want to store the bytes.

  • In the onClick() method of the Save button we are creating a file using the FileOutputStream object.

  • The name of the file is the string which we have initialized.

  • We then use the write() method to write into the file and finally we close the file.

  • Various exceptions are possible and hence try catch statements are added.

  • For the onCllick() method of the Load button, we load the file into our FileInputStream object and then we make a byte array in which we will store the data that is read.

  • The available() method returns the approimate number of bytes in the file and hence we make an array as large so as to contain all the data.

  • We then read the contents from the file and store into the String…hence converting from byte array to a string.

  • We also close the fil.

  • We then display the string in the Text View.

 

Pr.Pg border                                              Next Pg