Search

Write byte array to file using BufferedOutputStream

public class WriteByteArrayToFile
   {
     public static void main(String[] args)
     {
       String strFileName = "C:/FileIO/BufferedOutputStreamDemo";
       BufferedOutputStream bos = null;
       try
       {
         //create an object of FileOutputStream
         FileOutputStream fos = new FileOutputStream(new File(strFileName));
        
         //create an object of BufferedOutputStream
         bos = new BufferedOutputStream(fos);
        
         String str = "BufferedOutputStream Example";
        
         /*
         * To write byte array to file use,
         * public void write(byte[] b) method of BufferedOutputStream
         * class.
         */
         System.out.println("Writing byte array to file");
        
         bos.write(str.getBytes());
         
         System.out.println("File written");
       }
       catch(FileNotFoundException fnfe)
       {
         System.out.println("Specified file not found" + fnfe);
       }
       catch(IOException ioe)
       {
         System.out.println("Error while writing file" + ioe);
       }
       finally
       {
         if(bos != null)
         {
           try
           {
            
             //flush the BufferedOutputStream
             bos.flush();
            
             //close the BufferedOutputStream
             bos.close();
            
           }
           catch(Exception e){}
         }
       }
     }
   }


Java Vector Example

public class SimpleVectorExample
   {
     public static void main(String[] args)
     {
      
       //create a Vector object
       Vector v = new Vector();
      
       /*
       Add elements to Vector using
       boolean add(Object o) method. It returns true as a general behavior
       of Collection.add method. The specified object is appended at the end
       of the Vector.
       */
       v.add("1");
       v.add("2");
       v.add("3");
      
       /*
       Use get method of Java Vector class to display elements of Vector.
       Object get(int index) returns an element at the specified index in
       the Vector
       */
      
       System.out.println("Getting elements of Vector");
       System.out.println(v.get(0));
       System.out.println(v.get(1));
       System.out.println(v.get(2));
     }
   }



TreeSet Example in java

    public class SimpleJavaTreeSetExample
   {
     public static void main(String[] args)
     {
      
       //create object of TreeSet
       TreeSet tSet = new TreeSet();
      
       /*
       Add an Object to TreeSet using
       boolean add(Object obj) method of Java TreeSet class.
       This method adds an element to TreeSet if it is not already present in TreeSet.
       It returns true if the element was added to TreeSet, false otherwise.
       */
      
       tSet.add(new Integer("1"));
       tSet.add(new Integer("2"));
       tSet.add(new Integer("3"));
       
       /*
       Please note that add method accepts Objects. Java Primitive values CAN NOT
       be added directly to TreeSet. It must be converted to corrosponding
       wrapper class first.
       */
      
       System.out.println("TreeSet contains.." + tSet); }
   }