Noob issue, transfer to file name / directory on command line in Java

I am trying to do some processing whether the user will enter the file name (1) or (2) the directory name on the command line. Everything else should throw an error. Starting with the simplest case, I wrote the following:

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 prompt, I type: java RemoveDuplicates example.txt and I get replays: "Shouldn't be." I also tried java RemoveDuplicates "example.txt" and this also does not work. So I was wondering if my code was wrong or how I pass it to the command line for starters.

Secondly, how do you pass the directory name? For example, if your directory was myDirectory, this is the same thing: java RemoveDuplicates myDirectory

Thirdly, why, if I put my file f = new file (args [0]) in a try block and have a catch block, I get a compilation error saying that in my block try never throws an exception. I thought the file threw an exception? Thanks in advance!

+6
java file-io
source share
4 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 this right.

3) Actually, I am not getting this error. How did you try / catch?

+3
source share

So I was wondering if my code is wrong or how I pass it wrong on the command line for starters

Is this file in your working directory? What does not exist is neither a file nor a directory. Try creating a sample text file with the same name, and then call your program.

how do you pass the directory name

Passing to a completely appropriate line that represents your directory (for example, java MyProg "c:\testdir\mydir" ) goes along the path to the directory relative to your current working directory (for example, java MyProg testdir/mydir )

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

Creating a new File object does not create a physical file, so there is no IOException as you expected. Only when you try to open a file for reading or writing, a FileNotFoundException is FileNotFoundException . Therefore, it is good practice to always check for a file before using it for I / O.

+2
source share
  • I compiled and ran your program. It works as expected.

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

is a file

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

is a directory

0
source share
  • You need to enter java RemoveDuplicates. java example.txt.
  • Correctly.
  • You should do this, since anything can throw a RuntimeException, which is a subclass of Exception.
0
source share

All Articles