java 有没有办法创建带有标记功能的 FileInputStream?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5496172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:24:04  来源:igfitidea点击:

Is there a way to create FileInputStream with mark feature?

javainputstream

提问by Roshan

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

是否有任何可能的方法来创建FileInputStream标记支持的功能为true

采纳答案by Suraj Chandran

Wrap your Fileinputstream inside a BufferedInputStream.

将您的 Fileinputstream 包裹在BufferedInputStream 中

The buffered streams support marks.

缓冲的流支持标记。

回答by iluxa

Wrap it in BufferedInputStream.

把它包起来BufferedInputStream

instead of

代替

FileInputStream fis = new FileInputStream(...);

do this:

做这个:

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

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

并使用bis代替fis;您的代码中不应有任何其他更改。

回答by benleis

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.

BufferedInputStreams 并不神奇。它们将仅支持与其底层缓冲区一样大的标记,并且这些缓冲区将占用内存。因此,如果您沿着这条路线走下去,重要的是您了解用例并可能使用适当大小的缓冲区调用 BufferedInputStream 构造函数。如果底层文件开始变大并且您标记回足够远,那么这种技术可能对您不起作用。

回答by Franky

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");
        }
        }