简体   繁体   English

Mac上文件的路径:FileNotFoundException

[英]Path to file on a Mac: FileNotFoundException

I'm working on a mac and I'm trying to specify the path to a file on my desktop. 我正在使用Mac,我正在尝试在桌面上指定文件的路径。

I just did it like this: File file = new File("/users/desktop/sample.json"); 我只是这样做: File file = new File("/users/desktop/sample.json");

When I tried running it I got a FileNotFoundException. 当我尝试运行它时,我得到了一个FileNotFoundException。

How do I correctly specify the path? 如何正确指定路径?

Mac OS X's filesystem is case sensitive. Mac OS X的文件系统区分大小写。 Users and Desktop should start with capital letters, and your filename should also match case too. UsersDesktop应以大写字母开头,您的文件名也应与大小写匹配。

Since you're looking for your desktop folder and not the root folder of a user with the name desktop , you need to add your username after the Users folder. 由于您正在查找桌面文件夹而不是名为desktop的用户的根文件夹,因此您需要在Users文件夹之后添加用户名。 For example: 例如:

File file = new File("/Users/LuxuryMode/Desktop/sample.json");

To get to know your correct path to be given, drag n drop your file in Mac terminal , which would give you the path of the file which would look like ex:/Users/Smith/Desktop/PI/ABApBASICS.docx 要了解正确的路径,请将文件拖放到Mac terminal ,这将为您提供文件的路径,如下所示ex:/Users/Smith/Desktop/PI/ABApBASICS.docx

All you got to do is paste this code in Eclipse but mind you blackslash would throw you an error: invalid escape sequence solution: ex://Users//Harish//Desktop//PI//ABApBASICS.docx I was able to read the file without error inputting above path. 你所要做的就是将这段代码粘贴到Eclipse中,但是请注意你的blackslash会给你一个错误: invalid escape sequence解决方案: ex://Users//Harish//Desktop//PI//ABApBASICS.docx我能阅读文件没有错误输入上面的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM