Dennis
Dennis

Reputation: 101

delete image from the folder

I want to delete one image from the folder when I delete a user from the table with this image. Here is my code: //first I can the function that finds a path of the image in the folder

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

This method allows choosing the image from the directory when I get the image I add the path in jTextField1.getText().

//use this method to get the path of my image.

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!");

                }
    } 

//I use this method to call another method 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);
}

I just don't know how to delete image from folder when I find it. Any ideas?

Upvotes: 0

Views: 5043

Answers (2)

user20687745
user20687745

Reputation:

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

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 data= Paths.get(selectedFile.getAbsolutePath());

    try {
        Files.delete(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Upvotes: 0

deHaar
deHaar

Reputation: 18568

You can use the modern and more powerful java.nio.* instead of the old fashioned java.io.File. You just have to create a Path object containing the path to the folder where the images are stored and resolve the file name:

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

EDIT due to discussion in comments below:

This is a very simple approach that deletes a file chosen via 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();
        }
    }
}

I have just tried it myself and it successfully removes the chosen file.

Upvotes: 4

Related Questions