Sunday, May 12, 2013

Java File Output Stream Example

Java File Output Stream Example

We can write the file content by using File Output Stream .

File Output Stream  writes the stream of raw bytes to the file.

File output Stream writes the data by using the write() method.

File output Stream Example:


 import java.io.File;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
      //write the file content using File OutputStream.  
      public class FileOutputStreamExample {  
            public static void main(String[] args) {  
                 FileOutputStream fos = null;//<i>File Output Stream</i>   
                File fileName = null;  
                 try {  
                      fileName = new File("C:\\core.txt");  
                      fileName.createNewFile();  
                      fos = new FileOutputStream(fileName);  
                      fos.write("Hello Write this content".getBytes());  
                      fos.flush();                       
                } catch (IOException e) {  
                     e.printStackTrace();  
                } finally {  
                     try {  
                          if (fos != null){  
                               fos.close();  
                               fos = null;  
                          }  
                     } catch (IOException ex) {  
                          ex.printStackTrace();  
                     }  
                }  
           }  
      }  



0 comments:

Post a Comment

 
Disclaimer : If you find any mistakes / corrections / feedback Please let us know...This website author shall not accept liability or responsibility for any errors, mistakes or misstatements found in this website.