保存到特定目录

发布于 2024-11-24 04:04:10 字数 903 浏览 1 评论 0原文

我当前正在尝试将新创建的文本文件保存到用户指定的目录中。但是,我不明白此代码设置如何可能。在哪里指定文件的保存位置?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2024-12-01 04:04:10

FileChooserFileWriter 让事情变得相当简单,这里是 java 教程:

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

您可以这样称呼它:

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
}

请记住,执行实际打印的是 PrintWriter 类。

编辑:

如果您希望用户仅选择目录,请

fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

在显示之前调用。请注意,在这种情况下,您必须在该目录中指定一个新的 File 对象,以便能够向其中写入文本(尝试将文本写入目录将导致 IOException)。

FileChooser and FileWriter make things fairly easy, here is the java tutorial:

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.

EDIT:

If you want the user to select directories only, call

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).

拔了角的鹿 2024-12-01 04:04:10

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

不要使用这样的方法。所有文本组件都支持 write(...) 方法。您所要做的就是获取要写入文件的文件名。

例如:

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

如果您不知道如何使用文件选择器,请阅读 Swing 教程中 如何使用文件选择器

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

Don't use methods like that. All text components support a write(...) method. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文