0

ファイルを保存する必要がある Java コードを書いています。

以下のコードはmac用です。

ImageIO.write(movie_image, "jpg",new File("/Users/sathyap/Desktop/movieimages/"+fileName+".jpg"));

Mac と Windows の両方で機能するハードコードされたディレクトリ構造「/Users/sathyap/Desktop/movieimages/」を指定する方法はありますか。

4

2 に答える 2

0

編集:あなたが探しているのは、状況ごとに個別にコーディングするのではなく、クロスプラットフォームに固執していると思うので、私の答えを完全に変更します。

System.getProperty("user.home")
于 2010-09-17T05:45:32.253 に答える
0

プログラムが Mac と Windows のいずれかのディレクトリ (X) の下で実行されると仮定します。次のようにコーディングできます。

//This will give the current working directory
String directoryPath = System.getProperty("user.dir");

//now get the platform dependent path
String filePath = directoryPath + File.separator + "data" + File.separator + "filename";

File fileToWriteTo = new File(filePath);

File.separator はプラットフォームを処理します...ファイルは常に現在の作業ディレクトリに保存されます...それらのパスにアクセスするためのショートカットを提供し、それらをデスクトップに配置できます

ユーザーがそのディレクトリに書き込むプログラムを実行できる場合...ユーザーも問題なくファイルにアクセスできると仮定します

于 2010-09-17T05:51:43.823 に答える