Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7001237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:46:39+00:00 2026-05-27T20:46:39+00:00

In the code below I am trying to select xml files from an ArrayList

  • 0

In the code below I am trying to select xml files from an ArrayList xmls where xmls.get(i) is the absolute path of a file with extension .xml. If this file cannot be parsed by Document an exception is thrown and the file is moved to a different directory. However, I cannot move or delete the file although I can copy it to the directory destFile. The values of the last if statement return true for f.exists(), f.canRead(), f.canWrite(), f.canExecute but returns false forf.renameTo(destFile);

for(int i=0; i<xmls.size(); i++){
    boolean delete = false;

    try {
        File f = new File(xmls.get(i));
        File destFile = new File(structDir + "/badXMLs/" + f.getName());

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(f);
        doc.getDoctype();

    } catch (Exception e) {
        File f = new File(xmls.get(i));
        File destFile = new File(structDir + "/badXMLs/" + f.getName());

        System.out.println(f.getName());
        delete = true;
    } 

    if(delete){
        File f = new File(xmls.get(i));
        File destFile = new File(structDir + "/badXMLs/" + f.getName());

        System.out.println(f.exists());
        System.out.println(f.canRead());
        System.out.println(f.canWrite());
        System.out.println(f.canExecute());

        System.out.println(f.renameTo(destFile));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:46:40+00:00Added an answer on May 27, 2026 at 8:46 pm

    The default DocumentBuilder does not close the file when you get an Exception. The easiest way to avoid this is to use a FileInputStream, and close it yourself, like:

    FileInputStream fis = null;
    
    try {
        File f = new File(xmls.get(i));
        File destFile = new File(structDir + "/badXMLs/" + f.getName());
        fis = new FileInputStream(f);
    
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(fis); // parse the FileInputStream, not the File
        doc.getDoctype();
    
    } catch (Exception e) {
        if (fis != null) {
            fis.close();
        }
    
        File f = new File(xmls.get(i));
        File destFile = new File(structDir + "/badXMLs/" + f.getName());
    
        System.out.println(f.getName());
        delete = true;
    } 
    

    It doesn’t close it, so under windows at least, you can’t delete it or rename it. Note that you don’t need to create the DocumentBuilder each time either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select the content of MINRANGE of the below XML. This is
The code below is what I am trying to use in order to get
I am trying to use the code below to select all values of notableWork,
I am trying to execute the code below to list the select item in
I'm trying to figure out how to select the textarea in the code below
HI, I am trying the below code . $domain = $_GET['url']; $sql = 'SELECT
In the code below i`m trying to read a list of selected projects and
In the code below I'm trying to load some images and put them in
Afternoon all, Using the code below I'm trying to load what is render by
I'm trying to use the code below to send messages via System.Net.Mail and am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.