私はプログラミングにかなり新しいので、うまくいけば、これは意味があります。私は、特定のディレクトリにファイルを作成するJavaプログラムを作成しています。私のプログラムはWindows上で動作し、適切な場所にファイルを作成しますが、Macでは動作しません。バックスラッシュを単一のスラッシュに変更しようとしましたが、うまくいきません。 Macで動作するようにコードを変更するのはどうすればよいですか?私は以下のコードをいくつか入れました。MacとWindowsの間のパスの違いを解決する
ありがとうございます!ファイル置く場所でのユーザー入力を取得
try{
//Create file path
String dirpath = new ReWriterRunner().getPath()+"NewFiles";
//Create directory if it doesn't exist
File path = new File(dirpath);
if (!path.exists()) {
path.mkdir();
}
//Create file if it doesn't exist
File readme = new File(dirpath+"\\README.md");
if (!readme.exists()) {
readme.createNewFile();
}
方法:ファイルの新しいパスを作成します
クラス
public static String getPath(){
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter the directory name under which the project files are stored.");
System.out.println("Example: C:\\Users\\user\\work\\jhipstertesting)");
System.out.println("Use double slashes when typing.");
s = in.nextLine();
return s;
}
システムプロパティを使用すると、完全に機能しました。あなたの助けをありがとう! – avl7949