检查远程计算机上是否存在目录并在其上复制文件

时间:2011-08-18 06:29:26

标签: java file copy

我在远程计算机M上有一个共享文件夹F.现在我想在我的本地计算机上运行一个程序,它可以执行以下操作。

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.

目前我使用以下方法复制文件,但我无法找出文件夹复制逻辑

 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");

1 个答案:

答案 0 :(得分:0)

如果我不得不猜测:

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

应改为:

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

你需要正确地逃避这些反斜杠。另一个(可能更容易?)选项是将远程计算机映射为网络驱动器并更方便地访问它,例如:

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