我正在编写需要保存文件的 java 代码。
以下代码适用于 mac。
ImageIO.write(movie_image, "jpg",new File("/Users/sathyap/Desktop/movieimages/"+fileName+".jpg"));
有没有办法我可以给目录结构“/Users/sathyap/Desktop/movieimages/”硬编码,适用于mac和windows。
我正在编写需要保存文件的 java 代码。
以下代码适用于 mac。
ImageIO.write(movie_image, "jpg",new File("/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 将负责平台...您的文件将始终保存在当前工作目录下...您可以提供访问这些路径的快捷方式并将它们放在桌面上
如果用户可以执行写入该目录的程序......我将假设用户也可以毫无问题地访问文件