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

? Data File Handling in Java Final Comprehensive Notes (ISC)

The document provides comprehensive notes on data file handling in Java, covering fundamental concepts such as data organization, file handling essentials, and the File class. It includes details on Java I/O classes for both character and byte streams, common exceptions, and practical examples of file operations. Additionally, it features practice questions and answers to reinforce understanding of the material.

Uploaded by

rajsingh132608
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)
3 views8 pages

? Data File Handling in Java Final Comprehensive Notes (ISC)

The document provides comprehensive notes on data file handling in Java, covering fundamental concepts such as data organization, file handling essentials, and the File class. It includes details on Java I/O classes for both character and byte streams, common exceptions, and practical examples of file operations. Additionally, it features practice questions and answers to reinforce understanding of the material.

Uploaded by

rajsingh132608
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

💾 Data File Handling in Java: Final Comprehensive

Notes (ISC) (By : Sarfraz Sir)


1. Fundamentals of Data Organization
Term Definition Example (Student Data)

Data The raw facts and figures, or unorganized 15, "Amit", "Mumbai"
text and numbers.

Field A single piece of information, or an Name (Amit), Age (15), City


attribute, related to an entity. (Mumbai)

Record A collection of related fields treated as a A single row containing a student's


single unit (one complete entity). Name, Age, and City.

Data File A collection of related records stored on A file named [Link] containing
a persistent storage device. the records of all students.

2. File Handling Essentials


●​ Package: All necessary classes are in [Link] (import [Link].*;).
●​ Streams: A flow of data between the program and the file.
○​ Input Stream: Program $\leftarrow$ File (Reading)
○​ Output Stream: Program $\rightarrow$ File (Writing)

3. The File Class


Unlike Stream classes (which handle the content of a file), the File class is used to manage the
metadata (properties) of the file itself.

Important Methods of the File Class:

Method Return Type Description


exists() boolean Checks if the file or directory actually exists.

getName() String Returns the name of the file or directory.

getPath() String Returns the path of the file.

length() long Returns the size of the file in bytes.

delete() boolean Deletes the file or directory.

createNewFile() boolean Creates a new, empty file if it doesn't already exist.

isFile() boolean Checks if the object is a file (not a folder).

delete() boolean Deletes the file from the disk permanently.

renameTo(File) boolean Renames the file to a new name provided as a File object.

Example of File Class Usage:

Java
File f = new File("[Link]");
if ([Link]()) {
[Link]("File Name: " + [Link]());
[Link]("Size: " + [Link]() + " bytes");
} else {
[Link]();
[Link]("File created.");
}

4. Java I/O Classes by Stream Type


A. Character Stream Classes (For Text Files - ISC Focus)

Operation Class Purpose Key Method/Function Used for

Writing FileWriter Connects to write(char[]) Basic


the file. connection.

Writing BufferedWriter Buffers write(String) , Speed and


output for newLine() cleaner line
efficient breaks.
writing.

Writing PrintWriter Writes print(data) , Convenient,


formatted data println(data) line-by-line text
easily. output.

Reading FileReader Connects to read() (returns int Basic


the file. ASCII) connection.

Reading BufferedReader Buffers input readLine() (returns Efficient


for efficient String, or null at EOF) line-by-line input.
reading.

B. Byte Stream Classes (For Binary Files)

Operation Class Purpose Key Used for


Method/Function
Writing FileOutputStream Writes raw bytes. write(int b) , Non-text files
write(byte[] b) (images, audio).

Reading FileInputStream Reads raw bytes. read() (returns int Non-text files.
byte value, or -1
at EOF)

Writing DataOutputStream Writes primitive writeInt() , Serialization.


data types (int, writeUTF()
float, boolean) in a
binary format.

Reading DataInputStream Reads primitive readInt() , Deserialization.


data types written readUTF()
by
DataOutputStream.

5. Common Exceptions in File Handling


Exception Cause Handling

IOException A general input/output error (e.g., Added to main


disk full, file corrupted). signature: throws
IOException

FileNotFoundException The file specified does not exist on A type of IOException.


the disk.

EOFException Attempting to read past the end of a Handled by catching the


file (common with DataInputStream). exception.
📜 Demo Programs (Functions for Each Operation)
Java
import [Link].*;

class FileOperationDemos {

// --- 1. Character Stream: Writing Text Data ---


public static void writeText(String fileName) throws IOException {
[Link]("\n--- Writing Text to " + fileName + " ---");
PrintWriter pw = new PrintWriter(new FileWriter(fileName, true));
[Link]("Hello from ISC Class 11!");
[Link]("The year is " + 2025);
[Link]();
[Link]("Text data written successfully.");
}

// --- 2. Character Stream: Reading Text Data ---


public static void readText(String fileName) throws IOException {
[Link]("\n--- Reading Text from " + fileName + " ---");
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = [Link]()) != null) {
[Link]("Read: " + line);
}
[Link]();
}

// --- 3. Byte Stream: Writing Binary Data ---


public static void writeBinary(String fileName) throws IOException {
[Link]("\n--- Writing Binary Data to " + fileName + " ---");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName));
[Link](12345);
[Link](true);
[Link]("Binary Record");
[Link]();
}

// --- 4. Byte Stream: Reading Binary Data ---


public static void readBinary(String fileName) throws IOException {
[Link]("\n--- Reading Binary Data from " + fileName + " ---");
DataInputStream dis = new DataInputStream(new FileInputStream(fileName));
[Link]("Int: " + [Link]());
[Link]("Boolean: " + [Link]());
[Link]("String: " + [Link]());
[Link]();
}

public static void main(String args[]) throws IOException {


writeText("[Link]");
readText("[Link]");
writeBinary("[Link]");
readBinary("[Link]");
}
}

❓ Practice Questions
A. Multiple Choice Questions (MCQ)
1.​ Which method of the File class returns the size of the file in bytes?​
a) size()​
b) length()​
c) volume()​
d) capacity()
2.​ If the file specified in new FileReader("[Link]") does not exist, which exception is
thrown?​
a) EOFException​
b) IOException​
c) FileNotFoundException​
d) ArithmeticException
3.​ The primary purpose of serialization (using DataOutputStream) is to:​
a) Convert binary data to text.​
b) Save data structures in a way they can be perfectly reconstructed.​
c) Read a file line by line.​
d) Delete a file.
4.​ Which method of BufferedReader should you check against null to determine the end of
a text file?​
a) read()​
b) available()​
c) readLine()​
d) next()
5.​ What is the correct flow for efficiently writing text data to a file?​
a) PrintWriter $\rightarrow$ FileWriter​
b) FileWriter $\rightarrow$ PrintWriter​
c) BufferedWriter $\rightarrow$ FileWriter $\rightarrow$ PrintWriter​
d) FileWriter $\rightarrow$ BufferedWriter
6.​ Which class is used to write Java primitive data types directly in a binary format?​
a) FileWriter​
b) DataOutputStream​
c) BufferedWriter​
d) Scanner
7.​ The term used to describe the flow of data from a file to a program is:​
a) Output Stream​
b) Input Stream​
c) Local Stream​
d) Data Stream
8.​ Which File class method is used to create a new file on the disk?​
a) initFile()​
b) makeFile()​
c) createNewFile()​
d) openFile()
9.​ A Record is defined as:​
a) A large chunk of raw data.​
b) A collection of related fields.​
c) A single character.​
d) A list of file names.
10.​If a FileWriter is created as new FileWriter("[Link]"), what is the default behavior?​
a) Appends data.​
b) Throws an exception.​
c) Overwrites existing content.​
d) Closes the file.

B. Assertion and Reason (A/R)


1.​ Assertion (A): The File class can be used to check if a file exists without opening it for
reading.​
Reason (R): The File class manages file properties and metadata, not the actual data
stream.​
(Answer: a)
2.​ Assertion (A): The close() method must be called on all stream objects.​
Reason (R): Failure to call close() can result in data loss for buffered streams.​
(Answer: a)
3.​ Assertion (A): BufferedReader is typically used for text files, whereas FileInputStream is
used for binary files.​
Reason (R): BufferedReader works with 16-bit characters, while FileInputStream works
with 8-bit bytes.​
(Answer: a)
4.​ Assertion (A): The exists() method of the File class returns an integer.​
Reason (R): It is used to check the presence of a file on the storage device.​
(Answer: d)
5.​ Assertion (A): Using new FileWriter("[Link]", true) opens the file in append mode.​
Reason (R): The second boolean parameter true signifies that data should be added to
the end of the file.​
(Answer: a)

C. Programming Questions
1.​ Record Writing (Text): Write a Java function writeStudent(String name, int age) that
writes a record (Name,Age) to "student_data.txt" in append mode.
2.​ Reading and Calculating (Text): Write a program to read records from "[Link]"
(format: Item,Price) and display the total sum of all prices.
3.​ File Management: Write a Java program that checks if a file named "[Link]" exists.
If it does, display its size; otherwise, create it.
4.​ Binary Write & Read: Write a Java function saveConfiguration() that uses
DataOutputStream to save int level and boolean sound_on to a binary file.
5.​ Character Filtering: Write a program that reads "[Link]" and counts how many lines
begin with a vowel (A, E, I, O, U).

✅ Answers to MCQ
1.​ b | 2. c | 3. b | 4. c | 5. d | 6. b | 7. b | 8. c | 9. b | 10. c

You might also like