从文件夹中删除图像

时间:2018-11-28 13:17:12

标签: java

当我从具有该图像的表中删除用户时,我想从文件夹中删除一个图像。这是我的代码: //首先,我可以找到在文件夹中找到图像路径的函数

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);
}

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

1 个答案:

答案 0 :(得分:3)

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

//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();
        }
    }
}

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