简体   繁体   English

在os x中复制.app文件

[英]Copy an .app file in os x

I'm trying to move an app file in OS X using the FileInputStream's transferTo method, but keep getting FileNotFoundException (No such file or directory). 我正在尝试使用FileInputStream的transferTo方法在OS X中移动应用程序文件,但不断获取FileNotFoundException(没有这样的文件或目录)。 I know this isn't the case because the .exists() method returns true when I run it on the app file. 我知道情况并非如此,因为当我在app文件上运行时,.exists()方法返回true。 I thought it had to do with File permissions but after a few tests that doesn't seem to be the case. 我认为这与文件权限有关,但经过几次测试似乎并非如此。 Here is the code I'm using to move the file: 这是我用来移动文件的代码:

public static void moveFile(File sourceFile, File destFile) throws IOException {
    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();

        long count = 0;
        long size = source.size();
        source.transferTo(count, size, destination);
    }
    finally {
        if(source!=null) {
            source.close();
        }

        if(destination!=null) {
            destination.close();
        }
    }
}

EDIT 1: The title has been changed from "Move an .app file in os x" to "Copy an .app file in os x". 编辑1:标题已从“将os x中的.app文件移动”更改为“在os x中复制.app文件”。 I need to keep the original file intact. 我需要保持原始文件不变。

EDIT 2: I was able to copy the file by means of the Apache Commons FileUtils as suggested by Joel Westberg (specifically the copyDirectory method). 编辑2:我能够通过Joel Westberg(特别是copyDirectory方法)建议的Apache Commons FileUtils复制文件。 The problem I'm facing now is when I go to run the copied app bundle the app bounces in the dock perpetually and never runs. 我现在面临的问题是当我去运行复制的应用程序包时,应用程序会永久地在Dock中弹跳并且永远不会运行。 Any ideas on why this is? 有关为什么会这样的想法?

EDIT 3: I have figured out how to fix the perpetually bouncing problem. 编辑3:我已经想出如何解决永久性的弹跳问题。 It turns out when I copied the app bundle that it didn't set 2 unix scripts to executable that needed to be. 事实证明,当我复制应用程序包时,它没有将2个unix脚本设置为需要的可执行文件。 I simply used the setExecutable method in the File class to fix this. 我只是使用File类中的setExecutable方法来解决这个问题。

The simple answer is that there is no such thing as a .app file in OSX terminology. 简单的答案是OSX术语中没有.app文件这样的东西。 The .app that you see is a folder. 您看到的.app是一个文件夹。 You'll get FileNotFound when trying to read it as a file, because it isn't a file. 尝试将其作为文件读取时,您将获得FileNotFound,因为它不是文件。

If you're on Java 7 you can use the new Files.move() method to perform the action you want regardless of it being a File or Folder. 如果您使用的是Java 7,则可以使用新的Files.move()方法执行所需的操作,而不管它是文件还是文件夹。

EDIT : As MadProgrammer suggested in the comments, it would be even easier to simply use File.renameTo() which has been around much longer. 编辑 :正如MadProgrammer在评论中所建议的那样,简单地使用File.renameTo()会更加容易。

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

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