9

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.

Kingteeb
  • 243
  • 2
  • 3
  • 8

3 Answers3

13

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

Remember, you don't need to add the .jpg if it's already there.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • This answer will do it. However do filePath.concat(".jpg") to avoid an unneeded StringBuilder construction. – Justin May 06 '12 at 14:55
  • @Justin The overhead of creating a new `StringBuilder` should be negligible as he's only performing the action once. – Jeffrey May 06 '12 at 14:57
  • Thank you Jeffrey :) Sadly I can't up-vote yet. Do you know why the format name is required here: ImageIO.write(image, "jpg", file); it doesn't seem to make a difference? If you have to have the format on the file anyway? – Kingteeb May 06 '12 at 15:11
  • In your code you may wanna adjust his answer to mine. To check for multiple extentions of jpg. His answer will work. However it just appends .jpg to the end of the file so example if you have: c:\examplepath\examplefile.txt it will turn it in to: c:\examplepath\examplefile.txt.jpg – Justin May 06 '12 at 15:13
  • 2
    ...and `toLowerCase()` the file name the user selected before testing for the presence of the extension. Without that the code will break in Windows, where the user may (for whatever reason) type "myfile.JPG". – Christian Garbin May 06 '12 at 15:18
  • Haha true! Damn, there is a lot to think about! :P – Kingteeb May 06 '12 at 15:23
4

It works if you do in two steps: add extension to selected file, then create File object with the extension appended to it.

String withExtension = chooser.getSelectedFile().getAbsolutePath() + ".jpg";
File file = new File( withExtension )

Implied in your question, but just to cover all bases: need to check if the extension isn't already there first, e.g.:

String withExtension = chooser.getSelectedFile().getAbsolutePath();
if( !withExtension.toLowerCase().endsWith( ".jpg" ) )
   withExtension += ".jpg";

[You may want to add ".jpeg" to the if above - it's a valid extension for JPEG files]

The code below may or may not work. File has a toString() that will allow it to compile, but I'd rather use the method above, where I can control the exact path name I'm getting. The toString() documentation for the File object is a bit unclear to me on what exactly it returns. In cases like this one, I prefer to call a function that I know returns what I need for sure.

File file = new File( chooser.getSelectedFile() + ".jpg" ); 
Christian Garbin
  • 2,512
  • 1
  • 23
  • 31
1

The best approach would be to check for an acceptable entry. If one isn't found replace the file exention instead of writing on top of it.

File f = chooser.getSelectedFile().getAbsolutePath();
String path = f.getAbsolutePath();
if (!path.matches(".*\\.(jpg|jpeg)")) {
    // If the extension isn't there. We need to replace it
    path = path.substring(0, path.lastIndexOf(".") + 1).concat("jpg");
    f = new File(path);
}
Justin
  • 436
  • 1
  • 3
  • 9
  • 1
    A good solution to cover all extensions with one `if`, but need to add `(?i)` to make it case insensitive (catch JPG as well as jpg): `if (!path.matches("(?i).*\\.(jpg|jpeg)"))`. Also need to account for the case where the selected (or typed) file name doesn't have an extension, where `path.lastIndexOf(".")`returns -1 (e.g. with input `test`, the code will change it to `jpg`, instead of `test.jpg`. – Christian Garbin May 06 '12 at 15:53
  • Agreed another thing you can do is skip regex all together if you want speed. – Justin May 06 '12 at 16:16
  • The code is about to read or write to the disk, the regex will pale in comparison to that. I liked the way you used the regex to catch all possible extensions. – Christian Garbin May 06 '12 at 18:42