deleteOnExit不删除文件 [英] deleteOnExit not deleting file

查看:97
本文介绍了deleteOnExit不删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些文件供临时使用,并将它们用作某些方法的输入.然后我打电话给

I have created a few files for temporary use and used them as inputs for some methods. And I called

deleteOnExit() 

在我创建的所有文件上.但是仍然有一个文件.

on all files I created. But one file still remains.

我认为是因为文件仍在使用中,但是编译器不是仅在当前行完成后才转到下一行吗?(单线程)

I assume it is because the file is still in use, but doesn't the compiler go to next line only after the current line is done?(Single thread)

尽管实际上由于Java覆盖而没有问题,但始终只有一个文件.我想了解为什么会发生这种情况,以及是否可以使用

While its not a problem practically because of java overwrite, there is only one file always. I would like to understand why it happens and also if I can use

 Thread.sleep(sometime);

-

File x = new file("x.txt");
new class1().method1();

创建所有文件(5)之后,我刚刚添加了这一行

After creating all files(5), I just added this line

x.deleteOnExit(); y.deletOnExit() and so on...

除最后一个文件外的所有文件都将被删除.

All the files except that last one is deleted.

推荐答案

确保关闭所有正在写入文件的流.如果未关闭流,则文件将被锁定,而delete将返回false.那是我遇到的一个问题.希望有帮助.

Make sure that whatever streams are writing to the file are closed. If the stream is not closed, file will be locked and delete will return false. That was an issue I had. Hopefully that helps.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class Test {

    public static void main(String[] args) {

        File reportNew = null;
        File writeToDir = null;

        BufferedReader br = null;
        BufferedWriter bw = null;
        StringWriter sw = null;

        List<File> fileList = new ArrayList<File>();

        SimpleDateFormat ft = new SimpleDateFormat("yyyymmdd_hh_mm_ss_ms");


        try {

            //Read report.new file
            reportNew = new File("c:\\temp\\report.new");

            //Create temp directory for newly created files         
            writeToDir = new File("c:\\temp");
            //tempDir.mkdir();

            //Separate report.new into many files separated by a token
            br = new BufferedReader(new FileReader(reportNew));
            sw = new StringWriter();
            new StringBuilder();



            String line;
            int fileCount = 0;

            while (true) {

                line=br.readLine();

                if (line == null || line.contains("%PDF")) {

                    if (!sw.toString().isEmpty()) {

                        fileCount++;

                        File _file = new File(writeToDir.getPath() 
                                            + File.separator
                                            + fileCount
                                            + "_"
                                            + ft.format(new Date())
                                            + ".htm");

                        _file.deleteOnExit();
                        fileList.add(_file);


                        bw = new BufferedWriter(new FileWriter(_file));
                        bw.write(sw.toString());

                        bw.flush();
                        bw.close();
                        sw.getBuffer().setLength(0);

                        System.out.println("File " 
                                            + _file.getPath()
                                            + " exists "
                                            + _file.exists());
                    }

                    if (line == null)
                        break;
                    else
                        continue;
                }

                sw.write(line);
                sw.write(System.getProperty("line.separator"));
            }

        } catch ( Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这篇关于deleteOnExit不删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆