5

私はすでにSOを調査し​​て答えを探しましたが、適切な答えを見つけることができませんでした。

jarからプログラムを起動するときは、jarファイルが置かれているディレクトリにフォルダを作成する必要があります。ユーザーがjarファイルをどこに保存するかは重要ではありません。

これが私が遊んでいた最新のコードです:ASystem.out.printlnは正しいディレクトリを出力しますが、フォルダは作成されません。対照的に、現在、すべてが私のSystem32フォルダーに保存されています。

    public static String getProgramPath() throws IOException{
    String currentdir = System.getProperty("user.dir");
    currentdir = currentdir.replace( "\\", "/" );
    return currentdir;

}

File dir = new File(getProgramPath() + "Comics/");//The name of the directory to create
    dir.mkdir();//Creates the directory
4

2 に答える 2

4

Jarのパスを取得することは、単にuser.dirディレクトリを取得するよりも少し難しい場合があります。理由の詳細を思い出せませんが、user.dirはすべての状況でこのパスを確実に返すわけではありません。どうしてもjarのパスを取得する必要がある場合は、少し黒魔術を実行して、最初にクラスのprotectionDomainを取得する必要があります。何かのようなもの:

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

import javax.swing.JOptionPane;

public class MkDirForMe {
   public static void main(String[] args) {
      try {
         String path = getProgramPath2();

         String fileSeparator = System.getProperty("file.separator");
         String newDir = path + fileSeparator + "newDir2" + fileSeparator;
         JOptionPane.showMessageDialog(null, newDir);

         File file = new File(newDir);
         file.mkdir();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   public static String getProgramPath2() throws UnsupportedEncodingException {
      URL url = MkDirForMe.class.getProtectionDomain().getCodeSource().getLocation();
      String jarPath = URLDecoder.decode(url.getFile(), "UTF-8");
      String parentPath = new File(jarPath).getParentFile().getPath();
      return parentPath;
   }
}

これでも動作が保証されているわけではなく、Jarのパスを取得できない場合があるという事実に自分自身を辞任する必要があります(たとえば、セキュリティ上の理由から)。

于 2012-06-23T04:34:22.967 に答える
2

いくつかの変更(コミックの前に「/」を追加するなど)を使用して、期待した場所にディレクトリを作成することができました。これが私が使用した完全なコードです。

import java.io.*;
public class TestClass {

        public static String getProgramPath() throws IOException{
                String currentdir = System.getProperty("user.dir");
                currentdir = currentdir.replace( "\\", "/" );
                return currentdir;

        }
        public static void main(String[] argv) {
                try {
                        String d = getProgramPath() + "/Comics/";
                        System.out.println("Making directory at " + d);
                        File dir = new File(d);//The name of the directory to create                                                                                      
                        dir.mkdir();//Creates the directory                                                                                                               
                }
                catch (Exception e) { System.out.println("Exception occured" + e);}
        }
}

今後、「/」などをハードコーディングしないでください。この場合、OSに何が正しいかを尋ねる組み込みライブラリを使用してください。これにより、機能が(簡単に)クロスプラットフォームで壊れることがなくなります。

もちろん、例外を適切にキャッチするなど。これは、コードを機能するものに成形するための迅速で汚い試みです。

于 2012-06-23T03:56:42.427 に答える