0% found this document useful (0 votes)
6 views4 pages

Java Lab 6

The document outlines a Java programming lab focused on file handling, including creating, writing, appending, reading, counting characters, and displaying file information. It provides algorithms and sample programs for each task, demonstrating the use of File, FileWriter, and FileReader classes. Additionally, it includes viva questions related to file handling concepts in Java.

Uploaded by

purabdon9
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)
6 views4 pages

Java Lab 6

The document outlines a Java programming lab focused on file handling, including creating, writing, appending, reading, counting characters, and displaying file information. It provides algorithms and sample programs for each task, demonstrating the use of File, FileWriter, and FileReader classes. Additionally, it includes viva questions related to file handling concepts in Java.

Uploaded by

purabdon9
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

Java Programming Lab

Experiment No: 6
Experiment Name:

File Handling in Java


Objective:

To implement and understand file handling concepts in Java including file creation, writing,
appending, reading, counting characters, and displaying file information.
Program 1 – Create File and Write Data

Algorithm:

1. Start the program


2. Create a File object
3. Use createNewFile() to create file
4. Create FileWriter object
5. Write data into file
6. Close the file
7. Display success message
8. Stop the program
Program:

import [Link].*;
public class CreateWriteFile {
public static void main(String[] args) throws Exception {
File file = new File("[Link]");
[Link]();
FileWriter writer = new FileWriter(file);
[Link]("Hello, this is file handling in Java.");
[Link]();
[Link]("Data written successfully");
}
}

Sample Output:

File created successfully


Data written successfully
Program 2 – Append Data into Existing File

Algorithm:

1. Start the program


2. Open FileWriter in append mode
3. Write additional data
4. Close the file
5. Stop the program
Program:

import [Link].*;
public class AppendFile {
public static void main(String[] args) throws Exception {
FileWriter writer = new FileWriter("[Link]", true);
[Link]("\nThis is appended data.");
[Link]();
[Link]("Data appended successfully");
}
}

Sample Output:

Data appended successfully


Program 3 – Read Data from File

Algorithm:

1. Start the program


2. Create FileReader object
3. Read file character by character
4. Display characters
5. Close the file
6. Stop the program
Program:

import [Link].*;
public class ReadFile {
public static void main(String[] args) throws Exception {
FileReader reader = new FileReader("[Link]");
int ch;
while((ch = [Link]()) != -1) {
[Link]((char)ch);
}
[Link]();
}
}

Sample Output:

Hello, this is file handling in Java.


This is appended data.
Program 4 – Count Characters in File

Algorithm:

1. Start the program


2. Open file using FileReader
3. Initialize counter
4. Read characters
5. Increment counter
6. Display total characters
7. Stop the program
Program:

import [Link].*;
public class CountCharacters {
public static void main(String[] args) throws Exception {
int count = 0;
FileReader reader = new FileReader("[Link]");
while([Link]() != -1) {
count++;
}
[Link]();
[Link]("Total Characters: " + count);
}
}

Sample Output:

Total Characters: 65
Program 5 – Display File Information

Algorithm:

1. Start the program


2. Create File object
3. Check if file exists
4. Display file details
5. Stop the program
Program:

import [Link].*;
public class FileInfo {
public static void main(String[] args) {
File file = new File("[Link]");
if([Link]()) {
[Link]("File Name: " + [Link]());
[Link]("File Size: " + [Link]());
}
}
}

Sample Output:

File Name: [Link]


File Size: 65
Viva Questions:

1. What is file handling in Java?


2. What is File class?
3. What is FileWriter?
4. What is FileReader?
5. What is append mode?
6. What is IOException?
7. How to check file existence?
8. What is file length method?

You might also like