wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Copying file from one PC to the other PC on  a network. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Copying file from one PC to the other PC on  a network." Watch "Copying file from one PC to the other PC on  a network." New topic
Author

Copying file from one PC to the other PC on a network.

swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
HI
Iam unable to move /copy a file from one machine to other on the network using this code.
class MoveFile
{
public static void main(String ar[])
{
try
{
Runtime rn=Runtime.getRuntime();
Process move =rn.exec("cmd /C move C:/text.txt I:/Test/text.txt") ;
move.waitFor() ;
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
}
Where C is the localdrive and I: is the mapped drive of the second machine.
This code doesnt throw any Runtime exception but the file is not coppied .

Thanks
Swapna.


SCPJ2
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Hi Swapna,
According to what you have posted, I don't think the command you have written:

would work from the Windows command prompt either -- although I admit I haven't tried it myself.
[Note: I assume you are on a Windows platform.]
As far as I know, the backslash character ("\") is used as the path separator on Windows platforms -- and not the [forward] slash character ("/") as in the code snippet you have supplied. However, remember that java interprets the backslash as an "escape" character, so your command line should be like this (I think):

Again, I have not tested this code.
However, I would also like to point out, that with mapped, network drives, you can use pure java to copy the files (rather than using the "exec()" method in class "Runtime"), using the classes in the "java.io" package. Assuming the file you wish to copy is a text file, you can use the "java.io.BufferedReader" and "java.io.BufferedWriter" classes. Here is some sample code (which is untested):

Again, the above code is completely untried. Please check the relevant javadocs first, in order to verify the above.
Hope this helps.
Good Luck,
Avi.
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Swapna,
I just remembered. If you prefer using the "exec()" method (in "java.lang.Runtime"), then I suggest you read this JavaWorld article (assuming you haven't already):
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Good Luck,
Avi.
 
wood burning stoves
 
subject: Copying file from one PC to the other PC on a network.
 
Similar Threads
Display text file in server from client
running another application from java
Can you start an application on the server by calling a servlet
File creation
Read Text File