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

Java File Editor Program Guide

Uploaded by

rasikakharape
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)
5 views4 pages

Java File Editor Program Guide

Uploaded by

rasikakharape
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

Experiment no -08

[Link] file name as input to your program through command line, if file exists the open
and display contents of the file. After displaying contents of file ask user – [Link] you want
to add the data at the end of file or 2. replace specified text in file by other text. Based on
user’s response, then accept data from user and append it to file. If file in not existing
then create a fresh new-file and store user data into it. Also user should type exit on new
line to stop the program. Do this program using Character stream classes.
import [Link].*;

import [Link];

public class FileEditor {

public static void main(String[] args) {

if([Link] == 0) {

[Link]("Please provide filename as a command line argument.");

return;

String filename = args[0];

File file = new File(filename);

Scanner sc = new Scanner([Link]);

try {

if([Link]()) {

// Display contents of the file

[Link]("File exists. Contents of the file:");

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

String line;

while ((line = [Link]()) != null) {

[Link](line);

}
[Link]();

// Ask user for action

[Link]("\nChoose an option:");

[Link]("1. Append data at the end of file");

[Link]("2. Replace specified text in the file");

[Link]("Enter 1 or 2: ");

int choice = [Link]([Link]());

if (choice == 1) {

appendData(file, sc);

} else if (choice == 2) {

replaceText(file, sc);

} else {

[Link]("Invalid choice!");

} else {

// File does not exist, create and write new data

[Link]("File does not exist. Creating new file and adding data.");

appendData(file, sc);

} catch (IOException e) {

[Link]("An error occurred: " + [Link]());

} finally {

[Link]();

private static void appendData(File file, Scanner sc) throws IOException {

FileWriter fw = new FileWriter(file, true); // append mode


BufferedWriter bw = new BufferedWriter(fw);

[Link]("Enter data to append to file (type 'exit' on a new line to stop):");

while (true) {

String input = [Link]();

if ("exit".equalsIgnoreCase([Link]())) {

break;

[Link](input);

[Link]();

[Link]();

[Link]("Data appended successfully.");

private static void replaceText(File file, Scanner sc) throws IOException {

// Read entire file content

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(fr);

StringBuilder content = new StringBuilder();

String line;

while ((line = [Link]()) != null) {

[Link](line).append([Link]());

[Link]();

[Link]("Enter text to be replaced: ");

String oldText = [Link]();

[Link]("Enter replacement text: ");

String newText = [Link]();


String updatedContent = [Link]().replace(oldText, newText);

// Write updated content back to file (overwrite mode)

FileWriter fw = new FileWriter(file, false);

BufferedWriter bw = new BufferedWriter(fw);

[Link](updatedContent);

[Link]();

[Link]("Text replaced successfully.");

You might also like