无论markSupported()如何,都要使InputStream读取多次 [英] Getting an InputStream to read more than once, regardless of markSupported()

查看:102
本文介绍了无论markSupported()如何,都要使InputStream读取多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够多次重复使用 java.io.InputStream ,并且我认为以下代码可以正常工作,但它只能在第一次使用。

I need to be able to re-use a java.io.InputStream multiple times, and I figured the following code would work, but it only works the first time.

public class Clazz
{
  private java.io.InputStream dbInputStream, firstDBInputStream;
  private ArrayTable db;

  public Clazz(java.io.InputStream defDB)
  {
    this.firstDBInputStream = defDB;
    this.dbInputStream = defDB;
    if (db == null)
      throw new java.io.FileNotFoundException("Could not find the database at " + db);
    if (dbInputStream.markSupported())
      dbInputStream.mark(Integer.MAX_VALUE);
    loadDatabaseToArrayTable();
  }

  public final void loadDatabaseToArrayTable() throws java.io.IOException
  {
    this.dbInputStream = firstDBInputStream;
    if (dbInputStream.markSupported())
      dbInputStream.reset();

    java.util.Scanner fileScanner = new java.util.Scanner(dbInputStream);
    String CSV = "";
    for (int i = 0; fileScanner.hasNextLine(); i++)
      CSV += fileScanner.nextLine() + "\n";
    db = ArrayTable.createArrayTableFromCSV(CSV);
  }

  public void reloadDatabase()//A method called by the UI
  {
    try
    {
      loadDatabaseToArrayTable();
    }
    catch (Throwable t)
    {
      //Alert the user that an error has occurred
    }
  }
}




请注意,ArrayTable是我的一类,它使用数组来提供接口用于处理表格。

Note that ArrayTable is a class of mine, which uses arrays to give an interface for working with tables.



问题






在这个程序中,在调用 reloadDatabase()方法之后,数据库会立即直接显示给用户,因此任何解决方案都会将初始读取保存到内存中的对象是无用的,因为它不会刷新数据(想象它就像浏览器;当你按刷新时,你希望它再次获取信息,而不仅仅是显示它第一次获取的信息)。 如何多次读取 java.io.InputStream

Question


In this program, the database is shown directly to the user immediately after the reloadDatabase() method is called, and so any solution involving saving the initial read to an object in memory is useless, as that will NOT refresh the data (think of it like a browser; when you press "Refresh", you want it to fetch the information again, not just display the information it fetched the first time). How can I read a java.io.InputStream more than once?

推荐答案

您不一定能多次读取InputStream。有些实现支持它,有些则不支持。你正在做的是检查markSupported方法,如果你可以读两次相同的流,这确实是一个指标,但是你忽略了结果。您必须调用该方法以查看是否可以读取流两次,如果不能,则进行其他安排。

You can't necessarily read an InputStream more than once. Some implementations support it, some don't. What you are doing is checking the markSupported method, which is indeed an indicator if you can read the same stream twice, but then you are ignoring the result. You have to call that method to see if you can read the stream twice, and if you can't, make other arrangements.

编辑(响应评论):当我写下我的答案时,我的其他安排是获得一个新的InputStream。但是,当我在你的评论中读到关于你想做什么的问题时,我不确定它是否可行。对于操作的基础知识,您可能需要RandomAccessFile(至少这是我的第一个猜测,如果它有效,那将是最简单的) - 但是您将遇到文件访问问题。你有一个应用程序主动写入文件,另一个读取该文件,你将遇到问题 - 究竟哪些问题将取决于操作系统,所以无论什么解决方案需要更多的测试。我建议在SO上提出一个单独的问题,那个尝试过这个问题的人可能会给你更多的见解。

Edit (in response to comment): When I wrote my answer, my "other arrangements" was to get a fresh InputStream. However, when I read in your comments to your question about what you want to do, I'm not sure it is possible. For the basics of the operation, you probably want RandomAccessFile (at least that would be my first guess, and if it worked, that would be the easiest) - however you will have file access issues. You have an application actively writing to a file, and another reading that file, you will have problems - exactly which problems will depend on the OS, so whatever solution would require more testing. I suggest a separate question on SO that hits on that point, and someone who has tried that out can perhaps give you more insight.

这篇关于无论markSupported()如何,都要使InputStream读取多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆