简体   繁体   中英

Noob question, passing in a file name / directory into command line in Java

I'm trying to do some processing on whether the user enters a (1) file name, or (2) directory name into the command line. Anything else should throw an error. Starting with the simplest case, I wrote this:

import java.io.*;
import java.util.*;

public class RemoveDuplicates {

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Program accepts one command-line argument.  Exiting!");
            System.exit(1);
        }
        File f = new File(args[0]);
        if (f.isDirectory()) {
            System.out.println("is directory");
        }
        else if (f.isFile()) {
            System.out.println("is file");
        }
        else {
            System.out.println("Shouldn't happen");
        }
    }
}

at the command line, I type: java RemoveDuplicates example.txt and I get the reuslts, "Shouldn't happen." I also tried java RemoveDuplicates "example.txt" and that doesn't work either. So I was wondering if my code is wrong, or how I'm passing it into the command line is wrong for starters.

Secondly, how do you pass in a directory name? Like if your directory was myDirectory, is it the same thing: java RemoveDuplicates myDirectory

Third, why if I put my File f = new File(args[0]) into a try block and have a catch block, I get a compile error about what is in my try block never throws an exception. I thought File threw an exception? Thanks in advance!

1) Are you sure example.txt exists in your working directory? Try adding the following and see what happens.

File f = new File(args[0]);
if (!f.exists()) {
  System.out.println("does not exist");
}

2) You have that right.

3) Actually, I don't get this error. How did you do the try/catch?

So I was wondering if my code is wrong, or how I'm passing it into the command line is wrong for starters

Is that file present in your working directory? Something which doesn't exist is neither a file nor a directory. Try creating a sample text file with the same name and then invoking your program.

how do you pass in a directory name

Pass in a fully qualified string which represents your directory (eg java MyProg "c:\\testdir\\mydir" ) pass in a directory path relative to your current working directory (eg java MyProg testdir/mydir )

File f = new File(args[0]). I thought File threw an exception?

Creating a new File object doesn't create a physical file hence no IOException is thrown as you expected. It is only when you try to open a file for reading or writing a FileNotFoundException is thrown. Hence it's a good practice to always check for the existence of a File before using it for IO.

  1. I compiled & ran your program. It is working as expected.

C:\\upul\\eclipse\\src\\Test\\src>java RemoveDuplicates RemoveDuplicates.java

is file

C:\\upul\\eclipse\\src\\Test\\src>java RemoveDuplicates c:\\upul\u003c/i>

is directory

  1. You need to type java RemoveDuplicates. java example.txt.
  2. Correct.
  3. You should be able to do that as anything can throw RuntimeException which is a subclass of Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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