Java:替换新文件
我使用Java:替换新文件,java,Java,我使用BufferedImage在面板上绘制图像,现在我想导出该图像 但是如何检测新文件是创建的还是替换旧文件?现在我的输出是: old filename: image.jpeg new filename: image.jpeg.jpeg 我该怎么做??我使用createNewFile方法在文件创建后放置了一个检测代码,但它似乎不起作用:( 这是进行保存的模式,用户可以选择各种类型的图像(bmp、jpeg…): imageFile是File private void saveImage(){
BufferedImage
在面板上绘制图像,现在我想导出该图像
但是如何检测新文件是创建的还是替换旧文件?现在我的输出是:
old filename: image.jpeg
new filename: image.jpeg.jpeg
我该怎么做??我使用createNewFile
方法在文件创建后放置了一个检测代码,但它似乎不起作用:(
这是进行保存的模式,用户可以选择各种类型的图像(bmp、jpeg…):
imageFile
是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();
}
}
}
做一个简单的检查,看看文件是否已经存在
做一个简单的检查,看看文件是否已经存在
做一个简单的检查,看看文件是否已经存在
进行简单检查,查看文件是否已存在。不要手动将文件扩展名附加到文件名。
它已经存在于绝对路径中 要处理已经存在的文件,请使用的else子句
if (imageFile.createNewFile())
希望这有帮助。不要手动将文件扩展名附加到文件名。
它已经存在于绝对路径中 要处理已经存在的文件,请使用的else子句
if (imageFile.createNewFile())
希望这有帮助。不要手动将文件扩展名附加到文件名。
它已经存在于绝对路径中 要处理已经存在的文件,请使用的else子句
if (imageFile.createNewFile())
希望这有帮助。不要手动将文件扩展名附加到文件名。
它已经存在于绝对路径中 要处理已经存在的文件,请使用的else子句
if (imageFile.createNewFile())
希望这有帮助。如果您使用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));
}
另外,关于你原来的问题:
我在创建文件后使用createNewFile方法放置了一个检测代码,但它似乎不起作用:(
这很正常,您可以调用.createNewFile()
两次:
System.out.println(imageFile.createNewFile());//如果使用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));
}
另外,关于你原来的问题:
我在创建文件后使用createNewFile方法放置了一个检测代码,但它似乎不起作用:(
这很正常,您可以调用.createNewFile()
两次:
System.out.println(imageFile.createNewFile());//如果使用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));
}
另外,关于你原来的问题:
我在创建文件后使用createNewFile方法放置了一个检测代码,但它似乎不起作用:(
这很正常,您可以调用.createNewFile()
两次:
System.out.println(imageFile.createNewFile());//如果使用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));
}
另外,关于你原来的问题:
我在创建文件后使用createNewFile方法放置了一个检测代码,但它似乎不起作用:(
这很正常,您可以调用.createNewFile()
两次:
System.out.println(imageFile.createNewFile());//在Java 7中,您将使用Files.exists()
在Java 7中,您将使用Files.exists()
在Java 7中,您将使用Files.exists()
您无法检查imageFile.delete()的返回值
:可能会失败!无法检查imageFile.delete()的返回值。
:可能会失败!无法检查imageFile.delete()的返回值。
:可能会失败!无法检查imageFile.delete()的返回值
:它可能会失败!它会抛出非法参数异常,因为图像为空。你是什么意思?ImageIO.write行有错误,因此我认为图像为空,ImageIO无法将空图像写入文件嗯,这是另一个问题……抱歉,虽然我知道如何进行文件i/O,但图像不是我的专长;)我认为操作应该是类似的。如果ImageIO为null,则该语句中有错误。但无论如何,谢谢:)它抛出一个非法参数异常,因为图像为空。你是什么意思?ImageIO.write行有错误,所以我认为图像为空,ImageIO无法将空图像写入文件嗯,这是另一个问题……对不起,虽然我知道如何进行文件i/O,但图像不是我的专长;)我认为操作应该是类似的。如果ImageIO为null,则该语句中有错误。但无论如何,谢谢:)它抛出一个非法参数异常,因为图像为空。你是什么意思?ImageIO.write行有错误,所以我认为图像为空,ImageIO无法将空图像写入文件嗯,这是另一个问题……对不起,虽然我知道如何进行文件i/O,但图像不是我的专长;)我认为操作应该类似。如果ImageIO为null,则该语句中存在错误。不过还是要谢谢你:)它抛出了一个非法参数异常,因为图像为空。你的意思是什么图像?行ImageIO.write处有错误,所以我认为图像为空,ImageIO无法将空图像写入文件嗯,这是另一个问题。。。抱歉,虽然我知道如何进行文件I/O,但图像不是我的专长;)我认为操作应该类似。如果ImageIO为null,则该语句中存在错误。不过还是要谢谢你:)