【问题标题】:Java copying files to a new path doesn´t workJava将文件复制到新路径不起作用
【发布时间】:2015-08-29 21:38:49
【问题描述】:

我还有一个问题。现在我正在编写一个小程序,它可以在我的电脑和笔记本电脑上运行。这两个程序相互通信。我可以写字符串(比如聊天),我想发送文件。这个小聊天有​​效,但文件现在出现问题。这让我有点疑惑,因为几天前我已经开始运行它了。但现在它不起作用(不记得我改变了重要的事情)。不幸的是,我无法撤消,因为 Eclipse 已经关闭。

所以我一直在寻找错误,但几个小时以来我一直找不到它。我希望你能帮助我。

情况:

我在我的电脑/笔记本电脑上选择一个文件并将其发送到我的笔记本电脑/电脑(我以与文件相同的方式发送文本 [Strings] 并且它可以工作)。接收方应将文件保存在一个目录中(targetPath - 它在代码中的其他位置定义。它是我桌面上的一个文件夹)。所以我从“ObjectInputStream”获取文件作为对象并将其转换为“文件”:

 if(msg instanceof File){ //msg is the object I got from the ObjectInputStream
                        //its a file
                        model.copyFileTo((File) msg);
}

这就是麻烦的方法:

    public void copyFileTo(File file) throws IOException{
            System.out.println(file.getName());//this is just a test and it works. It prints out the name of the sended file
    if(targetPath.toFile().exists()){
        if(file.exists()){
            Path temp = Paths.get(targetPath+"/"+file.getName());
            if(!temp.toFile().exists()){
                Files.copy( file.toPath(), temp,  StandardCopyOption.REPLACE_EXISTING);
                System.out.println("copied");
            }else{
                System.out.println("File already exists");
            }
        }else{
            System.out.println("File doesnt exists");
        }
    }else{
        System.out.println("targetPath doesnt exists!");
    }
}

我没有出错,但它会打印“文件不存在”,所以“if(file.exists())”处的某些内容出错了。如果我把这部分剪掉,程序会挂在 Files.copy(...) 处,我知道这是因为它不会打印出“已复制”。

【问题讨论】:

  • "所以我从ObjectInputStream 获取文件作为对象并将其转换为File": Cast???无需铸造。对象输入流???你为什么要用那个? --- File 代表文件的名称,而不是内容,只需使用普通的InputStream 来读取字节
  • 我想我必须强制转换它,因为当我想将它保存在一个目录中时,我需要它作为文件。
  • ObjectInputStream 用于反序列化 Java 对象,很少使用。 --- File 对象包含文件的名称。文件的内容是 byte 数组(如果是二进制)或 String(如果是文本)。
  • 但是当我想将它保存在目录“Files.copy(file.toPath(), temp, StandardCopyOption.REPLACE_EXISTING);”中时,我需要它作为文件/路径。所以我在从 InputStream 中得到它之后再转换它。这是错的吗?

标签: java file-io objectinputstream


【解决方案1】:

在源系统上,你会做这样的事情(Java 7):

Path path = Paths.get("C:\\MyFolder", "MyFile.bin");
byte[] fileContent = Files.readAllBytes(path);
// send fileContent to target system

在目标系统上,你会这样做:

// receive fileContent from source system
Path path = Paths.get("C:\\Where\\To\\Store\\File", "MyFile.bin");
Files.write(path, fileContent);

在 Java 6 或更低版本中,您将使用 File 对象而不是 Path 对象并自己复制字节。

【讨论】:

  • 谢谢,我还没试过,但我会的。现在我将此文件作为对象从客户端 A 发送到服务器,并从服务器发送到客户端 B 作为对象。这听起来更容易。它不像我想要的那样工作吗?如果是,为什么不工作?
  • 让我再说一遍:File(或Path)对象存储文件的路径/名称不是内容。如果您序列化一个File 对象并将其发送到另一个系统并在其中反序列化它,那么您得到的只是文件名。
  • 好的,我明白了。我正在尝试将您的解决方案翻译成我的代码,这需要一段时间。非常感谢。我会在我成功的时候写。我只是想知道,因为它已经像我一样运行了,但现在它没有。我想你的建议是最好的方法
  • 好了,完成了一半。我可以发送文件,但我需要知道文件的名称或至少是类型。我想把你的建议和我的结合起来。所以我发送内容并发送名称......(就像我之前所做的那样)。这是一个好方法还是你有更好的解决方案?再次感谢
【解决方案2】:

我听从了安德烈亚斯的建议

在源系统上,你会做这样的事情(Java 7):

Path path = Paths.get("C:\\MyFolder", "MyFile.bin");
byte[] fileContent = Files.readAllBytes(path);
// send fileContent to target system

在目标系统上,你会这样做:

Path path = Paths.get("C:\\Where\\To\\Store\\File", "MyFile.bin");
Files.write(path, fileContent);

在 Java 6 或更低版本中,您将使用 File 对象而不是 Path 对象并自己复制字节。

我只想为其他人写下我的代码:

当我收到输入时调用此部分:

 if(msg instanceof Message){// Message is a self made class wich contains the byte[] as an object and the File/Path as an Object #
                        model.copyFileTo((byte[]) ((ChatMessage)msg).getMessage(), (File)((ChatMessage)msg).getName());
 }

方法:

    public void copyFileTo(byte[] bytes, File file) throws IOException{
    if(targetPath.toFile().exists()){
            Path temp = Paths.get(targetPath+"/"+file.getName());
            if(!temp.toFile().exists()){
                Files.write(temp, bytes);
                System.out.println("Wurde kopiert");
            }else{
                System.out.println("File already exists");
            }
    }else{
        System.out.println("targetPath doesnt exists!");
    }
}

感谢安德烈亚斯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-17
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2022-08-11
    • 2018-11-10
    • 1970-01-01
    相关资源
    最近更新 更多