简体   繁体   English

在 Java Swing 中,有没有办法将文件保存到某种“lib”文件夹?

[英]In Java Swing, is there a way to save files to some sort of 'lib' folder?

Right now, I can save a file based on a word processor I made in Java Swing.现在,我可以根据我在 Java Swing 中制作的文字处理器保存文件。 When they press the save button, JFileChooser asks where they would like to save it to.当他们按下保存按钮时, JFileChooser会询问他们希望将其保存到哪里。 I would like it so that every time the user saves, that a backup is generated in a lib folder, so that the user may revert back to a previous copy.我希望每次用户保存时,都会在lib文件夹中生成备份,以便用户可以恢复到以前的副本。 Is there a way I can say generate a folder inside a bin folder for each respective file that the user has, and inside that folder generates the copies.有没有办法我可以说在bin文件夹中为用户拥有的每个文件生成一个文件夹,并在该文件夹中生成副本。 For example, the user creates a file called MyDoc.rtf and inside lib generates a folder called MyDoc and inside has all the copies of the saved versions copy1 , copy2, , copy3 , etc.例如,用户创建了一个名为MyDoc.rtf的文件,并在lib内部生成一个名为MyDoc的文件夹,其中包含已保存版本的所有副本copy1copy2,copy3等。

So, there are two parts to the question.所以,这个问题有两个部分。 How do I generate a cache folder for each file saved?如何为每个保存的文件生成一个缓存文件夹? And how do I generate those copies?我如何生成这些副本?

The way I save the file using JFileChooser is as follows我使用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");
        }
    }
}

} }

For creating a directory that doesn't already exist, I can simply do mkdirs() and then use Files to write to a path.要创建一个尚不存在的目录,我可以简单地执行mkdirs() ,然后使用Files写入路径。 Alternatively, I can use FileWriter , BufferedWriter , and FileOutputStream .或者,我可以使用FileWriterBufferedWriterFileOutputStream

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM