All About Class FileOutputStream in Java



A FileOutputStream is an output stream for writing data to a File or to a FileDescriptor.
FileOutputStream is useful to write some text to a text file.As like other streams we need to open the file first,then we will be able to write something in it.
The class structure of FileOutputStream is given as below:

public class java.io.FileOutputStream extends java.io.OutputStream{
//constructors
public FileOutputStream (File file);
//create a FileInputStream object to write to the specified File object. It may throw
//IOException if the file is not available or could not be opened for writing.It may also throw
//SecurityException if a security manager exists,and its checkWrite() method is called with the 
//file descriptor to see if the application is allowed write access to the specified File.
//This may result in a SecurityException.
public FileOutputStream (FileDescriptor fdObj);
//create a FileInputStream object to write to the specified FileDescriptor object.It may throw
//SecurityException if a security manager exists,and its checkWrite() method is called with the 
//file descriptor to see if the application is allowed write access to the specified FileDescriptor or file.
//This may result in a SecurityException.
public FileOutputStream (String name);
//create a FileInputStream object to write to the specified File object. It may throw
//IOEception if the file is not available or could not be opened for writing.It may throw
//SecurityException if a security manager exists,and its checkRead() method is called with the 
//file descriptor to see if the application is allowed read access to the file.
//This may result in a SecurityException.
//Methods
public void close();//closes the input source;further read attempt on a closed input will generate
//an IOException.
public void finalize();
public final FileDescriptor getFD();
public void write(byte b[]);//writes an entire byte array i.e block of bytes at a time.
public void write(byte b[],int offSet,int length);//write out length number of bytes from the block of 
//bytes b starting at offset offSet.
public abstract void write(int b);//writes one byte at a time
}
 
An Example

class FileOutputTest{
public static void main(String args[]){
byte buffer=new byte[512];
int i=0;
int size=0;
String inFile=null;
String outFile=null;
FileInputStream fin;
FileoutputStream fout1,fout2;
try{
inFile=arg[0];//take input from user in command line
outFile=arg[1];
int c=0;
fout1=new FileOutputStream(arg[0]);
while((c.System.in.read())!=1){
//read from keyboard
buffer[i]=(byte)c;
i++;
}
size=i;
fout1.write(buffer);
fout1.close();
fin=new FileInputStream(arg[0]);
fout2=new FileOutputStream(arg[0]);
fin.read(buffer);
for(int i=0;i<buffer.length;i++)
{
fout2.write(buffer[i]);
}
if(size==i)
{
System.out.println("File successfully copied");
}
else
{
System.out.println("File copy error");
}
}
catch(ArrayIndexOutOfBoundException e)
{
System.out.println("File handling error");
}
}
}
 
We can append a file by using FileOutputStream.
FileOutputStream object can be opened in the following way:

  1. FileOutputStream fout;
    fout=new FileOutputStream(String name);//name is the output file along with the path.
  2. File myFile;
    myFile=new File(String name);
    fout=new FileOutputStream(myFile)


All About Class FileOutputStream in Java All About Class FileOutputStream in Java Reviewed by Animesh Chatterjee on October 09, 2018 Rating: 5

No comments:

Powered by Blogger.