0

I have learned Java for about a month and am currently learning I/O of Java but I have encountered some problems. Below is a simply toy code to practice with the Inputstream.

import java.io.*;

public class IOTest{
   public static void main(String[] args) throws IOException{
   InputStream in;
   in = new FileInputStream(args[0]);
   int total = 0;
   while (in.read() != -1)
   total++;
   System.out.println(total + " bytes");
   }
}

The above code compiles Okay. The purpose of this code is to simply calculate how many bytes in the arguments. However, when I run the compiled code with arguments, for example:

java IOTest firstTrial 

The system gives the following Exception message:

Exception in thread "main" java.io.FileNotFoundException: firstTrial <The system 
cannot find the file specified>
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init><Unknown Source>
       at java.io.FileInputStream.<init><Unknown Source>
       at IOTest.main<IOTest.java:8>

Please help point out how was the exception thrown?

One extra question is I am using Eclipse for java programming. What is the End-of-Input character in Eclipse for Java? Thanks

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Optimus Prime
  • 409
  • 7
  • 15
  • Ctrl+D for EOF/end of input – Mike Jan 27 '14 at 20:40
  • 1
    where firstTrial file exist ? – Kick Jan 27 '14 at 20:42
  • Sorry, maybe I didn't make myself understood. I was trying to read the arguments during running of the code and calculate the number of bytes in the arguments. In the example above, firtTrial is the argument I input during running the IOTest code. – Optimus Prime Jan 27 '14 at 20:49

2 Answers2

0

You are not reading a file:

java.io.FileNotFoundException: firstTrial <The system 
cannot find the file specified>

Put some full filepath as argument and your program will cont the bytes.

Thrash Bean
  • 658
  • 4
  • 7
0

It seems as if you want to read the argument string itself as an InputStream, but the way FileInputStream works is that the String you pass it is not the data to read, but the name of a file to open and read.

However, you can read the string itself as data. You can do that with a StringReader, if you want to use the Reader API in Java, or if you want to read the raw bytes as an InputStream you can do that, too. (But you need to specify the character encoding. In this case, I am specifying it as "UTF-8".)

import java.io.*;

public class IOTest {
    public static void main(String[] args) throws IOException {
        byte[] bytes = args[0].getBytes("UTF-8");
        InputStream in = new ByteArrayInputStream(bytes);
        int total = 0;
        while (in.read() != -1) {
            total++;
        }
        System.out.println(total + " bytes");
    }
}

Notice that I obtain the bytes from the string, and then use a ByteArrayInputStream instead of a FileInputStream to read them. I made one other change, which was to put braces around the while loop body. I prefer to either have loops on one line, or, better yet, to put braces around the body to make the extent of the loop clearer (and possibly avoid errors).

David Conrad
  • 15,432
  • 2
  • 42
  • 54