Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Objective:
In the previous article Collection in Java we learned about the collection method used in Java. In this article we will learn about the Set in Collection and File Handling in Java.

 

Set:

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.

Set have its implementation in various classes like

  1. HashSet

  2. LinkedHashSet.

  3. TreeSet

Methods with description:

add( )

Adds an object to the collection

clear( )

Removes all objects from the collection

contains( )

Returns true if a specified object is an element within the collection

isEmpty( )

Returns true if the collection has no elements

iterator( )

Returns an Iterator object for the collection which may be used to retrieve an object

size( )

Returns the number of elements in the collection

 

Example 1:

Following is the example to explain Set functionality:

import java.util.*;

public class SetDemo {

 public static void main(String args[ ]) {
    int count[ ] = {34, 22,10,60,30,22};
    Set<Integer> set = new HashSet<Integer>();
    try{
       for(int i = 0; i<5; i++){
          set.add(count[i]);
       }
       System.out.println(set);
 
       TreeSet sortedSet = new TreeSet<Integer>(set);
       System.out.println("The sorted list is:");
       System.out.println(sortedSet);

       System.out.println("The First element of the set is: "+
                         (Integer)sortedSet.first());
       System.out.println("The last element of the set is: "+
                       (Integer)sortedSet.last());
    }
    catch(Exception e){}
 }
}

//Output:

[amrood]$ java SetDemo
[34, 30, 60, 10, 22]
The sorted list is:
[10, 22, 30, 34, 60]
The First element of the set is: 10
The last element of the set is: 60

 

File Handling in java:

Java FileInputStream and OutputStream

In Java, FileInputStream and FileOutputStream classes are used to read and write data in file. In another words, they are used for file handling in java.

 

Java FileOutputStream:

Java FileOutputStream is an output stream for writing data to a file.

If you have to write primitive values then use FileOutputStream.Instead, for character-oriented data, prefer FileWriter.But you can write byte-oriented as well as character-oriented data.

Example:

import java.io.*;  

class Test{  

public static void main(String args[]){  

try{  

    FileOutputstream fout=new FileOutputStream("abc.txt");  

    String s="Sachin Tendulkar is my favourite player";  

    byte b[]=s.getBytes();//converting string into byte array  

    fout.write(b);  

    fout.close();  

    System.out.println("success...");  

   }catch(Exception e){system.out.println(e);}  

 }  

}  

//Output:

success...

 

Java FileInputStream:

Java FileInputStream class obtains input bytes from a file.It is used for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

It should be used to read byte-oriented data for example to read image, audio, video etc.

 

Example:

import java.io.*;  

class SimpleRead{  

public static void main(String args[]){  

 try{  

   FileInputStream fin=new FileInputStream("abc.txt");  

   int i=0;  

   while((i=fin.read())!=-1){  

    System.out.println((char)i);  

   }  

   fin.close();  

 }catch(Exception e){system.out.println(e);}  

}  

}

//Output

Sachin is my favourite player.


Varun Kapila

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Related Items

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner