26

Is there any possible way to create FileInputStream with mark supported feature as true?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Roshan
  • 2,019
  • 8
  • 36
  • 56

4 Answers4

25

Wrap your Fileinputstream inside a BufferedInputStream.

The buffered streams support marks.

Infuzion
  • 641
  • 6
  • 16
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • @Sathish Marking is more like a feature, even if the feature is there, its upto the user to use it or not. But if you forcefully want to prevent marking then you could create decorator class and override mark() method to do nothing and markSupported() to return false. – Suraj Chandran Apr 01 '11 at 10:55
  • This is not always correct - It may give you "Resetting to invalid mark" it's not consistent. – ha9u63a7 Nov 17 '17 at 11:10
  • The buffer of the buffered input stream must be large enough. – kap Jun 12 '20 at 14:58
  • Another caveat - if the underlying (file) stream gets completely read during a mark-reset cycle, regardless of reset() you'll receive a "stream closed" error when trying to read the (post-reset) buffered stream – Janaka Bandara Jan 01 '22 at 07:52
16

Wrap it in BufferedInputStream.

instead of

FileInputStream fis = new FileInputStream(...);

do this:

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(...));

and use bis instead of fis; nothing else should have to change in your code.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
iluxa
  • 6,941
  • 18
  • 36
  • Russian file name is not found exception in this line new FileInputStream(...) what should i do other all file work fine but russinal char not support with file name – Bhanu Sharma Feb 10 '14 at 12:51
  • There is a good chance that you will get "Resetting to invalid mark" IOException cause. – ha9u63a7 Nov 17 '17 at 11:11
  • Thank you so much. Helped me out with the "java.io.IOException: getFileMagic() only operates on streams which support mark(int)". Excellent answer ! – Dheemanth Bhandarkar Apr 25 '18 at 06:39
13

BufferedInputStreams are not magic. They will only support marking for as large as their underlying buffers and these buffers are going to take up memory. So if you go down this route its important that you understand the usage case and potentially call the BufferedInputStream constructor with the appropriatedly sized buffer. If the underlying file starts to get large and you mark far enough back then this technique may not work for you.

benleis
  • 131
  • 1
  • 2
0

Try something like this

public FileInputStream fstream;
public DataInputStream in;
public BufferedInputStream bs;
public String path;

public void myExample() throws IOException{
    path = "yourPath";
    try {
        fstream = new FileInputStream(path);
        in = new DataInputStream(fstream);
        bs = new BufferedInputStream(new InputStreamReader(in));

        //do something

        br.close(); //when do something is completed
        } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "File not found");
        }
        }
Fseee
  • 2,476
  • 9
  • 40
  • 63