In: Computer Science
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ChangeTextCase {
public static void main(String[] args) {
File file = new File("input.txt");
try {
Scanner fin = new Scanner(file);
PrintWriter pw = new PrintWriter("output.txt");
String line, modified;
while (fin.hasNextLine()) {
line = fin.nextLine();
modified = "";
for (int i = 0; i < line.length(); i++) {
if (Character.isLowerCase(line.charAt(i))) {
modified += Character.toUpperCase(line.charAt(i));
} else {
modified += Character.toLowerCase(line.charAt(i));
}
}
pw.println(modified);
}
System.out.println("Modified file is written to output.txt");
pw.close();
fin.close();
} catch (FileNotFoundException e) {
System.out.println(file.getAbsolutePath() + " is not found!");
}
}
}