In: Computer Science
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries at once). If two people have the same name, create a new file by appending an index for the new file. For example, if there was another John in the second file, you will create John-1.txt for the new personalized letter. Assume that all the files that you read/write are in the same directory or the project build path of this class.
example template of text file:
Dear <N>,
You are <A> years old and <G>, we have a gift for you. You have absolutely nothing to buy; just pay the shipping and handling charge of $19.99.
To claim your gift, call us immediately.
Thank you!
Example information in second file:
John 22 male
Sara 32 female
John 45 male
Amy 21 female
Jim 60 male
example output of saved file:
Dear john,
You are 45 years old and male, we have a gift for you. You have absolutely nothing to buy; just pay the shipping and handling charge of $19.99.
To claim your gift, call us immediately.
Thank you!
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: The files containing template data and person data must be placed in the current working directory or the root directory of your project and when the program is executed, enter those names correctly. Failing to do this properly will cause FileNotFoundException. If that happens, check the file path/names and try again properly.
// ReplacePlaceholders.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReplacePlaceholders {
// method to open a file, read the contents into a String variable and
// return it
static String readFile(File file) throws FileNotFoundException {
Scanner scanner = new Scanner(file);
String text = "";
while (scanner.hasNext()) {
text += scanner.nextLine() + "\n";
}
scanner.close();
return text.trim();
}
// method to save a text to a file with given name
static void saveFile(String text, String name) throws FileNotFoundException {
// creating a file object with name: name.txt
File file = new File(name + ".txt");
int i = 1;
// looping as long as file exists
while (file.exists()) {
// creating another file with index i
file = new File(name + "-" + i + ".txt");
i++;
}
// at this point, file does not exist, so we create a PrintWriter that
// will create the required file a
PrintWriter writer = new PrintWriter(file);
// writing data
writer.println(text);
// closing file, saving changes
writer.close();
}
public static void main(String[] args) throws FileNotFoundException {
// scanner to read file names from keyboard
Scanner scanner = new Scanner(System.in);
// asking for first file name (containing template)
System.out.print("Enter first file name: ");
String filename = scanner.nextLine();
File file1 = new File(filename);
// reading contents of file1 to text
String text = readFile(file1);
// prompting for second file name
System.out.print("Enter second file name: ");
filename = scanner.nextLine();
File file2 = new File(filename);
// re initializing scanner to read from file2
scanner = new Scanner(file2);
// looping through each record in file2
while (scanner.hasNext()) {
// copying text to a variable
String textForThisPerson = text;
// reading name, age and gender
String name = scanner.next();
int age = scanner.nextInt();
String gender = scanner.next();
// replacing placeholders with these values
textForThisPerson = textForThisPerson.replace("<N>", name);
textForThisPerson = textForThisPerson.replace("<A>", age + "");
textForThisPerson = textForThisPerson.replace("<G>", gender);
// saving updated data to file with name of the person
saveFile(textForThisPerson, name);
}
// closing file
scanner.close();
}
}
/*OUTPUT*/
Enter first file name: template.txt
Enter second file name: people.txt
/*screenshot of file structure after running the program*/