Advertisement



< 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 FileOutputStream object to write to 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");

In both of the examples of different constructors, we have created a FileOutputStream object to create an output stream to write to 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.


In the upcoming example, we are going to write a byte at a time to a file called TextBook.txt, by passing its full path D:\\TextBook.txt to the constructor of FileOutputStream. To do this, first the file is created and then bytes(one byte at a time) is written to it.

// 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);
	}
}
}



Output is -:


After the execution of the program, a file called "TextBook.txt" is created in D: drive, with the contents:
World peace is extremely important.


Program Analysis





Advertisement




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


In the next example, we are going to write a whole byte array(all 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.

//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);
	}
}
} 
 


Output is -:


After the execution of this program, a file called "Book1.txt" is created in D: drive, with the contents -:
World peace is extremely important.


Program Analysis


The program writes a byte array, bA(all at once) to the file Book1.txt, by calling the write(byte[] b) method of FileOutputStream class.



Please share this article -




< Prev
Next >
< FileInputStream
ByteArrayInputStream>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement