/* * Read in a text file using Scanner and useDelimiter * 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; public ReadingWritingManager() throws IOException { boolean flag, newFlag; 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(); Scanner scLine = new Scanner(line).useDelimiter("#"); String dateSt = scLine.next(); LocalDate theDate = LocalDate.parse(dateSt, DateTimeFormatter.ofPattern("dd-MM-yyyy")); LocalDate newDate = theDate.plusDays(45); flag = scLine.nextBoolean(); // Compliment the boolean flag if(flag == true) newFlag = false; else newFlag = true; // Parse date and flag to String pw.println("" + newDate + "#" + newFlag); } 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