views:

73

answers:

3

I can not get the difference between these sentences! would you please write some snippet code for these sentences?thanks

  • The program will receive a path to a directory as the first command-line argument.
  • The program will receive a path to a file as the second command-line argument.
+1  A: 

It's as simple as that:

public static void main(String[] args)
{
    // args[0] is the directory path
    // args[1] is the file path
}

So what don't you understand?

AndiDog
what are difference between their codes? e.g. File file = new File(args[0]) this is for directory path,how about the file path?
Johanna
The `File` class can represent path names of any type of file (e.g. normal files or directories). The difference is, for a directory, `File(...).list()` will give you a list of contained files. See the documentation at http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
AndiDog
thanks,I get the whole!
Johanna
A: 

It means that the program will be run like this:

java some.package.YourProgram /some/directory /some/file/name
Pointy
+1  A: 

Imagine a command line copy program that you use like that:

copy <destination-dir> <source-file>

A simple implementation in Java would be (provided as a fragment):

package com.example;
import java.io.File;
public class Copy {

  public static void main(String[] args) {

    if (args.length != 2) {
      exitWithErrorCode(); // to be implemented
    }

    File destinationDir = new File(args[0]);
    File sourceFile = new File(args[1]);

    copyFileToDir(sourceFile, destinationDir); 
  }

  private static void copyFileToDir(File sourceFile, File destDir) {
    // to be implemented
  }
}

and you would call it like

java com.example.Copy /tmp /home/me/example.txt
Andreas_D
what does copyFileToDir class would it be?
Johanna
It's a method - changed the code example to make it clearer.
Andreas_D