java线程完成后如何删除文件?
How to delete a file after thread is finished in java?
我正在尝试发送带有附件的邮件。我正在使用 javamail api 来执行此操作。由于多个用户可以同时发送邮件,因此我创建了一个线程以确保安全。我可以使用 file.delete() 函数删除文件,该函数发生在邮件附件完成之前。但是 attachment/mail 发送后我无法删除文件。请帮我解决这个问题。
这是我用来附加和发送邮件的代码:
public void sendMailWithAttachment(String from, final String to, final String subject, final String msg, final String filePath) {
Thread ty = new Thread(){
public void run(){
MimeMessage message = mailSend.createMimeMessage();
try{
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(msg);
FileSystemResource file = new FileSystemResource(filePath);
helper.addAttachment(file.getFilename(), file);
//I want to write code to delete the file here
}catch (MessagingException e) {
throw new MailParseException(e);
}
mailSend.send(message);
}
};
ty.start();
}
这个任务你有多种方法,我推荐你第二种
1) 启动该文件,然后如果您的 java 环境对该文件夹具有正确的权限,您可以删除该文件,如下所示
try{
File file = new File("filePath");
if(file.delete()){
System.out.println("Deleted file: " + file.getName());
}else{
System.out.println("Delete failed on file ": + file.getName());
}
}catch(Exception e){
e.printStackTrace();
}
2) 您还可以使用通用 Class 文件
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
如果您在删除文件时遇到问题,可能是因为对象 FileSystemResource 指向该文件,请尝试使用
完成该对象
file.finalize()
看看这个问题就完成了Threads
How to know if other threads have finished?
在发送操作后的 finally 块中删除文件。
无论如何,您正在初始化您的 Thread 实现。您可以覆盖 Thread 实现的 finalize() 方法,该方法在 Thread 对象即将被垃圾回收时调用。
代码如下所示:
Thread ty = new Thread(){
public void run(){
// do mail sending stuff here.
}
@Override
protected void finalize() throws Throwable {
// delete file here
}
};
ty.start();
我正在尝试发送带有附件的邮件。我正在使用 javamail api 来执行此操作。由于多个用户可以同时发送邮件,因此我创建了一个线程以确保安全。我可以使用 file.delete() 函数删除文件,该函数发生在邮件附件完成之前。但是 attachment/mail 发送后我无法删除文件。请帮我解决这个问题。
这是我用来附加和发送邮件的代码:
public void sendMailWithAttachment(String from, final String to, final String subject, final String msg, final String filePath) {
Thread ty = new Thread(){
public void run(){
MimeMessage message = mailSend.createMimeMessage();
try{
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(msg);
FileSystemResource file = new FileSystemResource(filePath);
helper.addAttachment(file.getFilename(), file);
//I want to write code to delete the file here
}catch (MessagingException e) {
throw new MailParseException(e);
}
mailSend.send(message);
}
};
ty.start();
}
这个任务你有多种方法,我推荐你第二种
1) 启动该文件,然后如果您的 java 环境对该文件夹具有正确的权限,您可以删除该文件,如下所示
try{
File file = new File("filePath");
if(file.delete()){
System.out.println("Deleted file: " + file.getName());
}else{
System.out.println("Delete failed on file ": + file.getName());
}
}catch(Exception e){
e.printStackTrace();
}
2) 您还可以使用通用 Class 文件
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
如果您在删除文件时遇到问题,可能是因为对象 FileSystemResource 指向该文件,请尝试使用
完成该对象file.finalize()
看看这个问题就完成了Threads
How to know if other threads have finished?
在发送操作后的 finally 块中删除文件。
无论如何,您正在初始化您的 Thread 实现。您可以覆盖 Thread 实现的 finalize() 方法,该方法在 Thread 对象即将被垃圾回收时调用。
代码如下所示:
Thread ty = new Thread(){
public void run(){
// do mail sending stuff here.
}
@Override
protected void finalize() throws Throwable {
// delete file here
}
};
ty.start();