Java: renameTo() function failing
<p>Java</p>

<p>The code below was written to read all files in, and send the data to another method (setOutput()), and then call a method to rename the last read file to another directory, and then delete the original.  Everything seems to work up until the smdrCleanup() method is called.  The renameTo() is failing.</p>

<p>From what I understand, if a FileReader is wrapped in a BufferedReader, I only need to call BufferedReader.close() to release the last read file... which I am doing here.</p>

<p>I have also seen where if the file were still "open", being scanned by anti-virus programs, or otherwise locked by a process, the renameTo() function would fail.  I have used Process Explorer to review what may have it locked, and I don't see anything locking it.</p>

<p>I have my method setup to throw any kind of IOExceptions, but I am not getting any exceptions.  Everything runs, but the console merely says that the file was not copied.</p>

<p>I am running Eclipse Helios Release 2, Windows 7 Ultimate, local administrator, UAC disabled.</p>

<p>Any help would be greatly appreciated.</p>

<pre><code>public void smdrReader(String path, String oldPath) throws IOException
{
  output = null; //nullify the value of output to avoid duplicate data
  File folder = new File(path); //setting the directory for raw data files
  File[] listOfFiles = folder.listFiles(); //array of files within the folder/directory

  //For loop to iterate through the available files, open, &amp; read contents to String Buffer.
  for (int i = 0; i &lt; listOfFiles.length; i++) 
  {

      if (listOfFiles[i].isFile()) //verifying next entry in array is a file
      {
          File fileName = new File(listOfFiles[i].getName());//getting file name from array iteration
          StringBuffer fileData = new StringBuffer(2048);//establishing StringBuffer for reading file contents into
          BufferedReader reader = new BufferedReader(new FileReader(path + fileName));//reader to actually access/read file
          String readData = String.valueOf(reader);//String variable being set to value of the reader
          fileData.append(readData);//appending data from String variable into StringBuffer variable
          setOutput(fileData);//setting the value of "output" to the value of StringBuffer
          fileData.delete(0, i);
          reader.close();//closing the reader (closing the file)
          smdrCleanup(oldPath,fileName.toString());//calling method to move processed file and delete original
      }

  }

}

//method to rename input file into another directory and delete the original file to avoid duplicate processing
public void smdrCleanup(String oldPathPassed, String fileNamePassed) throws IOException   
{
   File oldFile = new File(oldPathPassed);//establishing File object for path to processed folder
   File fileName = new File(fileNamePassed);//establishing File object for the fileName to rename/delete
   String oldFilePath = oldFile.toString();
   boolean success = fileName.renameTo(new File(oldFilePath + "\\" + fileName + ".old"));//attempting to rename file
    if (!success) //checking the success of the file rename operation
    {
        System.out.println("The File Was NOT Successfully Moved!");//reporting error if the file cannot be renamed
    }
        else
        {
            fileName.delete();//deleting the file if it was successfully renamed
        }
}
</code></pre>
