import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class CheckInFile { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter input file name: "); File file = new File(in.nextLine()); try { Scanner fin = new Scanner(file); PrintWriter pw1 = new PrintWriter("ofmy.txt"); PrintWriter pw2 = new PrintWriter("there.txt"); boolean ofmy = false, there = false; String line; while (fin.hasNextLine()) { line = fin.nextLine().toLowerCase(); if (line.contains("of my")) { ofmy = true; } if (line.contains("there")) { there = true; } } if (ofmy) { pw1.println("of my is in " + file.getAbsolutePath()); } else { pw1.println("of my is not in " + file.getAbsolutePath()); } if (there) { pw2.println("there is in " + file.getAbsolutePath()); } else { pw2.println("there is not in " + file.getAbsolutePath()); } System.out.println("statuses of my and there have been written to ofmy.txt and there.txt"); fin.close(); pw1.close(); pw2.close(); } catch (FileNotFoundException e) { System.out.println(file.getAbsolutePath() + " is not found!"); } in.close(); } }
Get Answers For Free
Most questions answered within 1 hours.