Skip to main content

Posts

Showing posts from November, 2018

Java I/O operations & streams : Read, Write from file

A stream is a sequence of data of different types like simple bytes, primitive data types and objects. Input Stream is used to read data from a source one at a time and Output Stream is used to write data to a destination one at a time. Byte Streams  Byte Streams are used to input and output 1 byte (i.e. 8 bits) at a time. Byte Stream classes for input and output are extended from java.io.InputStream and java.io.OutputStream abstract classes. FileInputStream and FileOutputStream are the commonly used classes for dealing with file read and write operations, which reads or writes data one byte at a time. Let's take an example, where we have an input file with data "Year is 2018". Below program will read one byte at a time and write in a new output file. package com.company; import java.io.*; public class Test { public static void main(String[] args) throws IOException { FileInputStream fin = null ; FileOutputStream fout = null ;

Java Exceptions - Checked and Unchecked

An Exception is a condition in your program which breaks the normal execution of the program. Like other programming languages, in case of an error condition, Java throws an exception object which has information about the error, state of the program etc. In this event, the run-time environment searches for exception-handler for particular type of exception in the code stack trace. If no handlers are found, then the program terminates. In Java, all Execptions and Errors are sub-classed from java.lang.Throwable parent class.                                              +----------------+                                           |   Throwable  |                                          +----------------+                                                /           \                                              /               \                                            /                  \                          +------------+                  +------------+