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 FileOutputStrea开发者_Go百科m(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"));
精彩评论