How to display the file name & status when copying files.

I write a program to backup/copy data from one directory to other directory, and I'm using File List and Runtime.getRuntime().exec("xcopy "+dir1+"\\"+name+" "+dir2+" /y");

to copy the files, so it will copy the file one by one, and everytime one file is copied, I want to display the result to the screen, but guess what has happened, the screen just display a blank screen, and wait until all the copying processes finish, then it will display all files that has been copied.

Appreciate your expertise to check my coding below.

String[] fileList = fbackupSource.list();

boolean done=true;

JTextArea backupText = new JTextArea();

JScrollPane backupScroll = new JScrollPane(backupText,

ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

JFrame backupWindow = new JFrame("Backup Data");

backupWindow.setBounds(320,240,400,330);

backupWindow.getContentPane().add(backupScroll);

backupText.setText("Copy files from "+backupSource.toUpperCase()+" to "+backupDest.toUpperCase()+" : \n\n");

backupWindow.show();

for (int i=0; i < fileList.length; i++) {

backupText.append(" ("+ i +")... processing " + fileList);

done = copyFile(fileList,backupSource,backupDest);

if (done=true) {

backupText.append(" -> done. \n");

}

else {

backupText.append(" -> fail! \n");

}

}

boolean copyFile(String name, String dir1, String dir2) {

try {

java.lang.Process proc =

Runtime.getRuntime().exec("xcopy "+dir1+"\\"+name+" "+dir2+" /y");

proc.waitFor();

return true;

}

catch (IOException e) {

return false;

}

catch (InterruptedException e) {

return false;

}

}

[1845 byte] By [johan_ak] at [2007-9-26 1:54:32]
# 1
Runtime.exec() returns a Process object... you can call Process.getOutputStream() and read the command output from that stream.Yours,Tom
tcopeland at 2007-6-29 3:07:05 > top of Java-index,Archived Forums,Java Programming...
# 2
Thanks for your info.I tried to get more information about the getOutputStream(), but not many sources explain this clearly. Anybody have a sample procedure that explain how to use this and to display it into the JTextArea?
johan_ak at 2007-6-29 3:07:05 > top of Java-index,Archived Forums,Java Programming...
# 3

Oops, sorry, I should have written "Process.getInputStream()". Here's an example from:

http://javafaq.nu/tips/opsys/index.shtml

==============

How can I pass a string to the command line(DOS) ? Also i want to capture the output given by the command line in a string.

Answer: Try this out:

// works for DOS

String cmds[] = new String[2];

cmds[0] = "dir"; // replace with "ls" on UNIX

cmds[1] = "c:"; // replace with "/" on UNIX

// execute the command

Process pro = Runtime.getRuntime().exec(cmds);

// wait until it's done executing

pro.waitFor();

// what did the process output from the Input pipe back to

// this process (okay, who named this stuff)?

InputStream out = pro.getInputStream();

// output it (really slowly)

int i;

while ((i = out.read()) != -1) System.out.println((char) i);

==============

Although I don't think this example will do exactly what you want... you might have to delete the waitFor() call and just check to see when there's nothing else to read from the InputStream. It's a start, though, and the web page has a lot of similar examples of this sort of thing.

Yours,

Tom

tcopeland at 2007-6-29 3:07:05 > top of Java-index,Archived Forums,Java Programming...