Sunday, September 30, 2007

File Handling: Using FileOutputStream

If you want to write to a file we use the FileOutputStream class. FileOutputStream is meant for writing streams of raw bytes such as image data. (from JAVA API)

The following are constructors of this class from JAVA API:

FileOutputStream(File file)
Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(FileDescriptor fdObj)
Creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
FileOutputStream(String name)
Creates an output file stream to write to the file with the specified name.
FileOutputStream(String name, boolean append)
Creates an output file stream to write to the file with the specified name.

SAMPLE CODE:

Let's use the code from the sample code of FileInputStream:

/**
* @(#)ReadFile.java
*
* @JEDI Phase II Course Material
* @With comments insertions from
* @ erlyn manguilimotan
*/

import java.io.*;

public class ReadFile {

public static void main(String args[]){

BufferedReader fileRead = new BufferedReader (new InputStreamReader(System.in));
String fileName = "";
System.out.println("Enter the name of the file to Open");
try{
fileName=fileRead.readLine(); //input the name of the file
}catch(IOException e1){
}//end of catch

FileInputStream rf = null; //create an object for FileInputStream

try{
rf = new FileInputStream(fileName); //connects the object with the file name
}catch(FileNotFoundException e2){
System.out.println("File not found...");
}//end of catch

try{
char data; //variable to hold the data
int temp=0;
do{
temp=rf.read(); // Reads a byte of data from this input stream.
data = (char) temp; //stores the data into variable data
if(temp!=-1) //while it's not the end of file
{
System.out.print(data);
}
}while(temp!=-1); //ends the loop if it's the end of file.

}catch(IOException e3){
System.out.println("Problem in reading the file..");

}//end of catch

//WE WILL INSERT THE CODE HERE

} //end of main
}//end of class

Now let us insert the following code as indicated above:
*************************************************************
System.out.println("---------------------------------------");
System.out.println("Now let's try to add a line in the file");
System.out.println("We will use the same file");

FileOutputStream fos = null; //creates object for FileOutputStream
try{
fos = new FileOutputStream(fileName); //connects the object to the file
}catch(FileNotFoundException e4){ //exceptions
System.out.println("File cannot be opened for writing");
}

try{
boolean done = false; //used to text the loop
int data;
do{//do while the line is not a '.'
data=fileRead.read();// read from keyboard
if((char) data =='.')
done=true;
else
fos.write(data); //write to file
}while(!done);
}catch(IOException e5){
System.out.println("Problem in reading from file");
}

*************************************************************

Compile and execute the program. Check on syntax errors.

Note: Make sure you still have the text file you had earlier.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QUESTION:

What happened to your text file? Check the content of your text file again.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

No comments: