Can perform the following operations: Create a File, Get File Information, Write to a File, Read from a File, Delete a File
Java File Class
An abstract representation of file and directory pathname. A pathname can be either absolute or relative.
Creating a File Object
A File object is created by passing in a string that represents the name of a file, or another File object
Java File Class Methods
The File class has a number of methods that allow us to work with and manipulate files on the file system
File class cannot modify or access the contents of the file it represents
Reader Class
Used to read the 16-bit characters from the input stream
It is an abstract class and can't be instantiated
There are various subclasses that inherit the Reader class and override the methods of the Reader class
Reading a text file in Java
There are several ways to read a plain text file in Java
Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability
Using BufferedReader class
Reads text from a character-input stream
Uses a buffer for efficient reading of characters, arrays, and lines
The buffer size may be specified, or the default size may be used
Checking End of File/Stream (EOF)
readLine() returns null
read() returns -1
Using FileReader class
Used for reading character files
The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate
Creating a FileReader
FileReader input = new FileReader(String name)
FileReader input = new FileReader(File fileObj)
Using Scanner class
Used if you want to read file line by line or based on some java regular expression
Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace
The resulting tokens may then be converted into values of different types using the various next methods (nextInt(), nextDouble(),etc)
The hasNext() verifies whether the file has another line and the nextLine() method reads and returns the next line in the file
Using FileInputStream class
Used for reading byte-oriented data (streams of raw bytes) such as image data, audio, video etc.
Can also read character-stream data
Reading CSV File in Java
The CSV stands for Comma-Separated Values
The CSV file format is used when we move tabular data between programs that natively operate on incompatible formats
The default separator of a CSV file is a comma (,)
The BufferedReader class can be used to read it line by line, and the String.split() method can be used to split each line by comma to convert it into columns
Creating a file
To create a file in Java, you can use the createNewFile() method
This method returns a boolean value: true if the file was successfully created, and false if the file already exists
The method is enclosed in a try...catch block to handle IOException
Options in Writing to a File
FileWriter
BufferedWriter
PrintWriter
FileOutputStream
FileWriter
The simplest way to write a file in Java
Writes directly into Files and should be used only when the number of writes is less
BufferedWriter
Almost similar to FileWriter but uses internal buffer to write data into File
Must be used when the number of write operations is more
PrintWriter
Can be used to write formatted text to a file
FileOutputStream
Used for writing byte-oriented data (streams of raw bytes) such as image data, audio, video etc.
File - a named location used to store related information
File I/O -
File I/O (input/output) means reading and writing the file contents
Java uses concept of a stream to make I/O operation fast
The java.io package contains all the classes required for input and output operations
Stream -
A series of data is referred to as a stream
In Java, Stream is classified into two types:
Byte Stream
Character Stream
Byte Stream -
it is mainly involved with byte data
a file handling process with a byte stream is a process which an input is provided and executed with the byte data
Character Stream -
it is mainly involved with character data
a file handling process which a character stream is a process in which an input is provided and executed with the character data
Java File Class -
The File Class is an abstract representation of file and directory pathname. A pathname can be either absolute or relative
The File class gives us the ability to work with files and directories on the file system
File Operation in Java -
In Java, a File is an abstract data type
We can perform the following operations on a file
Create a File
Get File Information
Write to a File
Read from a File
Delete a File
Creating a File Object - A File Object is created by passing in a string that represent the name of a file, or another file object
getName()
method is used to find the file name
return type is String
createNewFile()
method is used to create a new empty file
return type is boolean
delete()
method that is used to delete a file
return type is boolean
canRead()
method is used to check whether we can read the data of the file or not
return type is boolean
canWrite()
method that is used to check whether we can write the data into the file or not
return type is boolean
exists()
method that is used to check whether the specified file is present or not
return type is boolean
getAbsolutePath()
method is used to get the absolute pathname of the file
return type is String
length()
method is used to get the size of the file in bytes
return type is long
Reader Class
is is used to read the 16-bit characters from the input stream
it is an abstract class and can't be instantiated
there are various subclass that inherit the Reader class and override the methods of the Reader class
all methods of the Reader class throw an IOException
CharArrayReader - provided methods to read characters from the char array
FilterReader - provides methods to read characters from the underlying character input stream
InputStreamReader - provides methods to convert bytes to characters