Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: File handling issue

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default File handling issue

    I'm writing a program that dynamically updates an xml file as it is being edited. It needs an add function, delete, and save, and so far, I've gotten only the add function working properly. I've got a delete method that works when I run only that method, but not when I run the program as a whole. I don't get any exceptions or errors and I'm trying to edit the file via printwriter. It creates a new temporary file with the new xml with the deleted section and then overwrites the current .xml file. It runs flawlessly when I run only the delete method, but not when I run the program as a whole.

    public static void removeFromFile(String file, String lineToRemove) {
     
    		try {
     
    			System.out.println("Removing:\t" + lineToRemove);
     
    		  File inFile = new File(file);
     
    		  if (!inFile.isFile()) {
    		    System.out.println("Parameter is not an existing file");
    		    return;
    		  }
     
    		  //Construct the new file that will later be renamed to the original filename.
    		  File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
     
    		  PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
    		  String text = "";
    		  Scanner sc = new Scanner(inFile);
    		  while(sc.hasNext())
    		  {
    			  text+=sc.nextLine() + "\n";
    		  }
    		  //System.out.println(text);
    		  text = text.replace(lineToRemove + "\n", "");
    		  sc.close();
    		  sc = new Scanner(text);
     
    		  while(sc.hasNext())
    			  pw.println(sc.nextLine());
     
    		  pw.flush();
    		  pw.close();
    		  sc.close();
     
    		  //Delete the original file
    		  if (!inFile.delete()) {
    		    System.out.println("Could not delete file");
    		  }
     
    		  //Rename the new file to the filename the original file had.
    		  if (!tempFile.renameTo(inFile))
    		    System.out.println("Could not rename file");
     
    		}
    		catch (FileNotFoundException ex) {
    		  ex.printStackTrace();
    		}
    		catch (IOException ex) {
    		  ex.printStackTrace();
    		  System.out.println("IO Exception: " + ex.getMessage());
    		}
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: File handling issue

    Can you describe the problem in more detail? What happens when the program does not do what you want? Can you give a sample showing the contents of the file before the test and after the test? Add some comments saying what is wrong with the results.

    --- Update ---

    What was the outcome for this thread?
    http://www.javaprogrammingforums.com...g-problem.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: File handling issue

    The problem is at this line.

    if (!inFile.delete()) {
    		    System.out.println("Could not delete file");
    		  }

    This is the line that deletes the original file in order for it to be replaced by the new version. This works when I run just the delete method, however, it does not work when I run the full program - via a GUI I wrote; it won't delete the file and therefore won't make the change. I haven't been able to figure that problem out yet.

    What was the outcome for this thread?
    File editing problem
    The problem was solved when I wrote the String's byte data instead of writing as UTF.

Similar Threads

  1. java servlet file handling
    By K.A Naqvi in forum Java Servlet
    Replies: 1
    Last Post: April 30th, 2013, 07:43 PM
  2. File Input and Output and Exception Handling
    By MagicTricksKill in forum File I/O & Other I/O Streams
    Replies: 21
    Last Post: January 21st, 2013, 07:52 PM
  3. java file handling
    By MartinMc in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 16th, 2012, 04:07 PM
  4. Regarding File Handling
    By ravjot28 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:45 PM
  5. File handling in java
    By srikanth in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 22nd, 2010, 02:11 AM