Hello,
I have a
Java program that reads a singe text file, modifies it, and then saves it back into the same directory by appending "_Modified.txt" to the original filename. Code is attached below.
I want to modify this code so that instead of entering just a single filename, I instead enter a directory name. The Java program will then look into this directory and modify every file that it finds there. The files will all have the same type of text content and format, but the number of text files found in this directory can vary each time I run the program, and the filenames can be drastically different (not following any naming convention, other than ending with ".txt"). Every file in this directory will need to be modified each time the program is run.
Can someone please tell me how to modify this Java program so that is will look into a single directory, and modify every file that it finds there? As each text file is modified, I want to resave it (as before) by appending "_Modified.txt" to the original filename.
Thanks,
Kevin.
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Scanner;
class Kevins_File_Manipulator {
public static void main (
String args []) throws IOException {
Scanner stdin = new Scanner (System.in);
System.out.print ("File name to read from : ");
String inputFileName = stdin.next();
Scanner sf = new Scanner (new File (inputFileName));
String outputFileName = inputFileName + "_Modified.txt";
System.out.println ("Opening for writing: " + outputFileName);
FileWriter fw = new FileWriter (outputFileName);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter pw = new PrintWriter (bw);
String line;
int count = 0;
// My code goes here that modifies each line of the text file and resaves it to the new file....
pw.close(); // finish and close the output text file
} // end main
} // end class