gpt4 book ai didi

java - 从文件夹中删除图像

转载 作者:行者123 更新时间:2023-12-01 19:45:36 24 4
gpt4 key购买 nike

当我从包含此图像的表中删除用户时,我想从文件夹中删除一张图像。这是我的代码://首先我可以使用在文件夹中查找图像路径的函数

public void deleteStudent(String name) {
try {
CallableStatement statement = null;
Connection data = getmyConnection();
statement = data.prepareCall("{call delete_student(?)}");

statement.setString(1, name);
//statement.registerOutParameter(2, java.sql.Types.VARCHAR);
statement.executeQuery();
} catch (Exception c) {
c.printStackTrace();
}

//After I call the function to delete image from directory
deleteImageDerictory(name);
}

此方法允许在获取图像时从目录中选择图像,并在 jTextField1.getText() 中添加路径。

//使用这个方法来获取我的图片的路径。

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new File(System.getProperty("user.home")));

FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpeg", "jpg", "png");




file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
if(result ==JFileChooser.APPROVE_OPTION) {
File selectedFile = file.getSelectedFile();
//GET ABSOLUTE PATH OF PICTURES
jTextField1.setText(selectedFile.getAbsolutePath());
//addPicture.setText(selectedFile.getName());
//GET NAME OF PICTURES
//getPicName = selectedFile.getName();




} else if(result == JFileChooser.CANCEL_OPTION) {
System.out.println("File not found!");

}
}

//我用这个方法调用另一个方法deleteImageDerictory(jTextField1.getText());

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
try{
deleteImageDerictory(jTextField1.getText());
}catch(Exception e) {
e.printStackTrace();
}
}


public void deleteImageDerictory(String name) {
String pictureName = null;
try {
CallableStatement statement = null;
Connection data = getmyConnection();
statement = data.prepareCall("{call get_picture(?)}");
statement.setString(1, name);
//statement.registerOutParameter(2, java.sql.Types.VARCHAR);
myResults = statement.executeQuery();

while (myResults.next()) {
//COPY PATH IN pictureName
pictureName = myResults.getString(1);
}

myResults.close();
} catch (Exception c) {
c.printStackTrace();
}

//DELETE ELEMENT FROM FOLDER
File sourceFile = new File(pictureName);
File file = new File("/Computer/NetBeans IDE 8.2/NewDataBase/src/newdatabase/images/");

images = file.listFiles();

File file2 = new File(file.getAbsolutePath(), sourceFile.getName() );

boolean deleted = file2.delete();
System.out.println(deleted);
}

我只是不知道如何在找到图像时将其从文件夹中删除。有什么想法吗?

最佳答案

您可以使用现代且功能更强大的java.nio.*,而不是老式的java.io.File。您只需创建一个 Path 对象,其中包含存储图像的文件夹的路径并解析文件名:

//DELETE ELEMENT FROM FOLDER
Path imagesPath = Paths.get(
"/Computer/NetBeans IDE 8.2/NewDataBase/src/newdatabase/images/" +
pictureName);

try {
Files.delete(imagesPath);
System.out.println("File "
+ imagesPath.toAbsolutePath().toString()
+ " successfully removed");
} catch (IOException e) {
System.err.println("Unable to delete "
+ imagesPath.toAbsolutePath().toString()
+ " due to...");
e.printStackTrace();
}

根据下面评论中的讨论进行编辑:

这是一种非常简单的方法,可以删除通过 JFileChooser 选择的文件:

public static void main(String[] args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());

int result = jfc.showOpenDialog(null);

if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println("Chosen file: " +
selectedFile.getAbsolutePath() +
" will be deleted!");

Path pathToBeDeleted = Paths.get(selectedFile.getAbsolutePath());

try {
Files.delete(pathToBeDeleted);
} catch (IOException e) {
e.printStackTrace();
}
}
}

我刚刚亲自尝试过,它成功删除了所选文件。

关于java - 从文件夹中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53520358/

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