0

i can't pass the name of files as argument to the main method. here is my code...

import java.io.*;

class four {
public static void main(String args[])throws IOException{

    int i;
    FileInputStream fin = null;

    try{
        fin = new FileInputStream(args[0]);
    }catch(FileNotFoundException e){
        System.out.println("File not found........");
    }catch(IndexOutOfBoundsException e){
        System.out.println("Index out of bound........");
    }

    do{
        i = fin.read();
        if(i != -1)
            System.out.println(i);
    }while(i!=-1);

    fin.close();
}

}

1
  • 1
    And what is the exception? What have you written at the command line? Commented Dec 9, 2011 at 13:01

1 Answer 1

2

You can pass them in; they just aren't doing what you think they are.

Echo out the command line arguments and you'll see your problem right away:

for (String arg : args) {
    System.out.println(arg);
}

I'm guessing that you either have unescaped Windows slashes ('\') or spaces in your file paths.

Escape the slashes and enclose each file path in double quotes and you'll have better luck.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.