/* * Read in a text file using Scanner the split method * Manipulate the date and flag * Write new data to a new text file using PrintWriter */ package readingWritingTextFile; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class ReadingWritingUI { public static void main(String[] args) { try { ReadingWritingManager mw = new ReadingWritingManager(); } catch (IOException ex) { Logger.getLogger(ReadingWritingUI.class.getName()).log(Level.SEVERE, null, ex); } } } ==================================================================================================== /* * Read in the date and format it to default * Add 45 days to the date * Read in the flag and compliment it. * Write the new data to a new file. */ package readingWritingTextFile; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class ReadingWritingManager { PrintWriter pw = null; String[] tokenArr = null; public ReadingWritingManager() throws IOException { boolean flag = false; try { // The input file and the output file Scanner scFile = new Scanner(new File("textFile.txt")); pw = new PrintWriter(new FileWriter("newData.txt")); while(scFile.hasNextLine()){ String line = scFile.nextLine(); tokenArr = line.split("#"); LocalDate theDate = LocalDate.parse(tokenArr[0], DateTimeFormatter.ofPattern("dd-MM-yyyy")); LocalDate newDate = theDate.plusDays(45); // complement the boolena flag if(tokenArr[1].equals("true")) flag = false; else if(tokenArr[1].equals ("false")) flag = true; else System.out.println("Error with flag"); // Parse date and flag to String pw.println("" + newDate + "#" + flag); } pw.close(); System.out.println("Reading Write successful"); } catch (FileNotFoundException ex) { Logger.getLogger(ReadingWritingManager.class.getName()).log(Level.SEVERE, null, ex); System.out.println("Error finding file"); } } } ========================================================================================================= TEXT File - INPUT 14-09-2023#true 17-10-2022#false 26-01-2023#false 09-09-2022#true ========================================================================================================= OUTPUT TO NEW File 2023-10-29#false 2022-12-01#true 2023-03-12#true 2022-10-24#false