2012-07-24 46 views
1

我有一个groovy脚本,将多个文件从远程目录保存到我的临时目录,并将它们解析为xml。它有一个有趣的错误。每次运行时,它都不能在我的临时目录中找到一个文件。下次运行时,它会找到该文件,但找不到新文件。如果我有20个文件,直到第20次运行才会找到所有20个文件。临时目录在每次运行后清除。我想知道该程序是否还有其他遗留物?Groovy删除的文件在Windows 7中留下了什么?

如果我在16次运行后清理项目,它仍会找到前16个文件。所以它看起来不是日食中的神器。

这是在Eclipse 3,Java 1.5,Windows 7,Groovy 1.0中运行。

remoteftpFile.findAll { 
     println "in find" 
     ftp.getReply(); 
     it.isFile() 
      }.each { 
       println "in each" 
       ftp.getReply(); 
       println it 
       ftp.getReply(); 

      def tempDestination=PropertiesUtil.getTempDir() 
      def procDestination=PropertiesUtil.getProcessedDir() 
      def tempFile = new File(tempDestination+ it.name) 
      def procFile = new File(procDestination+ it.name) 
      //set it to delete 
      ftp.getReply(); 
      println "got tempfile" 
      def localftpFile = ftp.SaveToDisk(it,tempFile) //Save each file to disk 



      //************** Handles decryption via gpgexe 
      println "Decrypting file"; 
      println localftpFile.toString(); 
      def localftpFileStr=localftpFile.toString(); 
      def processedftpFileStr=procFile.toString(); 
      def gpgstring=PropertiesUtil.getGpgString(); 
      def decryptedOutputName = localftpFileStr.substring(0, (localftpFileStr.length()-4)); 
      def decryptedProcOutputName= processedftpFileStr.substring(0, (processedftpFileStr.length()-4)); 
      def decryptedOutputXMLName = decryptedOutputName.substring(0, (decryptedOutputName.length()-4))+".xml"; 
      def decryptedProcOutputXMLName = decryptedProcOutputName.substring(0, (decryptedProcOutputName.length()-4))+".xml"; 
      println decryptedOutputName; 

      def xmlfile = new File(decryptedOutputName) 
      def cdmpXmlFile = new File(decryptedOutputXMLName) 
      def procCdmpXmlFile = decryptedProcOutputXMLName 


      println gpgstring + " --output ${decryptedOutputName} --decrypt ${localftpFile} " 
      (new ExternalExecute()).run(gpgstring +" --output ${decryptedOutputName} --decrypt ${localftpFile} ");  
      Thread.sleep(1000); 


      //************* Now Parse CSV file(s) into xml using stack overflow solution 
      println "parsing file" 


      def reader = new FileReader(xmlfile) 
      def writer = new FileWriter(cdmpXmlFile) 


      def csvdata = [] 
      xmlfile.eachLine { line -> 
       if (line){ 
       csvdata << line.split(',') 
       } 
      } 

      def headers = csvdata[0] 
      def dataRows = csvdata[1..-1] 

      def xml = new groovy.xml.MarkupBuilder(writer) 

      // write 'root' element 
      xml.root { 
       dataRows.eachWithIndex { dataRow, index -> 
        // write 'entry' element with 'id' attribute 
         entry(id:index+1) { 
         headers.eachWithIndex { heading, i -> 
          // write each heading with associated content 
          "${heading}"(dataRow[i]) 
               } 
             } 
             } 
         } 


      println "Performing XSL Translation on ${cdmpXmlFile}" 
      def cdmpXML = new XMLTransformer(xmlTranslate).run(cdmpXmlFile) //Run it on each of the xml files and set the output 
      new File("C:\\temp\\temp.xml").write(cdmpXML) 
      new File(procCdmpXmlFile).write(cdmpXML) 
      println "Done Performing XSL Translation" 

      println "Uploading Data to CDMP" 
      def cdmpUp = new UpdateCDMP(updateDB) 
      cdmpUp.run(cdmpXML) 

      println "Finished Upload and Import" 


      //do clean-up backing it up AND removing the files 

      println "Finished" 
      println "Closing Buffers" 
      reader.close(); 
      writer.close(); 
      println "Deleting Local Files" 
      new File(decryptedOutputName).deleteOnExit(); 
      new File(localftpFile).deleteOnExit(); 
      xmlfile.deleteOnExit(); 
      cdmpXmlFile.deleteOnExit(); 

      println "Deleting " + cdmpXmlFile.getName() 
      new File("C:\\temp\\temp.xml").deleteOnExit(); 
      } 
    ftp.close() 
} 
+0

也许文件在写入临时目录后没有关闭或刷新?没有看到代码是不可能的。 – ataylor 2012-07-24 18:32:11

+0

Groovy版本?你的代码有问题的例子? – 2012-07-24 18:34:49

回答

1

这是因为你使用deleteOnExit,这是not guaranteed to delete a file

  • 文件被正确关闭,
  • 的JVM正常退出(没有例外),
  • 一个System.exit()被称为与0参数(或VM自然终止):如果只删除。

它在Windows操作系统上尤其成问题。我不能指出关于这个问题的一个特定的堆栈溢出问题,但大多数questions involving deleteOnExit讨论这个问题。

如果你真的想删除一个文件,那么你应该总是use aFile.delete() directly。真的没有什么理由推迟删除,直到后来的例子。

+0

我可以看到文件在运行之间的临时目录中被删除,因此deleteOnExit正在运行。此错误发生在2台计算机上,但不在2台其他计算机上。我怀疑Windows留下的文件权限,Java版本或文件的文件。 – 2012-07-25 15:33:36

+0

感谢您的回答,过度热情! – 2012-08-20 23:45:30

相关问题