简体   繁体   中英

checking if a directory exists on a remote machine and copying the file on it

I have a shared folder F on a remote machine M. Now I want to run a program on my local machine which does following.

Check if subfoder  S exists with \\remoteMachine\F
   if S exists then copy my file tstfile.txt within S
   else if S does not exist then 
      create S at \\remoteMachine\F and 
      copy tstfile.txt within S.

Currently I am using the following to copy file, but I can't figure out the folder copy logic

 InputStream in = new FileInputStream(new File("C:\\testData\\aks.txt"));
 OutputStream out = new FileOutputStream(new File("\\remotemachine\\tst.txt"));

 //Transfer bytes from in to out
 byte[] buf = new byte[1024];
 int len;
 while ((len = in.read(buf)) > 0) {
     out.write(buf, 0, len);
 }
 in.close();
 out.close();
 System.out.append("done with copying");

If I had to guess:

OutputStream out = new FileOutputStream(new File("\\remotemachine\\tst.txt"));

Should instead be:

OutputStream out = new FileOutputStream(new File("\\\\remotemachine\\tst.txt"));

You need to escape those backslashes properly. The other (potentially easier?) option is to map the remote machine as a network drive and access it more conveniently such as:

OutputStream out = new FileOutputStream(new File("M:\tst.txt"));

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