File Operation and Handling
File Operation and Handling
Writer classes
When to use…
• Character stream over Byte stream?
• Suitable when we want to process the text files
• Byte stream over Character stream?
• Suitable when we want to process raw data like binary files
• This method returns a Boolean value based on the creation of the file
2. Getting information about file
Method Return type Description
canRead() Boolean To check whether we can read the data of the file or not.
createNewFile() Boolean To create a new empty file.
canWrite() Boolean To check whether we can write the data into the file or not.
exists() Boolean To check whether the specified file is present or not.
delete() Boolean To delete a file.
getName() String To find the file name.
getAbsolutePath() String To get the absolute pathname of the file.
length() Long To get the size of the file in bytes.
list() String[] To get an array of the files available in the directory.
mkdir() Boolean To create a new directory.
Writing on file
• Writer class is superclass that writes the stream of characters
• In order to use the functionality of Writer, we use its subclasses:
• BufferedWriter - similar to FileWriter but it uses internal buffer to write data into file. It is use when
the number of write operations are large. For example writing a 1000 names, it is preferable to use
BufferedWriter than FileWriter
• FileOutputStream - to write raw stream data (byte). Useful for image processing
• FileWriter - the simplest way to write a file. It provides write method to write int, byte array and
String to the file. It writes directly into the file and only be used when the number of writes is less
• PrintWriter - converts primitive datatype into the text format and then writes to the file.
Writer
Reader