1

I wrote a Java program that reads integers from a file. Five integers were written to that file earlier using the following code:

Scanner s=new Scanner(System.in);
DataOutputStream d=null;
System.out.println("Enter 5 integers");
try{
    d=new DataOutputStream(new FileOutputStream("num.dat"));
    for(int i=1;i<=5;i++){
    d.writeInt(s.nextInt());
    } //for
} //try
catch(IOException e){
    System.out.println(e.getMessage());
    System.exit(0);
}
finally{
    try{
        d.close()
    }
    catch(Exception e){}
}//finally

Now while reading the integers from the file num.dat, I wish to skip 'n' integers. I used the following code in another class:

DataInputStream d=null;
Scanner s=new Scanner(System.in);
int n=0; //stores no. of integers to be skipped
try{
    d=new DataInputStream(new FileInputStream("num.dat");
    for (...){
        if(...)
        n++; //condition to skip integers
    } //for
}//try
catch(IOException e){
    System.out.println(e.getMessage());
    System.exit(0);
}
finally{
    try{
        d.skip(n); //skips n integers
        System.out.println("Requested Integer is "+d.readInt());
        d.close();
    }
    catch(Exception e) {}
} //finally

The program shows correct output only if I request the first integer of the file. If I try to skip some integers, it either gives no or a wrong output. The integers I entered in the first program weren't one digit but three digit integers. I also tried to skip individual digits of a three digit integer but that also didn't help. Please tell me how I can skip while reading primitive data values.

1 Answers1

0
d.skip(n); //skips n integers

This interpretation of the skip(long n) method is incorrect: it skips n bytes, not n integers:

Skips over and discards n bytes of data from the input stream.

To fix this problem, write your own method that calls d.readInt() n times, and discards the results. You could also do it without a method, simply by adding a loop:

try {
    //skips n integers
    for (int i = 0 ; i != n ; i++) {
        d.readInt();
    }
    System.out.println("Requested Integer is "+d.readInt());
    d.close();
}
catch(Exception e) {}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Yep. skip() skips bytes. But since I didn't get a syntax error, I thought there might as well be a skip() method for primitive values. I'll try what you say and let you know. Thanks :) BTW, if d.readInt() is called n times, and I want to skip to the third of 5 three-digit integers, what should be 'n' ? –  Feb 28 '16 at 11:46
  • @ProgyadeepMoulik Every single method of the Java library is fully documented. When you are not sure what a method does, look up its documentation. It may be tricky when the method is inherited, like `skip`, because it's not described on the documentation page for `DataInputStream`. Instead, there's a link at the bottom for all inherited methods. You need to follow that link to see what the method does. – Sergey Kalinichenko Feb 28 '16 at 11:51
  • Thank you for your your solution. It worked perfectly. I just called `d.readInt()` the number of times as the number of integers I wanna skip. And I also learned about `RandomAccess` files and the class' `seek()` method which helps to do this task very easily. Thanks again for your help. –  Feb 28 '16 at 17:15