gpt4 book ai didi

java - 无法使用 renameTo() 移动文件

转载 作者:行者123 更新时间:2023-11-30 07:43:20 25 4
gpt4 key购买 nike

我目前有一个文件监视器,它读取 XML 文件并根据内容更新数据库表。读取文件时,会根据特定模式对其进行验证,以确保内容正确。

验证的结果会影响要移动的文件的目的地。

如果架构成功,则将文件移动到已处理目录时不会出现问题,但是,当架构失败时,我无法重命名文件本身。

但是,当我调试和探索文件路径以确保文件移动到正确的位置时,它通常允许我重命名文件,这让我认为这是一个计时问题。

文件监视器类:

// Get a directory listing;
FilterFiles filterFiles = new FilterFiles();
String inbox = GlobalVars.getConfiguration().getInboundInbox();
String [] dir = new File(inbox).list(filterFiles);

// Wait 500 ms before obtaining files (Avoid the ResourceBusy error);
try {
Thread.sleep(500);
} catch (InterruptedException e) {

// Log errors
logger.info("Interrupted Exception " + e.getMessage());
new LogStackTrace(logger, e);

e.printStackTrace();
}

// Iterate through directory;
for ( int a=0; a < dir.length; a++ ) {

// Derive the full file path i.e. folder + path name;
String fullFilePath = GetAbsoloutePath.getFullAbsoluteFilePath(inbox, dir[a]);

logger.info("==========================================================");
logger.info("");
logger.info("Found File : " + fullFilePath);
logger.info("==========================================================");

// Does the file exist & can we read the file at this time;
File file = new File(fullFilePath);

if (file.exists() && file.canRead()) {

// Process the file;
StringBuffer renamedFile = new StringBuffer();

// Rename the file to indicate InProgess;
String fileName = file.getAbsolutePath();
String fileNameInProgress = fileName.trim() + ".InProgress";

// Delete the file if exists where we are moving the file to;
File deleteFile = new File(fileNameInProgress);

if (deleteFile.delete()) logger.info("Existing file deleted: " + fileNameInProgress);

// Ensure file is renamed so we can see it is in progress
if (!file.renameTo(new File(fileNameInProgress)) ) {

// Logging
logger.error("Failed to renamed file :" + fileName + " to " + fileNameInProgress);
break;
}else{

logger.info("Renamed file to : " + fileNameInProgress);

// Pass back name of renamed file;
renamedFile.append(fileNameInProgress);

File renamedFileRef = new File(renamedFile.toString());

// Path to move - could be errors or processed
String pathToMove = "";

// Parse XMobject
ParseInboundXML par = new ParseInboundXML();

// check if parse was succesful
if(par.parseXML(renamedFileRef, con)){

// Path to move XML file
pathToMove = GlobalVars.getConfiguration().getInboundProcessed()+ "\\" + file.getName();

//Logging
logger.info("File parsed and tables updated successfully");
logger.info("Moving file to : " + pathToMove);
}else{

pathToMove = GlobalVars.getConfiguration().getInboundErrors()+ "\\" + file.getName();

//Logging
logger.error("Errors when parsing file and updating tables");
logger.error("Moving file to : " + pathToMove);
}


// Help with garbage collection
par = null;

// New file
File newPath = new File(pathToMove);

// Need to check if this already exists in processed- if so we must override the existing file
// Otherwise the move will not be processed and the same docuemnt will be continously processed
if(newPath.exists())
newPath.delete();

上面的代码执行完后,执行了这 block 代码,这就是重命名失败的地方:

        // Rename path so it is placed in processed folder 
if(renamedFileRef.renameTo(newPath)){

//Logging
logger.info("File processed successfully");
}else{

// Logging
logger.error("Unable process file");
}

在代码中,您可能会注意到对以下内容的调用:

 // Parse XMobject
ParseInboundXML par = new ParseInboundXML();

// check if parse was succesful
if(par.parseXML(renamedFileRef, con)){

此方法是进行验证和解析 XML 文档的地方,并根据结果返回 true 或 false 值。

失败的区域位于方法的开头:

 // Find schema path
String schemaPath = GlobalVars.getConfiguration().getInboundSchemaLocation();
String schemaFileName = "WMS_" + file.getAbsoluteFile().toString().split("#")[2] + ".xsd";

// Schema location
String schemaLocation = schemaPath + "\\" + schemaFileName;

// Create schema factory
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// Schema file
Source schemaFile = new StreamSource(new File(schemaLocation));

// Apply schema and validate XML
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
Source xmlFile = new StreamSource(file);
validator.validate(xmlFile);

我认为问题可能在于

 Source xmlFile = new StreamSource(file);

因为这是我们尝试重命名的文件,我认为如果验证失败,这可能会使文件保持打开状态,并解释为什么在通过验证时没有问题。

编辑

这是验证失败的 catch 子句:

 catch (SAXException e) {

// Write Error Record
CreateErrorMessages.createIW702Message(record.getTrno701TransactionNumber(), IntegrationConstants.SAX_ERROR, "SAX Error", e.toString(), con);

// Logging
logger.info("SAX Exception " + e.getMessage());

// Log stack trace
new LogStackTrace(logger, e);

// Prints to stack trace
e.printStackTrace();

return false;

}

最佳答案

如果您想稍后重命名该文件,则必须先关闭该文件。显然,您的代码没有这样做。

Closing StreamSourceMoving files after failed validation (Java) 建议不要传入 File ,而是传入 FileInputStream ,然后您可以在最后自己关闭它(或使用 try-with-resource)

try(InputStream in = new FileInputStream (file)) {
Source xmlFile = new StreamSource(fr);
validator.validate(xmlFile);
}

关于java - 无法使用 renameTo() 移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34332597/

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