NiallMitch14
NiallMitch14

Reputation: 1229

Reading an integer from a file using FileInputStream

I'm trying to read an integer I have saved to a file in my android internal storage. Using FileInputStream's read method, it only returns an int as a byte. I'm wondering if there is a way to read the integer I have saved to the file?

Thanks

Upvotes: 2

Views: 7168

Answers (2)

Manaswi B
Manaswi B

Reputation: 48

Try using readInt() method. This method gives you int value

Working example http://www.java-examples.com/read-int-file-using-datainputstream

Upvotes: 2

Malt
Malt

Reputation: 30335

The FileInputStream allows you to read the file byte by byte. The integer you saved is four bytes long. You can read it by either using the FileInputStream to read the four bytes yourself and use them to construct your int, or use a higher level utility, such as Scanner:

public static int readInt(FileInputStream in) {
    Scanner scanner = new Scanner(in);
    return scanner.nextInt();
}

Upvotes: 2

Related Questions