简体   繁体   English

保存到特定目录

[英]Saving to a specific directory

I'm currently trying to save a newly created text file to a directory that the user specifies.我目前正在尝试将新创建的文本文件保存到用户指定的目录中。 However, I don't see how it is possible with this code setup.但是,我看不出如何使用此代码设置。 Where does one specify where file is to be saved?在哪里指定文件要保存在哪里?

if(arg.equals(Editor.fileLabels[1])){
    if(Editor.VERBOSE)
        System.err.println(Editor.fileLabels[1] + 
                " has been selected");
    filedialog = new FileDialog(editor, "Save File Dialog", FileDialog.SAVE); 
    filedialog.setVisible(true);
    if(Editor.VERBOSE){
        System.err.println("Exited filedialog.setVisible(true);");
        System.err.println("Save file = " + filedialog.getFile());
        System.err.println("Save directory = " + filedialog.getDirectory());
    }
    File file = new File("" + filedialog.getName());
    SimpleFileWriter writer =     SimpleFileWriter.openFileForWriting(filedialog.getFile() + ".txt"); 
    if (writer == null){
        System.out.println("Failed.");
    }
    writer.print("" + this.editor.getTextArea().getText());
    writer.close();
}

FileChooser and FileWriter make things fairly easy, here is the java tutorial: FileChooserFileWriter让事情变得相当简单,这里是 java 教程:

http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html http://www.abbeyworkshop.com/howto/java/writeText/index.html http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html http://www.abbeyworkshop.com/howto/java/writeText/index.html

You call it like this:你这样称呼它:

JFileChooser fc = new JFileChooser();

int returnVal = fc.showOpenDialog(aComponent);

if (returnVal == JFileChooser.APPROVE_OPTION) 
{
    File toSave = fc.getSelectedFile();

    FileWriter outWriter = new FileWriter(toSave);
    PrintWriter outPrinter = new PrintWriter(outWriter);

    outPrinter.println("" + this.editor.getTextArea().getText());
}
else
{
    //user pressed cancel
}

Remember that it is the PrintWriter class that does the actual printing.请记住,实际打印的是PrintWriter class。

EDIT:编辑:

If you want the user to select directories only, call如果您希望用户只访问 select 目录,请调用

fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

before displaying it.在显示它之前。 Note that in this case you will have to specify a new File object WITHIN that directory in order to be able to write text to it (attempting to write the text to a directory will result in an IOException).请注意,在这种情况下,您必须在该目录中指定一个新文件 object 才能向其写入文本(尝试将文本写入目录将导致 IOException)。

writer.print("" + this.editor.getTextArea().getText()); writer.print("" + this.editor.getTextArea().getText());

Don't use methods like that.不要使用这样的方法。 All text components support a write(...) method.所有文本组件都支持 write(...) 方法。 All you have to do is get the File name that you want to write the file to.您所要做的就是获取要写入文件的文件名。

Something like:就像是:

JtextArea textArea = new JTextArea(....);
....
FileWriter writer = new FileWriter( "TextAreaLoad.txt" ); // get the file name from the JFileChooser.
BufferedWriter bw = new BufferedWriter( writer );
textArea.write( bw );
bw.close();

If you don't know how to use file choosers then read the section from the Swing tutorial on How to Use File Choosers .如果您不知道如何使用文件选择器,请阅读 Swing 教程中有关如何使用文件选择器的部分。

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

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