简体   繁体   中英

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)

    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. These abstract concepts are java.io.File and java.nio.file.Path .

The File class represents a file in the file system whereas the interface Path represents the path string of the file. 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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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