note: You can easily replace a portion of text within a String using the String method "replace()". For example: String svar = "raspberry jam"; svar = svar.replace("jam", "pie"); System.out.println(svar); will print out: raspberry pie A photographer is organizing a photo collection about the national parks in the US and would like to annotate the information about each of the photos into a separate set of files. Write a program that reads the name of a text file containing a list of photo file names. The program then reads the photo file names from the text file, replaces the "_photo.jpg" portion of the file names with "_info.txt", and outputs the modified file names. Assume the unchanged portion of the photo file names contains only letters and numbers, and the text file stores one photo file name per line. If the text file is empty, the program produces no output.
You can easily replace a portion of text within a String using the String method "replace()". For example:
String svar = "raspberry jam"; svar = svar.replace("jam", "pie"); System.out.println(svar);will print out:
raspberry pieA photographer is organizing a photo collection about the national parks in the US and would like to annotate the information about each of the photos into a separate set of files. Write a
Assume the unchanged portion of the photo file names contains only letters and numbers, and the text file stores one photo file name per line. If the text file is empty, the program produces no output.
Algorithm:
Define the input filename (e.g., "ParkPhotos.txt").
Try opening and reading the input file using a BufferedReader to handle file input.
Each line in the input file is looped through:
- Read a line from the file.
- Replace "_photo.jpg" with "_info.txt" in the line.
- Print the modified filename.
In case an IOException materializes when reading a file, handle it.
The program is now complete.
Step by step
Solved in 4 steps with 3 images