Mac 和 Windows 上存储文件的常见位置

发布于 2024-09-24 06:49:10 字数 233 浏览 3 评论 0原文

我正在编写一个需要保存文件的java代码。

以下代码适用于 Mac。

ImageIO.write(movie_image, "jpg",new File("/Users/sathyap/Desktop/movieimages/"+文件名+".jpg"));

有没有办法可以给出目录结构“/Users/sathyap/Desktop/movieimages/” 硬编码适用于 Mac 和 Windows。

am writing a java code that needs to save a file.

the below code is for mac.

ImageIO.write(movie_image, "jpg",new File("/Users/sathyap/Desktop/movieimages/"+fileName+".jpg"));

is there a way i can give the directory structure "/Users/sathyap/Desktop/movieimages/"
hardcoded that works for both mac and windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2024-10-01 06:49:10

假设该程序在 Mac 和 Windows 中的目录 (X) 下运行。您可以这样编码:

//This will give the current working directory
String directoryPath = System.getProperty("user.dir");

//now get the platform dependent path
String filePath = directoryPath + File.separator + "data" + File.separator + "filename";

File fileToWriteTo = new File(filePath);

File.separator 将处理平台...您的文件将始终保存在当前工作目录下...您可以提供访问这些路径的快捷方式并将它们放在桌面上

如果用户可以执行写入该目录的程序...我假设用户也可以毫无问题地访问这些文件

Assuming that the program runs under a directory (X) in either Mac and Windows. You can code it like this:

//This will give the current working directory
String directoryPath = System.getProperty("user.dir");

//now get the platform dependent path
String filePath = directoryPath + File.separator + "data" + File.separator + "filename";

File fileToWriteTo = new File(filePath);

File.separator will take care of platform ... your files will always be saved under current working direcoty ... you can provide shortcuts to access those paths and place them on desktop

If a user can execute the program that writes to that directory ... I will assume that the user can also access the files without problem

錯遇了你 2024-10-01 06:49:10

编辑:完全改变我的答案,因为我认为您正在寻找的是坚持跨平台而不是分别针对每种情况进行编码。

System.getProperty("user.home")

EDIT: Totally changing my answer because I think what you're looking for is sticking to cross-platform rather than coding for each situation separately.

System.getProperty("user.home")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文