3

私はいくつかのJavaコードを持っています:

public static String getSaveFilePath(String title2)
  {
  FileDialog fd = new FileDialog(new Frame(), "Save As...", 1);
    fd.setFilenameFilter(new FilenameFilter() {
      public boolean accept(File dir, String name) {
        return name.endsWith(".mp3");
      }
    });
    fd.setFile(title2 + ".mp3");
    fd.setVisible(true);
    String str3 = fd.getFile();
    String str4 = fd.getDirectory();
    if (str4 == null) return null;
    str3 = str3.replace(".mp3", "");
    str3 = str3 + ".mp3";
    String str5 = str3;
    File localFile = new File(str4, str5);
    return localFile.getPath();
}

問題はsetFile()、ファイル名を

NBA - In the Zone P.L. (Video by JESSExAKAxViCiOUS)

ダイアログが表示される前に、ダイアログが表示されたら、ファイルを として保存し、もう一度abc.mp3 使用します。getFile()SExAKAxViCiOUS).mp3abc.mp3

誰かが私がここで間違っていることを教えてもらえますか? また、ファイルの保存ダイアログを表示するためのより優れたクロスプラットフォーム ソリューションがあれば、私と共有してください。

4

3 に答える 3

0

機能getSaveFilePathは良好で、期待どおりに機能します。問題はおそらくそれをどのように使用するかにあります。この関数を呼び出すコードを投稿していただけませんか?

これが私がそれを呼んだ方法です:

public class FileDialogTest
{
    public static String getSaveFilePath(String title2)
    {
        FileDialog fd = new FileDialog (new Frame (), "Save As...", 1);
        fd.setFilenameFilter (new FilenameFilter ()
        {
            public boolean accept (File dir, String name)
            {
                return name.endsWith (".mp3");
            }
        });
        fd.setFile (title2 + ".mp3");
        fd.setVisible (true);
        String str3 = fd.getFile ();
        String str4 = fd.getDirectory ();
        if (str4 == null)
            return null;
        str3 = str3.replace (".mp3", "");
        str3 = str3 + ".mp3";
        String str5 = str3;
        File localFile = new File (str4, str5);
        return localFile.getPath ();
    }

    public static void main (String [] args) throws Exception
    {
        String title = "NBA - In the Zone P.L. (Video by JESSExAKAxViCiOUS)";
        title = getSaveFilePath (title);
        System.out.println (title);
    }
}
于 2013-02-20T06:05:08.747 に答える