< Prev
Next >



FileOutputStream



FileOutputStream class is a subclass of OuputStream abstract class. FileOutputStream is used create an output stream, which is used to write byte/bytes to a file.




Constructors :


1) FileOutputStream(File file)
This constructor creates a FileInputStream object to read a file specified by the File object, which is passed to this constructor as a parameter.
Example -
File file= new File("D:\\Textbook.txt");
FileInputStream fis= new FileInputStream(file); 
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-:

FileOutputStream fis= new FileOutputStream("D:\\TextBook.txt");
Both constructors have created a FileInputStream object to create an input stream to read a file called "TextBook.txt" which is located in the D drive.




Methods for writing using FileOutputStream


Some methods that we generally use while working with FileOutputStream are shown in the table below

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.






Writing one byte at a time to a file using write() method of FileOutputStream.


Writing a byte at a time to a file called TextBook.txt, by passing its full path D:\\TextBook.txt to the constructor of FileOutputStream. First, the file is created and then byte(one at a time) is written to it.
// 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);
}

}
}



Output is -:


A file called "TextBook.txt" is created in D: drive, with the contents -:
World peace is extremely important.


Program Analysis







Writing a byte array to a file using write(byte[] b) method of FileOutputStream.


Writing a whole byte array(at once) to a file with its full path - D:\\Book1.txt. This performs faster writing, because writing a whole byte array at once is faster than writing one byte at a time.
// 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);
}

}
} 
 


Output is -:


A file called "TextBook.txt" is created in D: drive, with the contents -:
World peace is extremely important.


Program Analysis






Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< FileInputStream
ByteArrayInputStream>
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2