- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
126 Views
Externalization is used whenever we need to customize serialization mechanism. If a class implements an Externalizable interface then, object serialization will be done using writeExternal() method.Whereas at receiver’s end when an Externalizable object is a reconstructed instance will be created using no argument constructor and then the readExternal() method is called.If a class implements only Serializable interface object serialization will be done using ObjectoutputStream.At the receiver’s end, the serializable object is reconstructed using ObjectInputStream.
196 Views
After a serialized object has been written into a file, it can be read from the file and Deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.Exampleimport java.io.*; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("/tmp/employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); ... Read More
679 Views
This article will help you understand what I/O filter in Java is. The Java I/O Filter The Java I/O Filter is inside java.io package. It provides sets of input and output streams used for reading and writing data to input and output sources. There are different types of classes in java.io, naming Input Stream, Output Stream, etc. Some of the important types are discussed below − Input Stream The InputStream class of java.io is an abstract superclass which reads data from an input source. The source can be a file, a string, or anything that can contain data. This class ... Read More
3K+ Views
The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, object, localized characters, etc.A stream can be defined as a sequence of data. There are two kinds of Streams −InputStream: The InputStream is used to read data from a source.OutputStream: The OutputStream is used for writing data to a destination.As described earlier, a stream can be defined as a sequence of data. The InputStream is used to read data from ... Read More
308 Views
The File class provides exists() method this returns true if a file in the specified path exists else it returns false.ExampleLive Demoimport java.io.File; public class FileHandling { public static void main(String args[]) { File file = new File("samplefile"); if(file.exists()) { System.out.println("Given file existed"); } else { System.out.println("Given file does not existed"); } } }OutputGiven file does not existed
288 Views
While using FileInputStream, FileOutputStream, and RandomAccessFile classes, we need to pass the path of the file to their constructors. In case of a file in the specified path does not exist a FileNotFoundException is raised.Examplepublic class Sample { public static void main(String args[]) throws Exception { File file = new File("myFile"); FileInputStream fis = new FileInputStream(file); System.out.println("Hello"); } }OutputException in thread "main" java.io.FileNotFoundException: myFile (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.(Unknown Source) ... Read More
344 Views
When you try to access an element of an array at an index which is out of range, an ArrayIndexOutOfBoundsException exception is raised.ExampleLive Demopublic class ArrayIndexOutOfBounds { public static void main(String args[]) { try { int[] a = new int[]{1,2,3,4,5}; int x = 6; a[10] = x; } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("Array size is restricted to 5 elements only"); } } }OutputArray size is restricted to 5 elements only
1K+ Views
When you divide a number by zero an Arithmetic Exception number is thrown.ExampleLive Demopublic class DividedByZero { public static void main(String args[]) { int a, b; try { a = 0; b = 54/a; System.out.println("hello"); } catch (ArithmeticException e) { System.out.println("you cannot divide a number with zero"); } } }Outputyou cannot divide a number with zero
763 Views
Questions related to Java exception handling are most frequent during interviews for many companies and even in exams. One such question that an interviewer might ask is whether there is a case when the finally block does not execute in Java. We will try to find the answer to this question in the simplest way possible. In general, the finally block is designed to execute regardless of whether an exception is thrown or handled in the try-catch blocks. Is there a case when finally block does not execute in Java? Before moving to the question, it is necessary to discuss ... Read More