Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Cursling; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; /** * * @author P7109156 */ public class deleteNotification { static final Logger logger = Logger.getLogger(deleteNotification.class); convertPropsJson obj_convert = new convertPropsJson(); public static String input_notification_file_del; public static String check_notification = ""; public File storeUpdate; public File storeUpdate1; boolean isdeleted; public String _doDelete(String notification_name) { isdeleted = false; try { File inFile = new File(input_notification_file_del); if (!inFile.isFile()) { logger.info("Failed to Delete. Data file not found."); return ("failed"); } storeUpdate = new File(input_notification_file_del); File tempFile = new File(inFile.getAbsolutePath() + ".tmp"); BufferedReader br = new BufferedReader(new FileReader(input_notification_file_del)); PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); String line = null; while ((line = br.readLine()) != null) { if (!line.equalsIgnoreCase("")) { if (line.startsWith(notification_name + "=") || line.startsWith(notification_name + " =")) { isdeleted = true; pw.flush(); } else { pw.println(line); pw.flush(); } } else { //do nothing } } pw.close(); br.close(); //Rename the new file to the filename the original file had. if (tempFile.canRead()) { Long before = storeUpdate.lastModified(); FileUtils.copyFile(tempFile, storeUpdate); Long after = storeUpdate.lastModified(); if (before.equals(after)) { logger.info("failed to delete Notification Group " + notification_name); return ("failed"); } else if (isdeleted == true) { logger.info("Notification Group " + notification_name + " Deleted."); obj_convert._doConvert(); return ("deleted"); } else { logger.info("Failed to delete Notification Group " + notification_name + " . Notification Group not found."); return ("not_found"); } } else { logger.info("failed to delete Notification Group " + notification_name); return ("failed"); } } catch (FileNotFoundException ex) { logger.info("failed to delete Notification Group " + notification_name); logger.error("Error : " + ex); return ("failed"); } catch (IOException ex) { logger.info("failed to delete Notification Group " + notification_name); logger.error("Error : " + ex); return ("failed"); } } }