gpt4 book ai didi

Java:renameTo() 函数失败

转载 作者:行者123 更新时间:2023-11-30 05:01:24 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com