tags:

views:

73

answers:

4

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!

+3  A: 

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?

Yes, the file name is in the same directory where my .java and .class files are, but I get the Does not exist now that I put that in. I tried writing a file instead, and it puts the file into this same directory, where my files lie. Edit: I just tried it with a directory, and that seemed to work so that is good.
Crystal
`example.txt` should be in the directory you run the program from, not necessarily the directory where the `.java` and `.class` files are.
David Zaslavsky
For me, those are all the same directories.
Crystal
Ok, I got it working now. In my directory, it says example.txt, however, the separate file I tried writing, did not have the .txt extension, so I assumed Windows wasn't showing it since it found that file. So I deleted .txt from the example.txt name. Thanks for the help!
Crystal
A: 
  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

Upul
+2  A: 

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.

sasuke
+1  A: 
  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.
fastcodejava