gpt4 book ai didi

java - 为什么文件没有删除?输入/输出

转载 作者:行者123 更新时间:2023-12-02 06:38:47 27 4
gpt4 key购买 nike

你好,盖伊兹,我有一种方法可以加密我的应用程序中的文件,我想加密文件并删除源代码,我尝试了这样的方法,但它不起作用..?

 public static void encrypt(String password, InputStream is, OutputStream os,String DelFile) throws Exception {

SecretKeySpec keySpec = new SecretKeySpec(password(password), "TripleDES");
Cipher cipher = Cipher.getInstance("TripleDES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] buf = new byte[8096];
os = new CipherOutputStream(os, cipher);
int numRead = 0;
while ((numRead = is.read(buf)) >= 0) {
os.write(buf, 0, numRead);
}
os.close();

// file deleting part...
File f = new File(DelFile);
f.delete();}

这个f.delete();应该删除这个文件吗?我对吗?但它不会工作,请指教,谢谢。

最佳答案

要么针对该文件打开了输入流,要么未给出完整文件名(以及路径)。这就是 Filedelete() 方法对于查找不删除的实际原因不太有用的原因。如果你看一下它的docs

Deletes the file or directory denoted by this abstract pathname. Ifthis pathname denotes a directory, then the directory must be empty inorder to be deleted. Note that the Files class defines the deletemethod to throw an IOException when a file cannot be deleted. This isuseful for error reporting and to diagnose why a file cannot bedeleted.

因此,这里是帮助您找到根本原因的建议。使用Files.delete()删除方法。

抛出:

NoSuchFileException - if the file does not exist (optional specificexception)

DirectoryNotEmptyException - if the file is a directory andcould not otherwise be deleted because the directory is not empty(optional specific exception)

IOException - if an I/O error occurs

SecurityException - In the case of the default provider, and asecurity manager is installed, the SecurityManager.checkDelete(String)method is invoked to check delete access to the file

看到它抛出的异常,你就可以找出根本原因是什么。

关于java - 为什么文件没有删除?输入/输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19360234/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com