Collection tutorial java

A collection is simply an object that represents a group of objects into a single unit. Collection framework: A collection framework is a unified architecture or a set of classes and interfaces for representing and manipulating collections. i.e. collection framework is used to store, retrieve and manipulate collections. Collection framework contains the following: Interfaces are abstract … Read more

Transient keyword in java

Transient variable: A variable declared with transient keyword is a transient variable. It is used in serialization process to provide flexibility for excluding some variables from serialization process. A variable declared with transient keyword will not be serialized. Example: TransientExample.java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;   /** * … Read more

Categories IO

Serialization and Deserialization in java

In java serialization is way used to convert an object into a byte stream which can be transported to any other running JVM through a network or can be persisted into disk and that object can be rebuilt again. Java provides serialization API for this. How to make a class serializable in java? To make … Read more

Categories IO

How To Check If A File Exists In Java

Example: FileCheckExample.java import java.io.File;   /** * This program is used to check that file * exists or not at given location. * @author w3schools */ class IOTest{ public void checkFileExistance(){ //Creating File object. File file = new File("F:\\New folder\\data1.txt"); if(file.exists()){ System.out.println("file exist."); }else{ System.out.println("file not exist."); } } }   public class FileCheckExample { … Read more

Categories IO

FileReader and FileWriter in java

FileReader: FileReader class is used for reading streams of characters from a file. Commonly used constructors of FileReader: 1. FileReader(File file) Creates a new FileReader, given the File to read from. 2. FileReader(String fileName) Creates a new FileReader, given the name of the file to read from. Example: FileReaderExample.java import java.io.FileReader;   /** * This … Read more

Categories IO

BufferedInputStream and BufferedOutputStream in java

BufferedInputStream and BufferedOutputStream used an internal buffer to store date for read and write operations. Commonly used constructors of BufferedInputStream: 1. BufferedInputStream(InputStream in) Creates a BufferedInputStream and saves its argument, the input stream in, for later use. 2. BufferedInputStream(InputStream in, int size) Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use. Example: BufferedInputStreamExample.java import java.io.BufferedInputStream; … Read more

Categories IO

DataInputStream and DataOutputStream in java

DataInputStream: DataInputStream used to read primitive data types from an input source. Commonly used constructors of DataInputStream: DataInputStream(InputStream in) Creates a DataInputStream that uses the specified underlying InputStream. Commonly used methods of DataInputStream: public String readLine() throws IOException Reads the next line of text from the input stream. It reads successive bytes, converting each byte separately into … Read more

Categories IO

Byte Streams in java

Byte Streams in java: Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream. Always close the files to avoid serious resource leaks. Byte streams should only be used for the most primitive I/O. It represents a kind of low-level I/O so you should avoid in case complicated data types like … Read more

Categories IO

FileInputStream and FileOutputStream in java

FileInputStream: FileInputStream stream is used for reading data from the files. Commonly used constructors of FileInputStream: 1. FileInputStream(File file) Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. 2. FileInputStream(String name) Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file … Read more

Categories IO

Inter-thread communication in java

Inter-thread communication: Inter-thread communication is a process in which a thread is paused running in its critical region and another thread is allowed to enter (or lock) in the same critical region to be executed. i.e. synchronized threads communicate with each other. Below object class methods are used for Inter-thread communication process: 1. wait(): this … Read more