Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting inputStream to FileInputStream?

I am in a problem - i need to read SecretKey from Android APK (e.g. key.keystore), for my app it has to be read as a FileInputStream, but from assets I am getting only inputStream. How to convert inputStream to FileInputStream? Or is there any other way, how to acces file from e.g. resources as a fileInputStream?

Thanks

like image 437
Waypoint Avatar asked Apr 24 '11 17:04

Waypoint


People also ask

What is the difference between FileInputStream and InputStream?

There is no real difference. FileInputStream extends InputStream , and so you can assign an InputStream object to be a FileInputStream object. In the end, it's the same object, so the same operations will happen. This behavior is called Polymorphism and is very important in Object-Oriented Programming.

How do I change InputStream to String?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.


3 Answers

Why do you need a FileInputStream specifically? In general, you don't have to care about the underlying implementation of the InputStream, you just read from it. Maybe your implementation should be InputStream agnostic.

like image 94
dmon Avatar answered Sep 22 '22 07:09

dmon


I think you are referring to AssetManager.open() that returns an InputStream. There is no need to "convert" it to a FileInputStream, just get the reference to the InputStream and use it (wrap it in a BufferedInputStream if you want).

like image 31
aromero Avatar answered Sep 22 '22 07:09

aromero


I don't think this is possible because a FileInputStream is simply a InputStream for Files. It does the job of getting an InputStream from the defined file for you, after that you're working with a normal InputStream.

Android already did this Job for you. So why do you need a FileInputStream?

like image 38
Lukas Knuth Avatar answered Sep 25 '22 07:09

Lukas Knuth