Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > runtime.exec() How to copy all files ?

Reply
Thread Tools

runtime.exec() How to copy all files ?

 
 
k4
Guest
Posts: n/a
 
      08-09-2007
Witam

My class:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;


public class test {

public static void main( String [] args ) {
try{
Runtime runtime = Runtime . getRuntime();

String cmd ="/bin/cp /home/k/java/* /var/www/k/";
System.out.println(cmd);
Process process = runtime.exec(cmd);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bfr = new BufferedReader(isr);
String line;

while ( ( line = bfr . readLine() ) != null ) {
System.out.println(line);
}
}catch(Exception e){
}

}
}

If I use "*" to copy all files, then command doesn't work why? If I try
copy single file then everything is ok.
String cmd ="/bin/cp /home/k/java/single.txt /var/www/k/";

How to copy all files ? :/a
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      08-09-2007
On Thu, 09 Aug 2007 14:46:22 +0200, k4 wrote:
> If I use "*" to copy all files, then command doesn't work why? If I
> try copy single file then everything is ok. String cmd ="/bin/cp
> /home/k/java/single.txt /var/www/k/";
>
> How to copy all files ? :/a


"*" is a special character only to a command shell. The shell itself
expands "*" before passing the resulting file list to the cp.

Runtime.exec() does not use a shell to run the command, so cp sees a
literal "*" instead of the file list.

If you want to use shell features, run a shell:

String[] cmd = {
"/bin/sh",
"-c",
"/bin/cp /home/k/java/* /var/www/k/"
};

/gordon

--
 
Reply With Quote
 
 
 
 
Joe Attardi
Guest
Posts: n/a
 
      08-09-2007
k4 wrote:
> String cmd ="/bin/cp /home/k/java/* /var/www/k/";

Seems overkill to use Runtime.exec() just to copy files. Why not just
use a Reader and a Writer?


--
Joe Attardi

 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      08-09-2007
k4 wrote:

> Witam
>
> My class:
>
> import java.io.BufferedReader;
> import java.io.InputStream;
> import java.io.InputStreamReader;
>
>
> public class test {
>
> public static void main( String [] args ) {
> try{
> Runtime runtime = Runtime . getRuntime();
>
> String cmd ="/bin/cp /home/k/java/* /var/www/k/";
> System.out.println(cmd);
> Process process = runtime.exec(cmd);
> InputStream is = process.getInputStream();
> InputStreamReader isr = new InputStreamReader(is);
> BufferedReader bfr = new BufferedReader(isr);
> String line;
>
> while ( ( line = bfr . readLine() ) != null ) {
> System.out.println(line);
> }
> }catch(Exception e){
> }
>
> }
> }
>
> If I use "*" to copy all files, then command doesn't work why? If I try
> copy single file then everything is ok.
> String cmd ="/bin/cp /home/k/java/single.txt /var/www/k/";


Because "*" is only special to the shell, and you are not invoking a shell. In
your case the executable /bin/cp would try to copy a file called "*", and there
isn't one.

>
> How to copy all files ? :/a


Invoke a shell:

String[] cmd ={"/bin/sh", "-c", "/bin/cp /home/k/java/* /var/www/k/"};

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail :
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
Reply With Quote
 
=?UTF-8?B?QXJuZSBWYWpow7hq?=
Guest
Posts: n/a
 
      08-12-2007
Joe Attardi wrote:
> k4 wrote:
>> String cmd ="/bin/cp /home/k/java/* /var/www/k/";

> Seems overkill to use Runtime.exec() just to copy files. Why not just
> use a Reader and a Writer?


I would only use Reader & Writer if I knew all the files were
text files.

Byte oriented classes would be more general.

Arne
 
Reply With Quote
 
Joe Attardi
Guest
Posts: n/a
 
      08-12-2007
On Aug 11, 11:48 pm, Arne Vajhøj <a...@vajhoej.dk> wrote:
> I would only use Reader & Writer if I knew all the files were
> text files.
>
> Byte oriented classes would be more general.


Whoops, I wasn't even thinking. I meant FileInputStream/
FileOutputStream, not Reader/Writer.
Thanks Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
help me! how can i copy all files from one directory to another in java Kulbir Java 2 12-22-2005 01:15 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
using File.Copy to copy files to shared hosting site Steve Richter ASP .Net 4 04-18-2005 03:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57