2017-06-29 54 views
1

私はプログラミングにかなり新しいので、うまくいけば、これは意味があります。私は、特定のディレクトリにファイルを作成する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

あなたが現在https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

で.. 詳細上で動作しているが、私はNIOを使用して希望のシステムを識別するために、システムのプロパティを使用することができます。それはあなたの選択です

https://docs.oracle.com/javase/tutorial/essential/io/fileio.html
+0

システムプロパティを使用すると、完全に機能しました。あなたの助けをありがとう! – avl7949

0

フォワードスラッシュ「/」はファイルパスを取得するために使用されなければなりませんここに。 。元>の使用のために:

File f = new File("/Users/pavankumar/Desktop/Testing/Java.txt"); 
f.createNewFile(); 
関連する問題