Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Arnold Scott   on Jul 10 In Java Category.

  
Question Answered By: Terence Mitchell   on Jul 10

You can use command line commands from withing Java using the
Runtime.exec() method, but you should try to avoid it at all costs. The
main reason to avoid it is because it makes your application much less
portable. You should try instead to use the functionality in the Java
API or extend it to provide this functionality.

For example, if you want to copy one file  to another in Java you could
do something like:

FileInputStream in = new FileInputStream("filename1");
FileOutputStream out = new FileOutputStream("filename2");

int b = -1;
while((b = in.read()) != -1){
out.write(b);
}

out.close();
in.close();


You can do a lot to make this better though, such as buffering your
input and output data, checking to see if files exist before reading and
writing, catching exceptions, etc.

Share: 

 

This Question has 4 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Could not locate compiled file Or get search suggestion and latest updates.


Tagged: