File 基于平台创建动态文件路径
我有以下代码将一个文件夹中的所有文件移动到另一个文件夹:File 基于平台创建动态文件路径,file,grails,groovy,File,Grails,Groovy,我有以下代码将一个文件夹中的所有文件移动到另一个文件夹: for(File file: sourcePath.listFiles()){ log.debug("File = " + sourcePath + "\\" + file.getName()) File f1 = new File("C:\\\\" + sourcePath + "\\" + file.getName()) f1.renameTo(new File("C:\\\\" + destinationPa
for(File file: sourcePath.listFiles()){
log.debug("File = " + sourcePath + "\\" + file.getName())
File f1 = new File("C:\\\\" + sourcePath + "\\" + file.getName())
f1.renameTo(new File("C:\\\\" + destinationPath + "\\" + file.getName()))
}
这在本地运行良好,因为我使用的是windows计算机
显然,当我将我的应用程序部署到unix测试/生产服务器时,它将不起作用
这在Grails2.1.0项目中
有没有可能不用条件语句就可以做到这一点?(一些开发人员将在本地使用linux)
更新
我必须使用Java6
谢谢将为您提供一个依赖于系统的分隔符,“/”
用于类unix,而“\”
用于windows。具有相同的内容,但在char
type中
此外,如果您可以使用Java 7,NIO2的Path API提供了更方便、更简洁的方法:
Path source = Paths.get("C:", sourcePath, file.getName());
Path target = Paths.get("C:", targetPath, file.getName());
Files.move(source, target);
有关文档,请参阅以下页面:
将为您提供一个依赖于系统的分隔符,“/”
用于类unix,而“\”
用于windows。具有相同的内容,但在char
type中
此外,如果您可以使用Java 7,NIO2的Path API提供了更方便、更简洁的方法:
Path source = Paths.get("C:", sourcePath, file.getName());
Path target = Paths.get("C:", targetPath, file.getName());
Files.move(source, target);
有关文档,请参阅以下页面:
工作解决方案:
File sourcePath = new File(config.deals.imageUploadTmpPath + "/test_" + testId)
File destinationPath = new File(config.deals.imageUploadPath + "/" + testId)
for(File file: sourcePath.listFiles()) {
log.debug("File = " + sourcePath.getAbsolutePath() + File.separator + file.getName())
File f1 = new File(sourcePath.getAbsolutePath() + File.separator + file.getName())
f1.renameTo(new File(destinationPath.getAbsolutePath() + File.separator + file.getName()))
}
使用就可以了。工作解决方案:
File sourcePath = new File(config.deals.imageUploadTmpPath + "/test_" + testId)
File destinationPath = new File(config.deals.imageUploadPath + "/" + testId)
for(File file: sourcePath.listFiles()) {
log.debug("File = " + sourcePath.getAbsolutePath() + File.separator + file.getName())
File f1 = new File(sourcePath.getAbsolutePath() + File.separator + file.getName())
f1.renameTo(new File(destinationPath.getAbsolutePath() + File.separator + file.getName()))
}
使用就可以了。但这对
C:
没有帮助。也许环境变量或与sourcePath
相关的路径可以做到这一点。@hsan rightFileSystems.getDefault()
将为您提供默认的系统文件系统(“C:\”或“/”)。是的,java.nio.file.FileSystems.default.rootDirectories
似乎是一个很好的起点。幸运的是,我一直使用java 6。Java 6中是否有与FileSystems.getDefault()等效的文件?@ThomasBuckley我宁愿使用特定于平台的配置文件来定义工作目录,并从中扩展其他路径。否则,从新文件(“.”)开始调用getParentFile()
,直到它返回null
,怎么样?它将是当前上下文的根目录。但这对C:
没有帮助。也许环境变量或与sourcePath
相关的路径可以做到这一点。@hsan rightFileSystems.getDefault()
将为您提供默认的系统文件系统(“C:\”或“/”)。是的,java.nio.file.FileSystems.default.rootDirectories
似乎是一个很好的起点。幸运的是,我一直使用java 6。Java 6中是否有与FileSystems.getDefault()等效的文件?@ThomasBuckley我宁愿使用特定于平台的配置文件来定义工作目录,并从中扩展其他路径。否则,从新文件(“.”)开始调用getParentFile()
,直到它返回null
,怎么样?它将是当前上下文的根目录。这可能更为groovy,例如:File f1=new File(${sourcePath.absolutePath}${File.separator}${File.name}”)
和f1.renameTo(new File(${destinationPath.absolutePath}${File.separator}${File.name}”)
这可能更为groovy,例如:File f1=new File(“${sourcePath.absolutePath}${File.separator}${File.name}”)
和f1.renameTo(新文件(${destinationPath.absolutePath}${File.separator}${File.name}”)