【发布时间】:2017-12-03 12:14:23
【问题描述】:
我是 Stack Overflow 的新手,对编程也很陌生,所以希望这是有道理的。我正在编写一个在特定目录中创建文件的 java 程序。我的程序在 Windows 上运行并在正确的位置创建一个文件,但它在 Mac 上不起作用。我尝试将反斜杠更改为单个正斜杠,但这不起作用。我应该如何更改代码以使其适用于 Mac 或两者都适用?我在下面放了一些代码。
提前致谢!
为文件创建新路径的类:
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;
}
【问题讨论】:
标签: java file path directory bufferedwriter