Java File Handling Part 1
Java File Handling
·
- Java I/O (Input and Output) is used to process the input and produce the output.
- Java uses the concept of stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations.
- We can perform file handling in java by Java I/O API.
Stream
A stream is a sequence of data. In Java a stream
is composed of bytes. It's called a stream because it is like a stream of water
that continues to flow.
In java, 3 streams are created for us
automatically. All these streams are attached with console.
- System.out: standard output stream
- System.in: standard input stream
- System.err: standard error stream
Byte
Streams
Java byte streams are used to perform
input and output of 8-bit bytes. Though there are many classes related to byte
streams but the most frequently used classes are, FileInputStream and FileOutputStream.
Character
Streams
Java Byte streams are used to perform input and
output of 8-bit bytes, whereas Java Character streams are used to perform input and
output for 16-bit unicode. Though there are many classes related to character
streams but the most frequently used classes are, FileReader and FileWriter. Though internally FileReader uses
FileInputStream and FileWriter uses FileOutputStream but here the major
difference is that FileReader reads two bytes at a time and FileWriter writes
two bytes at a time.
Here is a hierarchy of classes to deal
with Input and Output streams.
OutputStream
Java application uses an output stream to
write data to a destination, it may be a file, an array, peripheral device or
socket.
InputStream
Java application uses an input stream to
read data from a source, it may be a file, an array, peripheral device or
socket.
Directories in
Java
A directory is a File which can contain a
list of other files and directories. You use File object to create directories, to list down
files available in a directory. For complete detail, check a list of all the
methods which you can call on File object and what are related to directories.
Creating Directories
There are two useful File utility methods, which can be used to
create directories –
·
The mkdir(
) method
creates a directory, returning true on success and false on failure. Failure
indicates that the path specified in the File object already exists, or that
the directory cannot be created because the entire path does not exist yet.
·
The mkdirs() method creates both a directory and all
the parents of the directory.
Java FileOutputStream Example
import
java.io.*;
public class
Test {
public static void main(String[] args) {
File f1 = new
File("D:/text.txt");
try {
if (f1.createNewFile())
System.out.println("File
Created.");
FileOutputStream fout = new
FileOutputStream(f1);
fout.write(97);
String s = "Welcome
to Saurashtra.";
byte b[] =
s.getBytes();// converting string into byte array
fout.write(b);
} catch
(IOException e) {
System.out.println("IO
Error Generated");
}
System.out.println("Name
of File:" + f1.getName());
System.out.println("Length
of File:" + f1.length());
System.out.println("Abs.
path of File:" + f1.getAbsolutePath());
}
}
Output:
File Created.
Name of File:text.txt
Length of File:23
Abs. path of File:D:\text.txt
Java FileInputStream example
import
java.io.*;
public class
Test2 {
public static void main(String[] args) {
// TODO
Auto-generated method stub
File f1 = new
File("D:/test.txt");
try {
FileInputStream fin = new
FileInputStream(f1);
// read one character
int i = fin.read();
System.out.println((char) i);
// read all character
i =
0;
while ((i = fin.read())
!= -1) {
System.out.print((char) i);
}
} catch
(IOException e) {
System.out.println("IO
Error Generated");
}
}
}
Output:
J
ava File Handling
Note: text file test.txt must exist with content “Java File
Handling”
Java BufferedOutputStream Class
Java BufferedOutputStream class is used for
buffering an output stream. It internally uses buffer to store data. It adds
more efficiency than to write data directly into a stream. So, it makes the
performance fast.
Java BufferedInputStream Class
Java BufferedInputStream class is used to
read information from stream. It internally uses buffer mechanism to make the
performance fast.
The important points about
BufferedInputStream are:
- When the
bytes from the stream are skipped or read, the internal buffer
automatically refilled from the contained input stream, many bytes at a
time.
- When a
BufferedInputStream is created, an internal buffer array is created.
Example for BufferedOutputStream and BufferedInputStream Class
import
java.io.*;
public class
BIOC {
public static void
main(String[] args) {
try {
FileOutputStream fout = new
FileOutputStream("D:\\test.txt");
BufferedOutputStream
bout = new BufferedOutputStream(fout);
String s = "Welcome
to Java.";
byte b[] =
s.getBytes();
bout.write(b);
bout.flush();
bout.close();
fout.close();
} catch
(FileNotFoundException e) {
System.out.println("File
Not Found");
} catch
(IOException e) {
System.out.println("Error
in File Writing");
}
try {
FileInputStream fin = new
FileInputStream("D:\\test.txt");
BufferedInputStream
bin = new BufferedInputStream(fin);
int i;
while ((i = bin.read())
!= -1) {
System.out.print((char) i);
}
System.out.println("");
bin.close();
fin.close();
} catch
(Exception e) {
System.out.println(e);
}
System.out.println("Success");
}
}
Output:
Welcome to Java.
Success
Comments
Post a Comment