简体   繁体   English

如何使用Windows,Mac和Linux的通用文件路径保存文件?

[英]How can I save a file with a generic file path for Windows, Mac and Linux?

I have an aplicattion that needs to create a folder to unzip some files, but I want to pass a file path that my application could run, create and save my files in any OS, but I don't know what should be the output Path to do that. 我有一个aplicattion需要创建一个文件夹解压缩一些文件,但我想传递一个文件路径,我的应用程序可以运行,创建和保存我的文件在任何操作系统,但我不知道应该是什么输出路径要做到这一点。

for example: 例如:

File folder = new File(outputFolder);
if (!folder.exists()) {
    folder.mkdir();
}

What path should I use in outputFolder to create my folder in Documents/UnzipTest in any Operation System (Windows, mac or linux)? 我应该在outputFolder中使用什么路径在任何操作系统(Windows,Mac或Linux)的Documents / UnzipTest中创建我的文件夹?

System.getProperty("user.dir");

Or if you're using Java 7 或者如果您使用的是Java 7

Filesystems.getDefault();

will give you the base directory from which to work. 将为您提供工作的基本目录。

Use java.io.File#separator : 使用java.io.File#separator

String userHome = System.getProperty("user.home");
String outputFolder = userHome + File.separator + "dir1";
File folder = new File(outputFolder);
if (!folder.exists()) {
   folder.mkdir();
}

I would define a property (in some external properties file, for example) for the root folder, so user can define what is the root folder after the setup or during the installation for all your operations. 我将为文件夹定义一个属性(例如,在某些外部属性文件中),因此用户可以在安装后或安装期间为所有操作定义根文件夹。

Other option is to use user folder (aka home folder ) as this root folder (or if above property is not defined), defined in system property user.dir . 其他选项是使用用户文件夹 (也称为主文件夹 )作为此根文件夹(或者如果未定义上面的属性),在系统属性user.dir定义。 Or use folder where your application is installed - you can find it eg by finding some resource in application jar and then parsing path from returned URL. 或者使用安装了应用程序的文件夹 - 您可以通过在应用程序jar中查找某些资源然后从返回的URL解析路径来找到它。

With root folder defined, you can use constructor new File(rootFolder, outputFolder) ; 在定义了根文件夹后,可以使用构造函数new File(rootFolder, outputFolder) ; where you can simply use slashes for the path in outputFolder (eg /foo/bar). 你可以在outputFolder使用斜杠作为路径(例如/ foo / bar)。 Such path would work for all OSes. 这样的路径适用于所有操作系统。

You can use Commons IO from Apache Commons with the classes FileUtils and FilenameUtils . 您可以使用Apache Commons的 Commons IOFileUtils以及FilenameUtils类。

If you use in your application the UNIX style for your paths, you can transform the path to SO style with the method: 如果在应用程序中使用路径的UNIX样式,则可以使用以下方法将路径转换为SO样式:

org.apache.commons.io.FilenameUtils.separatorsToSystem(String)

If you want create a directory only if not exists: 如果要创建目录,则仅在不存在的情况下:

org.apache.commons.io.FileUtils.forceMkdir(File)

May you have a path to the directory and may you need a new folder in this directory, you can cancatenate using the method for get the full path: 如果你有一个目录路径,可能需要在这个目录中有一个新文件夹,你可以使用该方法获取完整路径:

org.apache.commons.io.FilenameUtils.concat(String, String)

The path returned path is always normalized, contain separators in the format of the system. 路径返回路径始终规范化,包含系统格式的分隔符。

May you need the user directory path: 您可能需要用户目录路径:

org.apache.commons.io.FileUtils.getUserDirectory()
String outputFolder = System.getProperty("user.home") + File.separator + "Documents" + File.separator +"TestandoUnzip";

The code that I needed. 我需要的代码。 Thanks guys. 多谢你们。

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

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