Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

file-io

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!

like image 582
Crystal Avatar asked Aug 02 '10 04:08

Crystal


People also ask

How to get the parent of an abstract pathname in Java?

The parent of an abstract pathname may be obtained by invoking the getParent () method of this class. First of all, we should create the File class object by passing the filename or directory name to it. A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing.

How to create a file object in Java?

A File object is created by passing in a String that represents the name of a file, or a String or another File object. For example, Attention reader! Don’t stop learning now.

How to write data into a file using fileoutputstream in Java?

To write data into a file using FileOutputStream class is shown in the following example. It also requires creating the object of the class with the filename to write data into a file. Here, the string content is converted into the byte array that is written into the file by using the write () method.

How to pass command-line arguments in Java?

Command-line arguments in Java are used to pass arguments to the main program. If you look at the Java main method syntax, it accepts String array as an argument. When we pass command-line arguments, they are treated as strings and passed to the main function in the string array argument. The arguments have to be passed as space-separated values.


1 Answers

  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?

like image 112
user85509 Avatar answered Sep 23 '22 09:09

user85509