Java:renameTo() 函数失败

标签 java

Java

下面的代码是读取所有文件,并将数据发送到另一个方法(setOutput()),然后调用一个方法将最后读取的文件重命名到另一个目录,然后删除原始文件。在调用 smdrCleanup() 方法之前,一切似乎都正常。 renameTo() 失败。

据我了解,如果 FileReader 包装在 BufferedReader 中,我只需要调用 BufferedReader.close() 来释放最后读取的文件......我在这里正在这样做。

我还发现,如果文件仍然“打开”、被防病毒程序扫描或被进程锁定,则 renameTo() 函数将会失败。我已使用 Process Explorer 查看了可能锁定它的内容,但没有看到任何锁定它的内容。

我的方法设置为抛出任何类型的 IOException,但我没有收到任何异常。一切都运行了,但控制台只是说文件没有被复制。

我正在运行 Eclipse Helios Release 2、Windows 7 Ultimate、本地管理员、禁用 UAC。

任何帮助将不胜感激。

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, & read contents to String Buffer.
  for (int i = 0; i < 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
        }
}

最佳答案

oldFile.toString(); 返回文件的完整路径,包括文件名,因此如果您的旧文件路径是 c:\path\to\file.txtoldFilePath + "\\"+ fileName + ".old" 将是 c:\path\to\file.txt\file.txt.old

由于没有文件夹c:\path\to\file.txt,因此失败。将其更改为

boolean success = fileName.renameTo(new File(oldFilePath + ".old"));

你应该可以开始了。

关于Java:renameTo() 函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6588293/

相关文章:

java - 使用 Java 扫描文件并根据其内容过滤它们(反向切片)

java - SortedMap 的 keySet() 能否始终安全地转换为 SortedSet?

java - 在单列上应用 Pig UDF 并自动生成所有其他列

java - xcode 4 中是否有 Shark 的替代品?

java - 哪种内存缓存实现需要花费最少的精力来构建

java - 将 ImageView 位图设置为返回的位图

java - Java 平台模块系统是否减小了 JAR 的大小?

java - 参数化类型数组

java - 如何在静态方法中获取 Java 中的命名参数?

java - java web服务Web服务超时