我是Stack Overflow的新手,對於編程來說相當新穎,所以希望這很有道理。我正在寫一個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