1
    open.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent e){
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new java.io.File("."));
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

            if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
                System.out.println(fileChooser.getSelectedFile());
                }
        }
    });

これは言葉遣いの悪い質問かもしれませんが、ここに行きます:

「fileChooser.getSelectedFile());」を生成するには、コードのこの部分が必要です。他の場所で使用できる形式で。それが変数であるかどうかは気にしません(別のactionListenerで呼び出す必要があるため、実際には機能しません)または(計画どおり)選択したフォルダーを文字列として出力ファイルに出力し、そのファイルを他の場所で読み取りますプログラムで。

ファイル パス (例: C:/Users/Desktop/) が文字列であることが重要です。これは、パスを使用するクラスがそれを取り込むためです。

私はいくつかのオプションを試しましたが、「変換できない型」のコンパイルエラーなどが頻繁に発生します。共有できるアイデアがあれば、それは素晴らしいことです

4

3 に答える 3

2

JFileChoose.getSelectedFile() は、String オブジェクトではなく File オブジェクトを返します。

File オブジェクトには、ファイル名とパスの文字列バージョンを返す getAbsolutePath()、getPath()、getName()、getParent() などのメソッドがあります。

次のようなものです:

File file = fileChooser.getSelectedFile();
System.out.println("Selected file is: "+file.getAbsolutePath()+"/"+file.getName());

あなたが望むものを手に入れるべきです。

また、参考までに、これはコンパイルされません...

String exportPath = fileChooser.getSelectedFile();

... getSelectedFile() によって返される File オブジェクトは String オブジェクトではないためです。ただし、 File オブジェクトには (すべてのオブジェクトと同様に) toString() メソッドがあり、これを行ったときに文字列を構築するために自動的に呼び出されます ...

String exportPath = fileChooser.getSelectedFile() +"\\";

私が言ったように、エレガントな方法は次のようなものです:

String exportPath = fileChooser.getSelectedFile().getAbsolutePath();

これが役に立てば幸いです、頑張ってください!ロブ

于 2011-02-13T14:53:33.400 に答える
1

それを行う可能性はほとんどありません。

// just path as a String
fileChooser.getSelectedFile().getPath();
// the same, this is done implicitly in your answer
fileChooser.getSelectedFile().toString();
// absolute path, if you need it
fileChooser.getSelectedFile().getAbsolutePath();
// canonical path, not sure what that is
fileChooser.getSelectedFile().getCanonicalPath();
于 2011-02-13T14:53:05.727 に答える
1
open.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent e){
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new java.io.File("."));
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

            if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
                try{
                DataOutputStream outputPath = new DataOutputStream(new FileOutputStream("C:/Users/Jonathan/Desktop/YouDetectJava/FolderPath.dat"));
                String exportPath = fileChooser.getSelectedFile() + "\\";
                outputPath.writeUTF(exportPath);
                //System.out.println(exportPath);
                }catch (IOException ioe){
                }
                }
        }
    });

それを修正するようです。自分の質問を投稿して回答して申し訳ありません。返信を待っている間に、その方法を考え出しました。文字列 'exportPath' には文字列が含まれている必要があるようです。この場合は "\" ですが、"" でもかまいません。

理由はわかりませんが、そこに行きます:D

于 2011-02-13T14:40:00.363 に答える