ファイルを保存する必要がある Java コードを書いています。
以下のコードはmac用です。
ImageIO.write(movie_image, "jpg",new File("/Users/sathyap/Desktop/movieimages/"+fileName+".jpg"));
Mac と Windows の両方で機能するハードコードされたディレクトリ構造「/Users/sathyap/Desktop/movieimages/」を指定する方法はありますか。
ファイルを保存する必要がある Java コードを書いています。
以下のコードはmac用です。
ImageIO.write(movie_image, "jpg",new File("/Users/sathyap/Desktop/movieimages/"+fileName+".jpg"));
Mac と Windows の両方で機能するハードコードされたディレクトリ構造「/Users/sathyap/Desktop/movieimages/」を指定する方法はありますか。
編集:あなたが探しているのは、状況ごとに個別にコーディングするのではなく、クロスプラットフォームに固執していると思うので、私の答えを完全に変更します。
System.getProperty("user.home")
プログラムが 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 はプラットフォームを処理します...ファイルは常に現在の作業ディレクトリに保存されます...それらのパスにアクセスするためのショートカットを提供し、それらをデスクトップに配置できます
ユーザーがそのディレクトリに書き込むプログラムを実行できる場合...ユーザーも問題なくファイルにアクセスできると仮定します