2012-07-24 20 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

相關問題