1

複数のファイルをリモート ディレクトリから一時ディレクトリに保存し、それらを xml に解析する groovy スクリプトがあります。興味深いバグがあります。実行するたびに、一時ディレクトリに 1 つのファイルが見つかりません。次回の実行時にそのファイルは見つかりますが、新しいファイルは見つかりません。20個のファイルがある場合、20回目の実行まで20個のファイルすべてが見つかりません。一時ディレクトリは、実行ごとにクリアされます。プログラムが残している他のアーティファクトがあるかどうか疑問に思っていますか?

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() 
} 
4

1 に答える 1

1

これは、ファイルの削除が保証されていないdeleteOnExitを使用しているためです。次の場合にのみ削除されます。

  • ファイルが正しく閉じられている
  • JVM が正常に終了する (例外なし)
  • aSystem.exit()が引数付きで呼び出された0(または VM が自然に終了した)。

Windows OS では特に問題があります。これに関する特定の Stack Overflow の質問を指摘することはできませんが、関連するほとんどの質問でdeleteOnExitこの問題について説明しています。

実際にファイルを削除したい場合は、常に直接使用aFile.delete()する必要があります。あなたの例の後半まで削除を遅らせる正当な理由は本当にありません。

于 2012-07-25T07:50:38.973 に答える