Showing posts with label Java Stream Objects. Show all posts
Showing posts with label Java Stream Objects. Show all posts

Wednesday, November 30, 2011

Append output to file using FileOutputStream

    package org.best.example;
    /*
      Append output to file using FileOutputStream
      This example shows how to append byte data to a file using write method of
      Java FileOutputStream object.
    */
    
    import java.io.*;
    
    public class AppendToFile {
    
      public static void main(String[] args) {
      
        String strFilePath = "C://FileIO//demo.txt";
      
         try
         {
           /*
            * To append output to a file, use
            * FileOutputStream(String file, booean blnAppend) or
            * FileOutputStream(File file, booean blnAppend) constructor.
            *
            * If blnAppend is true, output will be appended to the existing content
            * of the file. If false, file will be overwritten.
            */
          
          FileOutputStream fos = new FileOutputStream(strFilePath, true);
          String strContent = "Append output to a file example";
        
           fos.write(strContent.getBytes());
        
          /*
           * Close FileOutputStream using,
           * void close() method of Java FileOutputStream class.
           *
           */
        
           fos.close();
        
         }
         catch(FileNotFoundException ex)
         {
          System.out.println("FileNotFoundException : " + ex);
         }
         catch(IOException ioe)
         {
          System.out.println("IOException : " + ioe);
         }
      
      }
    }

Tuesday, November 29, 2011

Copy binary file using Streams

    package org.best.example;
    /*
            Copy binary file using Streams
            This example shows how to copy a binary file using Java FileInputStream
            and FileOutputStream classes. If you want to copy text file use
            FileReader and FileWriter classes instead of FileInputStream and
            FileOutputStream classes.
    */
    
    import java.io.FileNotFoundException;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class CopyBinaryFile {
    
            public static void main(String[] args) {
                  
                    String strSourceFile="C:/FileIO/source.dat";
                    String strDestinationFile="C:/FileIO/dest.dat";
                  
                    try
                    {
                            //create FileInputStream object for source file
                            FileInputStream fin = new FileInputStream(strSourceFile);
                          
                            //create FileOutputStream object for destination file
                            FileOutputStream fout = new FileOutputStream(strDestinationFile);
                          
                            byte[] b = new byte[1024];
                            int noOfBytes = 0;
                          
                            System.out.println("Copying file using streams");
                          
                            //read bytes from source file and write to destination file
                            while( (noOfBytes = fin.read(b)) != -1 )
                            {
                                    fout.write(b, 0, noOfBytes);
                            }
                          
                            System.out.println("File copied!");
                          
                            //close the streams
                            fin.close();
                            fout.close();                 
                          
                    }
                    catch(FileNotFoundException fnf)
                    {
                            System.out.println("Specified file not found :" + fnf);
                    }
                    catch(IOException ioe)
                    {
                            System.out.println("Error while copying file :" + ioe);
                    }
            }
    }
    
    /*
    Typical output would be
    Copying file using streams
    File copied!
    */

Monday, November 28, 2011

Create FileOutputStream object from File object

    package org.best.example;
    /*
      Create FileOutputStream object from File object
      This example shows how to skip createFileOutputStream object from File object.
    */
    
    import java.io.*;
    
    public class CreateFileOutputStreamObjectFromFile {
    
      public static void main(String[] args) {
      
        File file = new File("C://FileIO//demo.txt");
      
        /*
         * To create FileOutputStream object from File object use,
         * FileOutputStream(File file) constructor.
         */
      
         try
         {
          FileOutputStream fos = new FileOutputStream(file);
         }
         catch(FileNotFoundException ex)
         {
          System.out.println("Exception : " + ex);
         }
      }
    }

Sunday, November 27, 2011

Create FileOutputStream object from String file path

    package org.best.example;
    /*
      Create FileOutputStream object from String file path
      This example shows how to create FileOutputStream object from String containing
      file path.
    */
    
    import java.io.*;
    
    public class CreateFileOutputStreamObjectFromString {
    
      public static void main(String[] args) {
      
        String strFilePath = "C://FileIO//demo.txt";
      
        /*
         * To create FileOutputStream object from String use,
         * FileOutputStream(String filePath) constructor.
         */
      
         try
         {
           FileOutputStream fos = new FileOutputStream(strFilePath);
         }
         catch(FileNotFoundException ex)
         {
           System.out.println("Exception : " + ex);
         }
      }
    }

Saturday, November 26, 2011

Write byte array to a file using FileOutputStream

    package org.best.example;
    /*
      Write byte array to a file using FileOutputStream
      This example shows how to write a byte array to a file using write method of
      Java FileOutputStream object.
    */
    
    import java.io.*;
    
    public class WriteByteArrayToFile {
    
      public static void main(String[] args) {
      
        String strFilePath = "C://FileIO//demo.txt";
      
         try
         {
          FileOutputStream fos = new FileOutputStream(strFilePath);
          String strContent = "Write File using Java FileOutputStream example !";
            
          /*
           * To write byte array to a file, use
           * void write(byte[] bArray) method of Java FileOutputStream class.
           *
           * This method writes given byte array to a file.
           */
        
           fos.write(strContent.getBytes());
        
          /*
           * Close FileOutputStream using,
           * void close() method of Java FileOutputStream class.
           *
           */
        
           fos.close();
        
         }
         catch(FileNotFoundException ex)
         {
          System.out.println("FileNotFoundException : " + ex);
         }
         catch(IOException ioe)
         {
          System.out.println("IOException : " + ioe);
         }
      
      }
    }

Friday, November 25, 2011

Write file using FileOutputStream

    package org.best.example;
    /*
      Write file using FileOutputStream
      This example shows how to write byte data to a file using write method of
      Java FileOutputStream object.
    */
    
    import java.io.*;
    
    public class WriteFile {
    
      public static void main(String[] args) {
      
        String strFilePath = "C://FileIO//demo.txt";
      
         try
         {
          FileOutputStream fos = new FileOutputStream(strFilePath);
          byte b = 01;
        
          /*
           * To write byte data to a file, use
           * void write(int i) method of Java FileOutputStream class.
           *
           * This method writes given byte to a file.
           */
        
           fos.write(b);
        
          /*
           * Close FileOutputStream using,
           * void close() method of Java FileOutputStream class.
           *
           */
        
           fos.close();
        
         }
         catch(FileNotFoundException ex)
         {
          System.out.println("FileNotFoundException : " + ex);
         }
         catch(IOException ioe)
         {
          System.out.println("IOException : " + ioe);
         }
      
      }
    }