文件输出流到文件输入流

发布于 2024-12-07 23:44:51 字数 65 浏览 0 评论 0原文

将 FileOutputStream 转换为 FileInputStream 的最简单方法是什么(一段代码就很好)?

What is the simplest way to convert a FileOutputStream into FileInputStream (a piece of code would be great)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初见终念 2024-12-14 23:44:51

这可能对您有帮助:

http://ostermiller.org/convert_java_outputstream_inputstream.html

本文提到了 3 种可能性

  • :将完整输出放入字节数组中,然后再次读取它
  • 使用管道
  • 使用循环字节缓冲区(该页面上托管的库的一部分)

仅供参考,以相反的方式进行(输入到输出):

使用 Apache Commons IO 的一个简单解决方案是:

IOUtils.copyLarge(InputStream, OutputStream)

或者如果您只想复制文件:

FileUtils.copyFile(inFile,outFile);

如果您不想使用 Apache Commons IO,这是 copyLarge 方法的作用:

public static long copyLarge(InputStream input, OutputStream output) throws IOException 
{
  byte[] buffer = new byte[4096];
  long count = 0L;
  int n = 0;
  while (-1 != (n = input.read(buffer))) {
   output.write(buffer, 0, n);
   count += n;
  }
  return count;
}

This might help you:

http://ostermiller.org/convert_java_outputstream_inputstream.html

This article mentions 3 possibilities:

  • write the complete output into a byte array then read it again
  • use pipes
  • use a circular byte buffer (part of a library hosted on that page)

Just for reference, doing it the other way round (input to output):

A simple solution with Apache Commons IO would be:

IOUtils.copyLarge(InputStream, OutputStream)

or if you just want to copy a file:

FileUtils.copyFile(inFile,outFile);

If you don't want to use Apache Commons IO, here's what the copyLarge method does:

public static long copyLarge(InputStream input, OutputStream output) throws IOException 
{
  byte[] buffer = new byte[4096];
  long count = 0L;
  int n = 0;
  while (-1 != (n = input.read(buffer))) {
   output.write(buffer, 0, n);
   count += n;
  }
  return count;
}
莳間冲淡了誓言ζ 2024-12-14 23:44:51

我收到一个 FileOutputStream。我想要的是阅读它。

您当然无法从 OutputStream 中读取。我认为您的意思是您想从 FileOutputStream 写入的文件中读取内容。我认为你也做不到。 FileOutputStream 似乎没有保留对正在写入的文件的引用。

您需要做的是发现什么File或路径(String)被传递到FileOutputStream并使用相同的File< /code> 或 String 创建新的 FileInputStream

I receive an FileOutputStream. What I want is read it.

You certainly can't read from an OutputStream. I think you mean you want to read from the file being written to by the FileOutputStream. I don't think you can do that either. The FileOutputStream doesn't seem to keep a reference to the file being written to.

What you need to do is discover what File or path (a String) was passed into the FileOutputStream and use that same File or String to create a new FileInputStream.

夏九 2024-12-14 23:44:51

如果您希望能够在写入后读取内容(而不刷新到磁盘),请使用 ByteArrayOutputStream 和 ByteArrayInputStream。完成对 ByteArrayOutputStream 的写入后,您可以获取底层字节数据并使用 ByteArrayInputStream 将其读回。

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// do any output onto outStream you need
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
// you can read back the output with inStream now

如果你想要一个缓冲区(生产者/消费者类型问题),请查看java的nio包提供的缓冲区。

If you mean that you want to be able to read the contents after writing (without flushing to disk), use a ByteArrayOutputStream and a ByteArrayInputStream. Once you're done writing to the ByteArrayOutputStream, you can get the underlying byte data and use a ByteArrayInputStream to read it back.

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// do any output onto outStream you need
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
// you can read back the output with inStream now

If you want a buffer (producer/consumer type problem), look into the Buffers provided with java's nio package.

层林尽染 2024-12-14 23:44:51

如果您可以使用@Thomas给出的示例,否则您可以这样做:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class FileInputOutputExample {
  public static void main(String[] args) throws Exception {

    InputStream is = new FileInputStream("in.txt");
    OutputStream os = new FileOutputStream("out.txt");
    int c;
    while ((c = is.read()) != -1) {
      System.out.print((char) c);
      os.write(c);
    }
    is.close();
    os.close();

  }
}

来自java2s.com的示例

编辑:

由于@Thomas删除了他的答案,我将仅添加用于处理此问题的参考Commons IO方法:

IOUtils.copy(InputStream is, OutputStream os)

If you can use @Thomas example given, otherwise you can do:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class FileInputOutputExample {
  public static void main(String[] args) throws Exception {

    InputStream is = new FileInputStream("in.txt");
    OutputStream os = new FileOutputStream("out.txt");
    int c;
    while ((c = is.read()) != -1) {
      System.out.print((char) c);
      os.write(c);
    }
    is.close();
    os.close();

  }
}

example from java2s.com

EDIT:

As @Thomas deleted his answer, I will just add for reference Commons IO approach for handling this:

IOUtils.copy(InputStream is, OutputStream os)
疯狂的代价 2024-12-14 23:44:51

你想要的是一个 RandomAccessFile。它具有与 FileInputStream 和 FileOutputStream 相同的基本方法。我知道,你可能没有得到这个。

What you want is a RandomAccessFile. This has the same basic methods of both the FileInputStream and a FileOutputStream. I know, you probably aren't given that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文