0

I'm having some trouble reading in a file from the command line. I've never used command line arguments before so I guess I'm a little lost. Here's what I'm trying so far:

FileInputStream fin1 = null;
for (int i = 0; i < args.length; i++) //command line argument for file input
{
    fin1 = new FileInputStream(args[i]);
}
//Scanner scan = new Scanner(fiin1);

I've commented out my scanner because I'm using a different method (into which I'm passing in fin1 as a parameter) and that method has a scanner in it. However, I'm not too sure if I still need the scanner there (maybe to pass into the other method as a param).

Anyway, if I run my code, I get a NullPointerException, which I assume comes from the fact that I initialized my FileInputStream as null. But if I'm changing it in the for loop, why does that matter? Also, I need to keep my main method the way it is so I can do more in it.

Can anyone help?

1
  • You're assigning a FileInputStream created with the last argument passed to your program. It seems to me like you might not be passing any parameter at all, therefore the statement within the for loop is never executed. Please update the question with the arguments that you are passing to the program. Nov 22, 2015 at 2:27

3 Answers 3

0

Notice that it is called FileInputStream , and so we need to be using a File .

You can simply use a Scanner , and set it to System.in :

            Scanner scanner = new Scanner(System.in);

And then afterwards, you can initialize that FileInputStream

How to Read Strings from Scanner in console Application JAVA?

0

Use following code.

    if (args.length < 1) {
        System.out.println("No file was given as an argument..!");
        System.exit(1);
    }
    String fileName = args[0];
    Scanner scanner = new Scanner(new File(fileName));

if you want to use a FileInputStream then change the last line to create a FileInputStream instance.

   fin1 = new FileInputStream(fileName);

No need to use a for-loop if you are giving only one filename as the argument. You can run your code as follow.

javac MyClass.java       //Compile your code(Assumed that your file is MyClass.java
java MyClass filename    //Change filename with the path to your file

You are getting NullPointerException probably because you are not using filename as a argument when you run your java code.

0

First of all : when you run your code, you'll reach only the last argument. You should do like this:

 FileInputStream fileInputStream = null;
    for (String argument : args) {
        fileInputStream = new FileInputStream(argument);
        //you should process your argument in block together with creating fis
        Scanner scanner = new Scanner(fileInputStream);
        //now, when a scanner copy is created, you can use it (or you can use your own
        while (scanner.hasNext()) {
            System.out.println(scanner.nextLine());
        }
    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.