0% found this document useful (0 votes)
11 views8 pages

Java File Handling and I/O Streams Guide

File handling in Java allows for permanent data storage and efficient management of large datasets through various operations such as creating, reading, writing, and deleting files. The File class and I/O streams (byte and character) are essential components for handling file operations, with byte streams managing binary data and character streams handling text data. Best practices include using buffered I/O for performance, ensuring proper exception handling, and closing file resources after use.

Uploaded by

Sathish Koppoju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Java File Handling and I/O Streams Guide

File handling in Java allows for permanent data storage and efficient management of large datasets through various operations such as creating, reading, writing, and deleting files. The File class and I/O streams (byte and character) are essential components for handling file operations, with byte streams managing binary data and character streams handling text data. Best practices include using buffered I/O for performance, ensuring proper exception handling, and closing file resources after use.

Uploaded by

Sathish Koppoju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

->File Handling helps a program save and use information permanently on the computer.

Why File Handling is Required?

To store data permanently instead of keeping it only in memory.

To read and write data from/to files for later use.

To share data between different programs or systems.

To organize and manage large data efficiently.

File Class

File class in Java (from the [Link] package) is used to represent the name and path of a file or
directory.

It provides methods to create, delete, and get information about files and directories.

Example:

// Importing File Class

import [Link];

class Geeks

public static void main(String[] args)

// File name specified

File obj = new File("[Link]");

[Link]("File instance Created!");

Output:
File instance Created!

I/O Streams in Java---------

In Java, I/O streams are the fundamental mechanism for handling input and output operations.

They provide a uniform way to read data from various sources (files, network, memory) and write
data to different destinations.

Java I/O streams are categorized into two main types based on the type of data they handle:

--------[Link] Streams-------

In Java, Byte Streams are used to handle raw binary data such as images, audio files, videos or any
non-text file.

They work with data in the form of 8-bit bytes.

byte_streams_

Byte Streams

The two main abstract classes for byte streams are:

InputStream: for reading data (input)

OutputStream: for writing data (output)

Since abstract classes cannot be used directly, we use

their implementation classes to perform actual I/O operations.

FileInputStream: reads raw bytes from a file.

FileOutputStream: writes raw bytes to a file.

BufferedInputStream / BufferedOutputStream: use buffering for faster performance.

ByteArrayInputStream: reads data from a byte array as if it were an input stream.


ByteArrayOutputStream: writes data into a byte array, which grows automatically.

----2. Character Streams-----

In Java, Character Streams are used to handle text data.

They work with 16-bit Unicode characters, making them suitable for international text and language
support.

character_streams

Character Stream

The two main abstract classes for character streams are:

Reader: Base class for all character-based input streams (reading).

Writer: Base class for all character-based output streams (writing).

Since abstract classes cannot be used directly, we use their implementation classes to perform actual
I/O operations.

FileReader: reads characters from a file.

FileWriter: writes characters to a file.

BufferedReader: reads text efficiently using buffering; also provides readLine() for reading lines.

BufferedWriter: writes text efficiently using buffering.

StringReader: reads characters from a string.

StringWriter: writes characters into a string buffer.

Use Byte Streams when working with binary data (images, audio, video, executable files)

and use Character Streams when working with text data (characters, strings, text files).

-----The following are the several operations that can be performed on a file in Java:-----

[Link] a File

[Link] from a File


[Link] to a File

[Link] a File

1. Create a File

In order to create a file in Java, you can use the createNewFile() method.

If the file is successfully created, it will return a Boolean value true and false if the file already
exists.

2. Write to a File

We use the FileWriter class along with its write() method in order to write some text to the
file.

3. Read from a File

We will use the Scanner class in order to read contents from a file.

4. Delete a File

We use the delete() method in order to delete a file.

[Link]();

[Link]();

[Link]();//check read only mode or not

[Link](true);//if read only mode is enable

-----------------------------------------------------------------------------------------------------

File Handling Concepts in Java


1. File Class

Belongs to [Link] package.

Represents file/directory path (not actual file content).

Common methods:

createNewFile() → creates a new file.

exists() → checks if file exists.

delete() → deletes file.

length() → size of file in bytes.

canRead() / canWrite() → permissions.

list() → lists files in a directory.

2. Streams

Streams = flow of data (input/output).

Byte Streams → handle raw binary data (e.g., images, PDFs).

FileInputStream, FileOutputStream.

Character Streams → handle text data (Unicode safe).

FileReader, FileWriter.
Interview Tip: Byte streams = 8-bit, Character streams = 16-bit (Unicode).

3. Buffered I/O

Wrappers around streams/readers/writers to improve performance.

Examples:

BufferedReader → reads text line by line (readLine()).

BufferedWriter → writes text with buffering (newLine()).

Advantage → reduces disk access (reads/writes chunks instead of char by char).

4. Reading from a File

Character-by-character → FileReader.

Line-by-line → BufferedReader.

Using Scanner → Scanner sc = new Scanner(new File("[Link]"));.

5. Writing to a File

FileWriter → overwrite by default.

FileWriter("[Link]", true) → append mode.

BufferedWriter → efficient writing.


Always close() or use try-with-resources.

6. Serialization (Advanced)

Used to save Java objects into a file (and retrieve them later).

Classes: ObjectOutputStream, ObjectInputStream.

Object must implement Serializable interface.

Example use case: Save user session or app state.

7. Exception Handling

File operations can throw IOException.

Use try-catch-finally OR try-with-resources for auto-close.

8. Practical Use Cases

Logging (store logs in .log file).

Configuration ([Link] files).

Report Generation (CSV, JSON, XML).

Bulk Data Import (read CSV before sending to DB).

Temporary Storage when DB isn’t needed.

9. Best Practices
Always close file resources (close() or try-with-resources).

Prefer BufferedReader/BufferedWriter for text.

Use append mode carefully to avoid overwriting.

For large files → read/write in chunks, not character by character.

Interview Booster:

They might ask:

Difference between FileWriter and BufferedWriter

Difference between byte stream & character stream

How to append vs overwrite

What is serialization in file handling

You might also like