Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaSwing中,有没有一种方法可以将文件保存到某种形式的';lib';文件夹?_Java_Swing_Jfilechooser - Fatal编程技术网

在JavaSwing中,有没有一种方法可以将文件保存到某种形式的';lib';文件夹?

在JavaSwing中,有没有一种方法可以将文件保存到某种形式的';lib';文件夹?,java,swing,jfilechooser,Java,Swing,Jfilechooser,现在,我可以基于我在JavaSwing中制作的字处理器保存一个文件。当他们按下保存按钮时,JFileChooser询问他们想将其保存到哪里。我希望每次用户保存时,都会在lib文件夹中生成备份,以便用户可以恢复到以前的副本。是否有一种方法可以说,在bin文件夹中为用户拥有的每个文件生成一个文件夹,并在该文件夹中生成副本。例如,用户创建一个名为MyDoc.rtf的文件,并在lib内部生成一个名为MyDoc的文件夹,其中包含已保存版本的所有副本copy1、copy2、、copy3,等等 因此,问题有两

现在,我可以基于我在JavaSwing中制作的字处理器保存一个文件。当他们按下保存按钮时,
JFileChooser
询问他们想将其保存到哪里。我希望每次用户保存时,都会在
lib
文件夹中生成备份,以便用户可以恢复到以前的副本。是否有一种方法可以说,在
bin
文件夹中为用户拥有的每个文件生成一个文件夹,并在该文件夹中生成副本。例如,用户创建一个名为
MyDoc.rtf
的文件,并在
lib
内部生成一个名为
MyDoc
的文件夹,其中包含已保存版本的所有副本
copy1
copy2、
copy3
,等等

因此,问题有两个部分。如何为保存的每个文件生成缓存文件夹?我如何生成这些副本

使用
JFileChooser
保存文件的方法如下

public class SaveContent {
String formattedText;

public void save(JTextPane text){
    if(text.getText().length() > 0){
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("RICH TEXT FORMAT", "rtf", "rtf");
        chooser.setFileFilter(filter);

        int option = chooser.showSaveDialog(null);
        String filePath = chooser.getSelectedFile().getPath();

        if(!chooser.getSelectedFile().getPath().toLowerCase().endsWith(".rtf")){
            filePath=chooser.getSelectedFile().getPath() + ".rtf";
        }

        if(option == JFileChooser.APPROVE_OPTION){
            StyledDocument doc = (StyledDocument)text.getDocument();
            HTMLEditorKit kit = new HTMLEditorKit();
            BufferedOutputStream out;

            try{
                out = new BufferedOutputStream(new FileOutputStream(filePath));
                kit.write(out,doc,doc.getStartPosition().getOffset(), doc.getLength());
            } catch(FileNotFoundException e){

            } catch(IOException e){

            } catch(BadLocationException e){

            }
        } else{
            System.out.println("SAVE CANCCELED");
        }
    }
}

}

要创建一个不存在的目录,我只需执行
mkdirs()
,然后使用
Files
写入路径即可。或者,我可以使用
FileWriter
BufferedWriter
FileOutputStream

          if(textListiner != null) {
                textListiner.textEmitted("GOodeye\n");

                //Add a folder
                File f = new File("../Swing1/backups/testDir");
                if(!f.exists()) {
                    boolean success = (new File("../Swing1/backups/testDir")).mkdirs();
                    if (!success) {
                        // Directory creation failed
                        System.out.println("FAIL CREATE DIR");
                    }
                }
                else {
                    System.out.println("ALREADY EXISTS");
                }               
            }

                //Add the file to the folder (without JFileChooser and showSaveDialog)
                // COuld use FileWriter, BufferedWriter FIleOutputStream, or Files
                //Lookup: https://www.journaldev.com/878/java-write-to-file
                String data;
                try {
                    Files.write(Paths.get("../Swing1/backups/testDir/copy1.txt"), data.getBytes());
                } catch (IOException eIO) {
                    eIO.printStackTrace();
                }

查看其他源代码后,我想我可以首先使用
.mkdirs()
生成一个目录,如果它还不存在的话。在其中,我可以使用
JFileChooser
并指定相对路径。i、 e.
../backups/CoolDoc
您建议使用笨重的旧Java文件API而不是NIO有什么具体原因吗?@Zabuza老实说,这是我找到的第一个可行的解决方案。旧的遗产有什么问题?有很多东西。在Java7中,它被NIO完全取代是有原因的。例如,默认编码问题。或更正失败时的错误消息。或者用一行代码读写文件。