繁体   English   中英

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

[英]common place to store files on mac and windows

正在编写需要保存文件的Java代码。

以下代码适用于Mac。

ImageIO.write(movie_image,“ jpg”,新文件(“ /Users/sathyap/Desktop/movieimages/"+fileName+".jpg”));

有没有一种方法可以使目录结构“ / Users / sathyap / Desktop / movieimages /”经过硬编码,从而适用于Mac和Windows。

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

System.getProperty("user.home")

假设该程序在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将负责平台工作……您的文件将始终保存在当前工作目录下……您可以提供快捷方式来访问这些路径并将其放置在桌面上

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

暂无
暂无

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

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