1) Are you sure example. Txt exists in your working directory? Try adding the following and see what happens.
Up vote 6 down vote favorite share g+ share fb share tw.
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(args0); 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(args0) 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! Java file-io link|improve this question asked Aug 2 '10 at 4:36Crystal2,364932 81% accept rate.
1) Are you sure example. Txt exists in your working directory? Try adding the following and see what happens.
File f = new File(args0); 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 Aug 2 '10 at 4:51 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 Aug 2 '10 at 4:53 For me, those are all the same directories. – Crystal Aug 2 '10 at 4:54 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 Aug 2 '10 at 4:56.
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(args0).
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.
You need to type java RemoveDuplicates. Java example.txt. Correct.
You should be able to do that as anything can throw RuntimeException which is a subclass of Exception.
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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.