Program 1:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class StrReplOne extends Exception{
public static void main(String args[])
{
try
{
// simply opens the file of .txt format
File file = new File("D:\\a\\file1.txt");
// just like Scanner, BufferedReader reads data from a file
BufferedReader read = new BufferedReader(new
FileReader(file));
// create a temporary string to store each line "eachline"
// create another string to maintain the structure of the data from
text file "everyline"
String eachline = "", everyline = "";
// now read every line till end_of_line i.e one complete row, use
while
// the readline() reads the data from the text file
while((eachline = read.readLine()) != null)
{
// after each end_of_line, a new line should come, so use
"\r\n"
// "\r\n" is used to enter a new line in files i.e. carriage return
and new line
everyline += eachline + "\r\n";
}
// after all the line are read and stored, close the file
read.close();
// To replace a word everywhere in a file, use replaceAll()
// the "newtext" stores the same structured text file with replaced
names
String newtext = everyline.replaceAll("John", "Smith");
// To write a text file as it is, use FileWriter to open a
file
// and also use it to write the data
FileWriter write = new FileWriter("D:\\a\\file2.txt");
write.write(newtext);
// again close the file after the use
write.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Screenshot for program 1:
// you can change the d:\\a\\file1.txt to simply file1.txt
Program 2:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
public class StrReplTwo {
public static void main(String[] args) {
try {
// read input from the user
Scanner scan = new Scanner(System.in);
System.out.println("Enter employee number : ");
// next() is used to read input as string
String empno = scan.next();
// BufferedReader in = new BufferedReader(new
FileReader("D:\\a\\file3.txt"));
File file = new File("D:\\a\\file3.txt"); // or just
file3.txt
BufferedReader read = new BufferedReader(new
FileReader(file));
// we need a string to temporarily store each line and then check
the empno
String s;
while ((s = read.readLine()) != null) {
// split() is used to seperate each word by a delimiter
// "each[]" is now an array string that stores each word in a
line
String[] each = s.split(",");
//Since data format is employee number,first name,last
name,designation,salary
//each[0] holds the employee number, each[1] holds the first
name
//and each[4] holds the salary
if (each[0] == empno) {
System.out.println(each[1] + " " + each[4]);
//if we got the output, then no need to chech other lines, so break
the while
break;
} else {
//if no match found for empno, then this executes
System.out.println("no match found");
}
}
read.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
The screen shot for program 2:
// please make sure input file is created already
// COMMENT FOR DOUBTS
Lab exercise Computing III 9 1. There is text file (testl.txt) which contains multi-line data including...
Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...