Advertisement
1) FileOutputStream(File file) |
---|
This constructor creates a FileOutputStream object to write to a file specified by the File object, which is passed to this constructor as a parameter. Example:
|
2) FileOutputStream(String path) |
---|
This constructor creates a FileOutputStream to write to a file which is accessed by the path mentioned in the parameters of this constructor.
Example:
|
Methods | Description |
---|---|
flush() | This method flushes any buffered data to be written out of this output stream. |
write(int b) | This method writes one byte at a time to this output stream. |
write(byte[]) | This method writes a whole byte array at a time to this input stream. |
close() | This method closes this outut stream and also frees any resources connected with this output stream. |
// Java - A program to write one byte at a time to a file using FileOutputStream.
import java.io.*;
class A
{
public static void main(String... ar)
{
try
{
String str= new String("World peace is extremely important");
byte bA[]=str.getBytes();
FileOutputStream fos=new FileOutputStream("D:\\TextBook.txt");
for(byte byt:bA)
{
fos.write(byt); //writing one byte at a time to the file.
}
fos.flush();
fos.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Advertisement
//Java - A program to write a byte array to a File using FileOutputStream.
import java.io.*;
class A
{
public static void main(String... ar)
{
try
{
String str=new String("World peace is extremely important");
byte bA[]=str.getBytes();
FileOutputStream fos=new FileOutputStream("D:\\Book1.txt");
fos.write(bA);
fos.flush();
fos.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement