'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!



Solution 1:[1]

  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?

Solution 2:[2]

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 (e.g. java MyProg "c:\testdir\mydir") pass in a directory path relative to your current working directory (e.g. 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.

Solution 3:[3]

  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

is directory

Solution 4:[4]

  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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 General Grievance
Solution 2 sasuke
Solution 3 Upul Bandara
Solution 4 fastcodejava