> How to read any kind of file (i.e. text, .ENC, .CERT) in java?

How to read any kind of file (i.e. text, .ENC, .CERT) in java?

Posted at: 2014-12-18 
I need java code for reading file ( File which is stored in my computer). I write code in Neatbeans. I was looking for help by googling, but it derives so many confusing answers.

You can read any file using a FileInputStream (provided you have read access to the file. That gets you every byte in the file, in order.

FileInputStream f = new FileInputStream("path/filename.ext");

byte b = f.read(); // reads and returns one byte (0-255) or -1 at end of file

byte[] buffer = new byte[16386]; // pick any buffer size you like

int n = f.read(buffer); // read as much as possible in to buffer

// returns number of bytes read, or -1 if EOF before any data read.

That's easy, and it works for any file.

The hard part is always making sense of the bytes. There's no general rule for that. Every file format takes a program designed for that file format, and not all file formats have published specifications.

I need java code for reading file ( File which is stored in my computer). I write code in Neatbeans. I was looking for help by googling, but it derives so many confusing answers.