How can a FileInputStream get the content of File?

user1459961 picture user1459961 · Jul 14, 2012 · Viewed 31.6k times · Source

I have a file f and I need to affect it into a FileInputStream fs :

File f = new File("C:/dir/foo.txt");
FileInputStream fs = (FileInputStream)f;

But i get this error :

Cannot cast from File to FileInputStream

How can fs get the content of f?

Answer

A.H. picture A.H. · Jul 14, 2012

The trick is this:

FileInputStream fs = new FileInputStream(f);