简体   繁体   English

Java:替换新文件

[英]Java: replacing a new file

I draw an image on a panel by using BufferedImage , and now I want to export that image. 我使用BufferedImage在面板上绘制图像,现在我想导出该图像。

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 :( 我使用createNewFile方法在文件创建后放置了检测代码,但是它似乎不起作用:(

This is pattern that do the saving, user can choose various types of image (bmp, jpeg ...): imageFile is File 这是进行保存的模式,用户可以选择各种类型的图像(bmp,jpeg ...): imageFileFile

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();
            }
        }
    }
if(imageFile.exists())

做一个简单的检查,看文件是否已经存在。

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. 希望这可以帮助。

If you use Java 7, use the new API: 如果您使用Java 7,请使用新的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 :( 我使用createNewFile方法在文件创建后放置了检测代码,但是它似乎不起作用:(

This is normal, you call .createNewFile() twice : 这是正常的,您两次调用.createNewFile()

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! 它将总是第二次失败!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM