-4

I use the function of "DataInputStream" called "readLong()" to read value from a file end with ".txt". This file was edited in advance. I want to get the correct value "123456789", but the answer is "3544952156018063160"

DataInputStream di = new DataInputStream(new FileInputStream("E:\\CMHI\\LocalHost\\src\\t.txt"));
Long res = di.readLong();
System.out.println(res);
di.close();

1 Answers1

2

From the JavaDoc for DataInputStream

Returns:

the next eight bytes of this input stream, interpreted as a long.

That's not what you want, because this is a text file, not a binary. I would recommend using a Scanner to read this file. It has a nextLong method which does exactly what you're looking for.

Scanner sc = new Scanner(new File("whatever.txt"));
System.out.println(sc.nextLong());
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110