使用JFileChooser将文件类型附加到Java文件中 [英] Appending the file type to a file in Java using JFileChooser
本文介绍了使用JFileChooser将文件类型附加到Java文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用JFileChooser保存图像。我只希望用户能够将图像保存为jpg。但是,如果他们不输入.jpg,则不会将其保存为图像。有可能以某种方式将.jpg附加到文件的末尾吗?
I'm trying to save an image using a JFileChooser. I only want the user to be able to save the image as a jpg. However if they don't type .jpg it wont be saved as an image. Is it possible to somehow append ".jpg" to the end of the file?
File file = chooser.getSelectedFile() + ".jpg";
因为我在文件中添加字符串不起作用。
Doesn't work as I'm adding a string to a file.
推荐答案
为什么不将文件
转换为字符串
并在完成后创建一个新的文件
?
Why not convert the File
to a String
and create a new File
when you're done?
File f = chooser.getSelectedFile();
String filePath = f.getAbsolutePath();
if(!filePath.endsWith(".jpg")) {
f = new File(filePath + ".jpg");
}
请记住,您不需要添加 .jpg
如果它已经存在。
Remember, you don't need to add the .jpg
if it's already there.
这篇关于使用JFileChooser将文件类型附加到Java文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文