8.file Handling Notes
8.file Handling Notes
2) Data Types
3) Variables
4) Operators
5) Control Statements
6) Arrays
7) String, String Buffer, StringBuilder
8) Command Line Arguments
9) Classes
10) Objects
11) Variables (non-static, static & local)
12) Methods ( method params & method return types)
13) Constructors
14) Blocks ( IB and SB)
15) Encapsulation
16) Inheritence
17) Polymorphism
18) Method Overloading
19) Method Overriding
20) Abstraction
21) Interfaces
22) Abstract Classes
23) Marker Interfaces
24) Variable Arguments
25) static keyword
26) this keyword
27) super keyword
28) final keyword
29) java.lang.Object class & method
30) Wrapper Classes
31) Type Casting
32) Packages
33) static import
34) Exception Handling
35) try-catch-finally- throws-throw
36) Exceptions Hierarchy
37) Checked Exceptions
38) Un-Checked Exceptions
1) File Handling
2) Multi-Threading
3) Collections
4) Generics
5) Inner Classes
6) Reflection API
7) Garbage Collection
8) Java 1.8 features
============
File Handling
===========
-> File Handling is Very important area in every programming language
1) Create a file
2) Write the data to file
3) Read the data from file
4) Delete the file
-> To perform File operations java language provided one predefined class
java.io.File
-> java.io package contains set of classes & interfaces to perform input and output
operations
import java.io.File;
import java.io.IOException;
// Java program to display all the files and directories in given path
import java.io.File;
import java.io.IOException;
Ex:
File : abx.txt
File: demo.txt
Directory: one
Directory: two
package in.ashokit;
import java.io.File;
import java.io.IOException;
if (f1.isFile()) {
System.out.println("File :: " + name);
}
if (f1.isDirectory()) {
System.out.println("Directory :: " + name);
}
}
}
}
===============
IO Streams
===============
-> Using I/O streams we can establish link between java program and file (physical
file )
write
java program <--------------------> file
read
java program <-----------------------> file
fw.close( ) ;
}
import java.io.*;
while ( i != -1 ){
System.out.print( (char) i );
i = fr.read ( ); // read next character and re-initialize i var
}
fr.close ( );
}
import java.io.*;
Note: FileReader will read the data character by character where as BufferedReader
will read the data Line by Line.
2) Write a java program to read 2 files data and write 2 files content into 3rd
file.
==========
PrintWriter
==========
System.out.println ("hi");
package in.ashokit;
import java.io.PrintWriter;
pw.flush();
pw.close();
}
}
package in.ashokit;
import java.io.PrintWriter;
1) File
2) File class methods
3) FileWriter
4) FileReader
5) BufferedReader
6) PrintWriter
==========================
Serialization & De-Serialization
==========================
-> When we store data in the object, that data will be available if our program is
running. If our program got terminated then we will loose our objects and data
available in the objects.
-> If we don't want to loose the data even after program got terminated then we
should go for Serialization.
-> The process of converting java object into file data in the form of bits and
bytes is called as Serialization.
-> The process of converting file data back to java object is called De-
Serialization.
package in.ashokit;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
*/
private static final long serialVersionUID = -100l;
int id;
String name;
public static void main(String[] args) throws Exception {
System.out.println("==========De-Serialization Started==========");
System.out.println("==========De-Serialization Ended==========");
========================
What is SerialVersionUID ?
========================
-> For every .class file JVM will assign one random number that is called as
serialVersionUID.
-> When we serialize the object, JVM will assign .class file serialVersionUID to
serialized file
-> When we de-serialize JVM will compare serialized file UID and .class file UID.
If both ids are matching then only de-serialization will happen otherwise it will
throw INvalidClassException.
-> To overcome this problem we can write our own serialVersionUID then jvm will not
assign that.
================
transient keyword
================
-> If we have any sensitive / secret data then we shouldn't serialize those fileds.
/**
*
*/
private static final long serialVersionUID = -100l;
int id;
String name;
String email;
transient String pwd;
Note: If we serialize the above person class object, id, name and email will be
serialized and pwd will not get serialized because it is transient variable.
package in.ashokit;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
*/
private static final long serialVersionUID = -100l;
int id;
String name;
String email;
transient String pwd;
System.out.println("==========De-Serialization Started==========");
System.out.println("==========De-Serialization Ended==========");