118

I just want to convert a FileInputStream to an InputStream, how can I do that?

e.g

FileInputStream fis = new FileInputStream("c://filename");
InputStream is = ?; 
fis.close();
Line
  • 1,529
  • 3
  • 18
  • 42
ranjan
  • 1,739
  • 5
  • 18
  • 22
  • 3
    ranjan. Can you please change the correct answer to this post. The current answer is subpar. If you change it to the one below that would be optimal. – Whitecat Oct 29 '13 at 23:51
  • It was just a silly question.. I was not having proper idea about serialization... any one can refer to @sudocode – ranjan Nov 19 '13 at 12:45
  • 4
    it maybe a silly question but many people still refer to this question because it is useful. – Whitecat Dec 03 '13 at 19:44
  • 1
    @WilliMentzel I came here searching for actual convertion from `File` class to `InputStream`. Your edit suggests that is what author is asking for (at least partly), and it's not right. – Line Aug 09 '18 at 10:30
  • @Line I merely changed the formatting I don't think that I changed the meaning. If you still object, feel free to rollback my changes. This will be reviewed by the community then. By the way: how do you know what the author meant except for what the question is now? – Willi Mentzel Aug 09 '18 at 13:34
  • @WilliMentzel I see in edit history. You change simple "file" forld to `File`, which suggest that this is about the `File` class. And it isn't, I see from example in original post that author was interested in converting `FileInputStream` (although he in fact wrote "file" there). – Line Aug 09 '18 at 13:53
  • @Line like I said: If you think the edit is wrong, you can correct it. I didn't want to interpret too much. thank you for talking with me about this matter :) – Willi Mentzel Aug 10 '18 at 20:15

5 Answers5

139
InputStream is;

try {
    is = new FileInputStream("c://filename");

    is.close(); 
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return is;
sunkuet02
  • 2,376
  • 1
  • 26
  • 33
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
56
InputStream is = new FileInputStream("c://filename");
return is;
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
38

FileInputStream is an inputStream.

FileInputStream fis = new FileInputStream("c://filename");
InputStream is = fis;
fis.close();  
return is;

Of course, this will not do what you want it to do; the stream you return has already been closed. Just return the FileInputStream and be done with it. The calling code should close it.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
7

You would typically first read from the input stream and then close it. You can wrap the FileInputStream in another InputStream (or Reader). It will be automatically closed when you close the wrapping stream/reader.

If this is a method returning an InputStream to the caller, then it is the caller's responsibility to close the stream when finished with it. If you close it in your method, the caller will not be able to use it.

To answer some of your comments...

To send the contents InputStream to a remote consumer, you would write the content of the InputStream to an OutputStream, and then close both streams.

The remote consumer does not know anything about the stream objects you have created. He just receives the content, in an InputStream which he will create, read from and close.

ewan.chalmers
  • 16,145
  • 43
  • 60
  • what if i want to send an inputstream over mail. .. is it possible to close on the other side.. or it is not needed to be closed – ranjan Jun 19 '12 at 12:26
  • 2
    The stream object itself is not serializable. The content of the stream can be serialized - by writing content to an OutputStream – ewan.chalmers Jun 19 '12 at 12:27
  • Actually my objective is to send a file.. I don't want anything running once I send.. e.g. as if I am sending a string.. so that the other side need not worry about closing anything. – ranjan Jun 19 '12 at 12:31
  • 1
    File is not serializable either. But the content of a file can be serialized using an OutputStream. – ewan.chalmers Jun 19 '12 at 12:34
  • is it possible i will wait for some specific amount of time and then close the inputstream,e.g depending upon size of file – ranjan Jun 19 '12 at 12:47
  • but no once i return the stream I exit the scope – ranjan Jun 19 '12 at 12:50
2

If you wrap one stream into another, you don't close intermediate streams, and very important: You don't close them before finishing using the outer streams. Because you would close the outer stream too.

user unknown
  • 35,537
  • 11
  • 75
  • 121