Java - Files & Streams
WHAT IS FILE CLASS:
- The file class is Java's representation of a file name or directory path name.
- Because the file and directory names have different formats on different platforms.
METHODS OF FILE CLASS:
- boolean exists() : Tests whether the file / directory by this pathname exists
- boolean canExecute() : Tests whether the file / directory by this pathname can be executed.
- boolean canWrite() : Tests whether the application can modify the file / directory by this pathname.
- boolean canRead() : Tests whether application can read the file / direcotry by this pathname.
- boolean isAbsolute() : Tests whether the abstract pathname is absolute. Returns true or false.
- String getName() : Returns the name of the file / directory denoted by this pathname.
- String getParent() : Returns the pathname string of the pathname's parent.
- String getParentFile() : Returns the abstract pathname of this abstract parthname's parent.
- String getPath() : Converts the abstract pathname into a pathname string.
- String getAbsolutePath() : Returns the absolute pathname string of this abstract pathname.
PROGRAM ON FILE CLASS:
import java.io.file;
class Fileclass
{
public static void main (String args[])
{
File f = new File("c://example.txt")
System.out.println("Name: " + f.getName());
System.out.println("Name: " + f.getPath());
System.out.println("Name: " + f.getparent());
}
}
EXPLAIN : SERIALIZATION & DE-SERIALIZATION:
- Serialization is the process of converting the state of an object into a byte stream.
- The byte stream that is created is platform independant.
- Thus, The object serialized on one platform can be de-serialized on the other platform.
- java.io.Serializable interface is used for using serialization.
- Whereas, De-Serialization is the reverse process of converting the byte stream into an object.
EXPLAIN : JAVA COLLECTION FRAMEWORK
- Collection in java is a framework that provides an architecture to store and manipulate the group of objects or data.
- It can achieve all the operations that can be performed on data such as searching, sorting, inserting & deleting, etc.
- It provides many interfaces and classes.
- Examples (Interfaces) : Set, Queue & De-Queue
- Examples (Classes) : ArrayList, LinkedList & Vector)
EXPLAIN : "ARRAYLIST" CLASS IN JAVA
- This class uses a dynamic array for sorting the elements.
- It inherits the "AbstractList" class.
- It implements the "List" interface.
- It can contain duplicate elements.
- It maintains the insertion order.
- This class is not synchronized.
EXPLAIN : CONSTRUCTORS IN "ARRAYLIST" CLASS OF JAVA
- ArrayList() : It constructs an empty ArrayList with the default initial capacity.
- ArrayList(int capacity) : It constructs an empty ArrayList with the specified initial capacity.
- ArrayList(Collection c) : It constructs an ArrayList containing the elements of the specified collection in the ArrayList.
EXPLAIN : METHODS IN "ARRAYLIST" CLASS OF JAVA
- add(int index, object element) : Inserts the specified element at the specified position in ArrayList.
- clone() : Returns a shallow copy of this ArrayList.
- clear() : Removes all the elements of this ArrayList.
- remove(int index) : Removes the specified element at the specified position in the ArrayList.
- size() : Returns the size (No. of components) in this ArrayList.
PROGRAM : "ARRAYLIST" CLASS
import java.util.*;
class TestCollection
{
public static void main (String args[])
{
ArrayList Ashish = new ArrayList Ashish // Creates the ArrayList.
Ashish.add("Saurabh") // Adding the first oject.
Ashish.add("Kaju") // Adding the second object.
Ashish.add("Lakva") // Adding the third object.
// Traversing the list throught iterator.
iterator itr = Ashish.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
EXPLAIN : MAP INTERFACE
- The "Map" interface contains the values on the basis of key and value pair.
- Each key and the value pair is known as an entry.
- The Map only contains the unique keys.
- Operations like searching, updating & deleting the elements can be done using the "Map" interface.
EXPLAIN : METHODS OF "ARRAYLIST"
- void add(int index, Object element) : It is used to Insert the specified element at the specified position index in a list.
- boolean addAll(Collection c) : It is used to Append all of the elements in the specified collection to the end.
- void clear() : It is used to Remove all of the elements from the List.
- int lastIndexOf(Object o) : It is used to Return the Index in this list of the last occurrence of the specified element.
- Object[] toArray() : It is used to return an array containing all of the elements in this list in the correct order.
- Object[] toArray(Object[] a) : It is used to Return an array containing all of the elements in this list in the correct order.
- boolean add(Object o) : It is used to Append the specified element to the end of the list.
EXPLAIN : ITERATOR INTERFACE
- This interface provides the facility of iterating the elements in forward direction only.
- There are 3 Methods in the Iterator Interface.
EXPLAIN : METHODS OF "ITERATOR" INTERFACE
- public boolean hasNext() : It returns True of the Iterator has more elements.
- public Object next() : It Returns the element & moves the cursor pointer to the next element.
- public void remove() : It Removes the last elements returned by the Iterator, It is rarely used.
EXPLAIN : SET INTERFACE
- A Set Interface is a interface that does not contain any Duplicate Values.
- It provides the 3 General Purpose Implementations in Java.
- There are 3 ways of implementing in Java:
ENLIST : WAYS OF IMPLEMENTING SET INTERFACE
- HashSet.
- LinkedHashSet.
- TreeSet.
- The "HashSet" is the best performing implementation of Set Interface. It stores it's elements in a "HashTable" and does not gurantee any type of ordering iretation.
EXPLAIN : DATE CLASS
- The "java.util.date;" class represents Date & Time in Java. it provides Constructors & Methods to deal with Date & Time in Java.
- It Implements Serializable, Cloneable & Comparable <Date> Interface. It is inherited by "java.sql.date", "java.sql.time" & "java.sql.timestamp" Interfaces.
EXPLAIN : METHODS IN THE DATE CLASS
- boolean after(Date date) : Tests if the Current Date is after the Given Date or not.
- boolean before(Date date) : Tests if the Current Date is before the Given Date or not.
- boolean equals(Date date) : Compares if the Current Date with the Given Date for Equality.
- static Date from(Instant instant) : Returns an instant of Date Object from the Instant Date.
- long getTime() : Returns the Time represented by this Date Object.
- int hashCode() : Returns the Hash Code Value for this Date Object.
- void setTime(Long time) : Changes the Current Date & Time to the given Time.
EXPLAIN : CONSTRUCTORS IN THE DATE CLASS
- Date() : Creates a Date Object representing the Current Date & Time.
- Date(Long milliseconds) : Creates a Date object for the given milliseconds Since January 1, 1970, 00:00:00 GMT.