The FileOutputStream class is used for creating or writing into the file. For more advanced file output, we use the RandomAccessFile class. A FileOutputStream object can be created using one of the following constructors:
• FileOutputStream(String name)
• FileOutputStream(File file)
• FileOutputStream(FileDescriptor fd)
The first constructor takes a String parameter, which specifies the name of the file to be used for output. The second constructor takes a File object parameter, which specifies the output file. The third constructor takes a FileDescriptor object as its only parameter.
import java.io.*;
class FileOutputStreamJavaExample
{
public static void main(String args[]) throws IOException
{
int i;
BufferedReader vv=new BufferedReader(new InputStreamReader(System.in));
FileOutputStream hh;
try
{
hh=new FileOutputStream("ecomputernotes.bat");
}
catch(IOException e)
{
System.out.println("File can not be created");
return;
}
try
{
System.out.println("Enter lines of text, # to quit");
do
{
i=vv.read();
hh.write(i);
}while(i !='#');
}
catch(FileNotFoundException e)
{
System.out.println("File error");
}
hh.close();
}
}