2017-06-29 59 views
1

我是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; 
} 

回答

0

正斜槓「/」必須被用來獲取文件路徑這裏。爲前>用途:

File f = new File("/Users/pavankumar/Desktop/Testing/Java.txt"); 
f.createNewFile(); 
相關問題