Trung Bún
Trung Bún

Reputation: 1147

Java: replacing a new file

I draw an image on a panel by using BufferedImage, and now I want to export that image.

But how can I detect if the new file is created or replace the old one? right now my output is:

old filename: image.jpeg

new filename: image.jpeg.jpeg

How can I do it?? I put a detect code after the file is created, using createNewFile method, but it doesn't seem to work :(

This is pattern that do the saving, user can choose various types of image (bmp, jpeg ...): imageFile is File

private void saveImage(){
        JFileChooser savefile = new JFileChooser("~/");
        savefile.setFileSelectionMode(JFileChooser.FILES_ONLY);//Chose file only
        savefile.setFileFilter(new pngSaveFilter());//Save in PNG format
        savefile.addChoosableFileFilter(new jpegSaveFilter());//Save in JPEG format
        savefile.addChoosableFileFilter(new bmpSaveFilter());//Save in BMP format
        savefile.addChoosableFileFilter(new gifSaveFilter());//Save in GIF format
        savefile.setAcceptAllFileFilterUsed(false);
        int returnVal = savefile.showSaveDialog(null);//Show save dialog
        String EXT="";
        String extension="";
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            imageFile = savefile.getSelectedFile();
            extension = savefile.getFileFilter().getDescription();
            if (extension.equals("JPEG file images *.jpeg,*.JPEG")) {
                EXT = "JPEG";
                imageFile = new File(imageFile.getAbsolutePath() + ".jpeg");
            }
            if (extension.equals("PNG file images *.png,*.PNG")) {
                EXT = "PNG";
                imageFile = new File(imageFile.getAbsolutePath() + ".png");
            }
            if (extension.equals("Bitmap file images *.bmp,*.BMP")) {
                EXT = "BMP";
                imageFile = new File(imageFile.getAbsolutePath() + ".bmp");
            }
            if (extension.equals("GIF file images *.gif,*.GIF")) {
                EXT = "GIF";
                imageFile = new File(imageFile.getAbsolutePath() + ".gif");
            }
            try {
                if(imageFile != null){
                    topViewImagePanel.drawToSave();
                    System.out.println(imageFile.createNewFile());
                    //ImageIO.write(topViewImagePanel.getSavingImage(), EXT, imageFile);
                  // the code detection is below
                    if (imageFile.createNewFile()){
                        int value = JOptionPane.showConfirmDialog(null, "Image existed! Replace?", "Warning!", JOptionPane.YES_NO_OPTION);
                        if (value == JOptionPane.YES_OPTION){
                            imageFile.delete();
                            ImageIO.write(topViewImagePanel.getSavingImage(), EXT, imageFile);
                        }else if (value == JOptionPane.NO_OPTION){

                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Upvotes: 0

Views: 258

Answers (3)

fge
fge

Reputation: 121710

If you use Java 7, use the new API:

if (imageFile != null) {
    final Path path = imageFile.toPath();
    if (Files.exists(path)) {
        int value = JOptionPane.showConfirmDialog(null,
            "Image existed! Replace?", "Warning!", JOptionPane.YES_NO_OPTION);
             if (value == JOptionPane.YES_OPTION)
                 Files.delete(path);
    }
    ImageIO.write(topViewImagePanel.getSavingImage(), EXT,
        Files.newOutputStream(path));
}

Also, as to your original question:

I put a detect code after the file is created, using createNewFile method, but it doesn't seem to work :(

This is normal, you call .createNewFile() twice:

System.out.println(imageFile.createNewFile()); // <-- CALL 1
//ImageIO.write(topViewImagePanel.getSavingImage(), EXT, imageFile);
// the code detection is below
if (imageFile.createNewFile()){ // <-- CALL 2

It will always fail the second time!

Upvotes: 0

Tanmay Patil
Tanmay Patil

Reputation: 7057

Don't append file extension manually to the file name.
It is already present in the absolute path.

To handle files already present, use else clause of

if (imageFile.createNewFile())

Hope this helps.

Upvotes: 1

Matt Clark
Matt Clark

Reputation: 28599

if(imageFile.exists())

Do a simple check to see if a file already exists or not.

Upvotes: 2

Related Questions