pass command line arguments to copy a file name in Java

Is it possible to write a program from an IDE (such as NetBeans or Eclipse) that can compile from the command line and run once the user enters two arguments just after the name of the java class program to run? If so, can an absolute path be passed in as one of the arguments? And if so, (if the source or destination file has a multi-word descriptor, like Big Sky File.txt), how can it be passed in as an argument? I know this is a lot of questioning, but I have searched high and low and nothing seems to so much as scratch the surface on any of these topics. @Code-Guru, @thkala, here's the code I am attempting (took a bit to format): edit: @Code-Guru, I have added the offending line (not sure how I missed that). next edit: @ Code-Guru, here is the updated file contents from CopyFile.java, and the resulting error message: import java.io.*; public class CopyFile { public static void main(String args[]) throws IOException, NullPointerException { int num; FileInputStream fileIn; FileOutputStream fileOut; try { // open input file try { fileIn = new FileInputStream(args[0]); } catch(FileNotFoundException e) { System.out.println("Input File Not Found."); return; } // open output file try { fileOut = new FileOutputStream(args[1]); } catch(FileNotFoundException e) { System.out.println("Error Opening Output File."); return; } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Incorrect argument use:java CopyFile Source Destination"); return; } // Copy File try { do { num = fileIn.read(); if(num != -1) { fileOut.write(num); } } while(num != -1); } catch(IOException e) { System.out.println("File Error: Could not copy file."); } fileIn.close(); fileOut.close(); } } Here is the error message I receive from the command prompt: > Error: Could not find or load main class CopyFile
Yes it is possible. I suggest that you google something like "command line arguments java". There is lots of information out there to help you get started. Feel free to come back when you have a specific question.

以上就是pass command line arguments to copy a file name in Java的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏