How to save file using JFileChooser?

前端 未结 4 645
花落未央
花落未央 2020-12-17 16:16

I have a method in my application called \"Save as\" which Saves the image of my application on computer my into a file. I used the JFileChooser to let the users choose thei

相关标签:
4条回答
  • 2020-12-17 16:27

    Prepare the file chooser filters:

        jFileChooser.addChoosableFileFilter(new FileNameExtensionFilter("File X (.xxx)", "xxx"));
        jFileChooser.addChoosableFileFilter(new FileNameExtensionFilter("File Y (.yyy)", "yyy"));
        jFileChooser.addChoosableFileFilter(new FileNameExtensionFilter("File Z (.zzz)", "zzz"));
    
        // set default type
        jFileChooser.setFileFilter(jFileChooser.getChoosableFileFilters()[0]);
    
        // set default file
        jFileChooser().setSelectedFile(defaultFile);
    

    After approve validation

    //Add extension to Selected file 
    File file = new File(jFileChooser().getSelectedFile().getCanonicalPath() + "." + ((FileNameExtensionFilter) jFileChooser().getFileFilter()).getExtensions()[0]);
    

    Could be a good idea validate if the selected file come with extension.

    0 讨论(0)
  • 2020-12-17 16:34

    Finally, I solved my own problem:

    JFileChooser fc = new JFileChooser("C:/");
    fc.addChoosableFileFilter(new JPGSaveFilter());
    fc.addChoosableFileFilter(new JPEGSaveFilter());
    fc.addChoosableFileFilter(new PNGSaveFilter());
    fc.addChoosableFileFilter(new GIFSaveFilter());
    fc.addChoosableFileFilter(new BMPSaveFilter());
    fc.addChoosableFileFilter(new WBMPSaveFilter()); 
    
    int retrieval = fc.showSaveDialog(null);
    
    if (retrieval == JFileChooser.APPROVE_OPTION) {
      String ext = "";
    
      String extension = fc.getFileFilter().getDescription();
    
      if (extension.equals("*.jpg,*.JPG")) {
        ext = ".jpg";
      } else if (extension.equals("*.png,*.PNG")) {
        ext = ".png";
      } else if (extension.equals("*.gif,*.GIF")) {
        ext = ".gif";
      } else if (extension.equals("*.wbmp,*.WBMP")) {
        ext = ".wbmp";
      } else if (extension.equals("*.jpeg,*.JPEG")) {
        ext = ".jpeg";
      } else if (extension.equals("*.bmp,*.BMP")) {
        ext = ".bmp";
      }
    }
    

    Example Filter:

    package example
    
    import java.io.File;
    import javax.swing.filechooser.FileFilter;
    
    class JPGSaveFilter extends FileFilter {
      @Override
      public boolean accept(File f) {
        if (f.isDirectory()) {
          return false;
        }
    
        String s = f.getName().toLowerCase();
    
        return s.endsWith(".jpg");
      }
    
      @Override
      public String getDescription() {
        return "*.jpg,*.JPG";
      }
    }
    
    0 讨论(0)
  • 2020-12-17 16:38

    I think I got better solution. Will explain it with sample code fragments.

    This is how I set file filter:
    jFileChooser.setFileFilter(new FileNameExtensionFilter(".txt", "txt"));.

    After this the main saving line:
    textArea1.write(new BufferedWriter(new FileWriter(jFileChooser.getSelectedFile().getAbsolutePath() + jFileChooser.getFileFilter().getDescription().replace("All Files", ""))));.

    Of course the most important is this fragment: jFileChooser.getSelectedFile().getAbsolutePath() + jFileChooser.getFileFilter().getDescription().replace("All Files", "").

    The only thing I don't like is, that I could not find any method like 'getExtension' which means that You cannot have any nice description without unnecessary troubles with strings.


    Ok, got it. You can do something like that: jFileChooser.getFileFilter().toString().replaceFirst(".*extensions=\\[(.*)]]", ".$1").replaceFirst(".*AcceptAllFileFilter.*", "").

    Unfortunately it's not so beautiful, but seems to work like a charm.

    0 讨论(0)
  • 2020-12-17 16:43

    Use JFileChoose.SetFileFilter example: http://www.java2s.com/Code/JavaAPI/javax.swing/JFileChoosersetFileFilterFileFilterfilter.htm

    0 讨论(0)
提交回复
热议问题