简体   繁体   English

如何将文件从一台PC复制到另一台PC

[英]How to copy file from one PC to another

I am trying this code to transfer file from my computer to another computer but i am getting Exception java.io.FileNotFoundException: \\192.168.1.4\\D:\\Color.txt (The network name cannot be found) 我正在尝试使用此代码将文件从我的计算机传输到另一台计算机,但出现异常java.io.FileNotFoundException:\\ 192.168.1.4 \\ D:\\ Color.txt(找不到网络名称)

    File source = new File("G:\\Color.txt");

    File dest = new File("\\\\192.168.1.4\\D:\\Color.txt");
 // File dest = new File("D:\\Color.txt");


    try {

        InputStream input = new FileInputStream(source);

        OutputStream output = new FileOutputStream(dest);

        byte[] buf = new byte[1024];

        int bytesRead;

        while ((bytesRead = input.read(buf)) > 0) {

            output.write(buf, 0, bytesRead);

        }
        System.out.println("File Copied successfully");
        input.close();
        output.close();

    } 
    catch(Exception e)
    {
          System.out.println("Exception "+e);
    }

A file or a directory in the file system is represented by two abstract concepts in java. 文件系统中的文件或目录由java中的两个抽象概念表示。 These abstract concepts are java.io.File and java.nio.file.Path . 这些抽象概念是java.io.Filejava.nio.file.Path

The File class represents a file in the file system whereas the interface Path represents the path string of the file. File类表示文件系统中的文件,而接口Path表示文件的路径字符串。 In this tutorial we look at various operations on File or Path. 在本教程中,我们研究文件或路径上的各种操作。 We get a handle on the File using 我们使用

Syntax : 句法 :

File file = new File("c:\\filefolder\\file.txt");

But in your case first check whether location is available through file explorer,and use the same address. 但在您的情况下,请先通过文件资源管理器检查位置是否可用,然后使用相同的地址。

在此处输入图片说明

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

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