简体   繁体   English

传递命令行参数以在Java中复制文件名

[英]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? 是否可以从IDE(例如NetBeans或Eclipse)编写可以从命令行编译并在用户输入要在要运行的Java类程序名称之后输入两个参数的情况下运行的程序?

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? 如果是这样(如果源文件或目标文件具有多字描述符,例如Big Sky File.txt),如何将其作为参数传递?

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): @ Code-Guru,@ thkala,这是我正在尝试的代码(花一些时间格式化):

edit: @Code-Guru, I have added the offending line (not sure how I missed that). 编辑:@ Code-Guru,我添加了有问题的行(不确定我是如何错过的)。

next edit: @ Code-Guru, here is the updated file contents from CopyFile.java, and the resulting error message: 下一个编辑:@ Code-Guru,这是CopyFile.java中更新的文件内容,以及由此产生的错误消息:

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 错误:找不到或加载主类CopyFile

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? 是否可以从IDE(例如NetBeans或Eclipse)编写可以从命令行编译并在用户输入要在要运行的Java类程序名称之后输入两个参数的情况下运行的程序?

Yes, it is possible. 对的,这是可能的。

If so, can an absolute path be passed in as one of the arguments? 如果是这样,可以将绝对路径作为参数之一传入吗?

Definitely - any string can be passed as an argument , as long as its size does not exceed a certain (usually system-defined) limit. 绝对- 任何字符串都可以作为参数传递 ,只要其大小不超过某个(通常是系统定义的)限制即可。

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? 如果是这样(如果源文件或目标文件具有多字描述符,例如Big Sky File.txt),如何将其作为参数传递?

Typically, arguments with spaces, special characters etc are handled by the calling shell, rather than the Java program. 通常,带有空格,特殊字符等的参数由调用外壳程序而不是Java程序处理。 Consult your documentation on how to properly quote such arguments in whatever command line shell you are using. 请查阅您的文档,以了解如何在使用的任何命令行shell中正确引用此类参数。

The answer lies in this part of the error message: 答案在于错误消息的这一部分:

(wrong name: copyfile/CopyFile)

Ie, your class's fully qualified name is copyfile.CopyFile . 即,您的班级的全名是copyfile.CopyFile Java requires that the package layout match the folder layout on disk so it knows how to find things. Java要求程序包布局与磁盘上的文件夹布局匹配,以便知道如何查找内容。

So either your class file must be in a subfolder called copyfile , and then invoked as java copyfile.CopyFile , or you can compile your class in the unnamed default package by removing the package copyfile; 因此,您的类文件必须位于一个名为copyfile的子文件夹中,然后作为java copyfile.CopyFile进行调用,或者您可以通过删除package copyfile;来在未命名的默认包中编译您的类package copyfile; declaration from the code (you haven't shown that line, but it must be there). 代码中的声明(您尚未显示该行,但必须在该行中)。

More info: http://www.jarticles.com/package/package_eng.html 更多信息: http : //www.jarticles.com/package/package_eng.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM