Hi!
I want to write an application on windows xp, which iterates n times a model script (exe) and copies every single run the results (multiple files such as text, maps, subfolders,etc) from the existing folder into a new one. The code works, but no files nor folders are copied. This is how far as I got being an absolute beginner in Java. Thanks for any help,
CB
[I]import java.io.*;
public class PCRmain {
public static void main(String[] args) throws InterruptedExce ption
{ /* specify number of model iterations... */
try {
File sim = new File("D:/eclipse/Hydro/sim");
File source = new File ("D:/eclipse/Hydro/PCRaster");
for (int j = 1; j < Integer.parseIn t(nb)+1; j++)
{ /* call PCRaster and specify model file .mod and path */
String command = "C:/PCRaster/apps/pcrcalc -f";
String arg = "D:/PCRaster/workspace/OPTI_test/OPTItest.mod";
Runtime runtime = Runtime.getRunt ime();
Process process = runtime.exec(co mmand + " " + arg);
process.waitFor ();
/* Copies results for every single run into "sim" folders */
if (source.isDirec tory())
{ if (!sim.exists())
{/* create j result folders called "sim" */
String folder_name = "sim_" + j;
String directory = "D:/eclipse/Hydro/" + folder_name;
File dir = new File(directory) ;
dir.mkdir();
String files[] = source.list();
for(int i = 0; i < files.length; i++)
{ copyDirectory(n ew File(source, files[i]), new File(sim, files));}}
else
{ if(source.exist s())
{System.out.pri ntln("File or directory does not exist.");
System.exit(0); }
else {
InputStream in = new FileInputStream (source);
OutputStream out = new FileOutputStrea m(sim);
/* 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();}
}}}}
catch(IOExcepti on e)
{ javax.swing.JOp tionPane.showCo nfirmDialog(nul l,
"The .exe can't be found.","Verify path.",
javax.swing.JOp tionPane.PLAIN_ MESSAGE);
return;
}}
I want to write an application on windows xp, which iterates n times a model script (exe) and copies every single run the results (multiple files such as text, maps, subfolders,etc) from the existing folder into a new one. The code works, but no files nor folders are copied. This is how far as I got being an absolute beginner in Java. Thanks for any help,
CB
[I]import java.io.*;
public class PCRmain {
public static void main(String[] args) throws InterruptedExce ption
{ /* specify number of model iterations... */
try {
File sim = new File("D:/eclipse/Hydro/sim");
File source = new File ("D:/eclipse/Hydro/PCRaster");
for (int j = 1; j < Integer.parseIn t(nb)+1; j++)
{ /* call PCRaster and specify model file .mod and path */
String command = "C:/PCRaster/apps/pcrcalc -f";
String arg = "D:/PCRaster/workspace/OPTI_test/OPTItest.mod";
Runtime runtime = Runtime.getRunt ime();
Process process = runtime.exec(co mmand + " " + arg);
process.waitFor ();
/* Copies results for every single run into "sim" folders */
if (source.isDirec tory())
{ if (!sim.exists())
{/* create j result folders called "sim" */
String folder_name = "sim_" + j;
String directory = "D:/eclipse/Hydro/" + folder_name;
File dir = new File(directory) ;
dir.mkdir();
String files[] = source.list();
for(int i = 0; i < files.length; i++)
{ copyDirectory(n ew File(source, files[i]), new File(sim, files));}}
else
{ if(source.exist s())
{System.out.pri ntln("File or directory does not exist.");
System.exit(0); }
else {
InputStream in = new FileInputStream (source);
OutputStream out = new FileOutputStrea m(sim);
/* 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();}
}}}}
catch(IOExcepti on e)
{ javax.swing.JOp tionPane.showCo nfirmDialog(nul l,
"The .exe can't be found.","Verify path.",
javax.swing.JOp tionPane.PLAIN_ MESSAGE);
return;
}}
Comment