Welcome to the seventh part of our Core Java series. In the last part, we discussed about exceptional handling in Java. If you are new, please checkout the previous parts. In this part, we’ll discuss some other interesting stuff in Java. So Let’s get started.

File Class(java.io package)

A file class object is used to refer to a file or directory. It has three constructors -

  1. File(String fullpath)
  2. File(String path, String filename)
  3. File(File object, String filename)
File f1 = new File("c://temp/example.txt");

String path = "c://temp";
File f2 = new File(path, "example.txt");

File f3 = new File("c:/temp");
File f4 = new File(f3, "A.txt");

This class has several methods to handle files. Some of them are as follows -

  1. boolean exists() - It returns true if the file or directory exists on the hard disk.
  2. boolean isFile() - returns true if the file object refers to a file.
  3. boolean canRead()/canWrite() - It returns true if the file has read or write access.
  4. long length() - returns the length of the file in terms of bytes.
  5. String getName() - returns the name of the file.

Streams

A stream is a segment of characters or bytes that flows from our program to some output device and from some input device to our program. We can categorise it into two categories -

  1. byte stream - It contains InputStream and OutputStream.
  2. character stream - It contains Reader and Writer.

Let’s see how to read a file in Java using InputStream class.

import java.io.*;
class ReadFile{
	public static void main(String arg[]) throws IOException{
		FileInputStream fis = new FileInputStream("example.txt");
		int ch;
		while((ch = fis.read()) != -1)
			System.out.println((char)ch);
		fis.close();
	}
}

Buffered Input/Output

BufferedInputStream and BufferedOutputStream classes are used to perform buffered input/output operations. A buffer is an area of primary memory that sits between our program and the actual source and destination of the data(file). Buffers are used to speed up the I/O operations. They are useful when we have to read or write large volumes of data.

StringTokenizer class(java.util package)

This class is used to retrive the tokens of a string one by one. Tokens are separated with space.

String str = "Java is love.";
StringTokenizer st = new StringTokenizer(str);

while(st.hasMoreTokens())
	System.out.println(st.nextToken());			// return current token and shift the cursor to the next

Output of this program will be -

Java
is
love

Object Class

In Java, Object class is the superclass of all the classes. All classes whether user defined or pre defined extend the Object class. This class contains several public and protected methods that are inherited by all the classes in Java. e.g.

toString() - returns the string representation of the object
equals() - returns true if both objects are same.
hashcode() - generates a hashcode with respect to a key 
getClass() - returns the class of the given object
wait() - is used to manage threads
notify() - is used to manage threads

Data Structures

java.util package provides several build-in data structures to store data. Let’s discuss some of them.

  1. Vector -

It is a dynamic array. It can be used when we don’t know the size of data beforehand. add() method is used to add the elements and get method is used to retrieve them. Example -

Vector v = new Vector();
v.add("Hellow");
v.add("World");

System.out.print(v.get(0));
  1. LinkedList -

It implements the linked list data structure which stores element with an address.

LinkedList v = new LinkedList();
v.add("Hellow");
v.add("World");

System.out.print(v.get(0)); 		// World(First element)
  1. HashMap -

It is used to store key-value pairs. We can access any element using its keys in linear time (O(1)).

HashMap map = new HashMap()
map.put("James", 942375822);
map.put("Will", 389348954);

System.out.print(map.get("Will"));
  1. HashSet -

Set is used to store unique elements only. It checks whether the element is already present in the Set or not.

HashSet set = new HashSet();
set.add("Java");
set.add("Python");
set.add("Java");

Iterator iter = set.iterator(); 		// Iterator object to loop over the collection
while(iter.hasNext())
	System.out.print((String) iter.next());
  1. Stack -

It is a last in first out(LIFO) structure.

Stack s = new Stack();
s.push("Hello");
s.push("world");

s.pop();		// returns the top element of the stack and removes it

That’s all for now. Now it’s your turn to try what you learned in this blog. Happy Coding :)