0

次のようなファイルを作成します。

PrintWriter out = new PrintWriter(
                     new FileOutputStream(
                      new File("C:/Users/.../Desktop/Server Recipe Log.txt"), 
                      true));
             out.println("serverText");
             out.close();

しかし、ファイルをデスクトップに保存したくありません。名前を付けて保存ダイアログを開いて、ファイルを保存する場所を選択したいのです。

フレームを使用していくつかのチュートリアルを試しましたが、フレームを作成したくありません。ネイティブのシステム ダイアログを使用したいと考えています。

4

2 に答える 2

2

..ネイティブ システム ダイアログを使用したい。

間違った言語を使用しています。最も近い Java オファーは、java.awt.FileDialogまたはjavax.swing.JFileChooserネイティブ PLAF の使用です。

例えば

import java.awt.*;
import javax.swing.*;

class FileDialogs {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                FileDialog fd = new FileDialog((Frame)null);
                fd.setVisible(true);
                
                JFileChooser fc = new JFileChooser();
                fc.showSaveDialog(null);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
于 2013-01-11T10:18:30.453 に答える
1
JFileChooser jl = new JFileChooser();
jl.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int save = jl.showSaveDialog(null);
if (JFileChooser.APPROVE_OPTION == save){
PrintWriter out = new PrintWriter(
                 new FileOutputStream(
                  new File(jl.getSelectedFile().getAbsolutePath()+"/name.txt"), 
                  true));
         out.println("serverText");
         out.close();
}
于 2013-01-11T11:08:55.747 に答える