如何为每个InputStream使用不同类型的InputStream来读取同一InputStream上的不同数据组? [英] How can I read different groups of data on the same InputStream, using different types of InputStreams for each of them?

查看:145
本文介绍了如何为每个InputStream使用不同类型的InputStream来读取同一InputStream上的不同数据组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以各种方式在Java中保存一些数据,分别保存为FileStringSystem.out ...,最后我得到了3种方法,它们几乎做同样的事情.因此,我将它们更改为使用OutputStream作为参数的单个方法.我向单个OutputStream写了一些东西,例如一些文本,一个序列化的对象,另一个序列化的对象,一些数字数据...

I needed to save some data in Java in various ways, to a File, to a String, to System.out... And I ended up with 3 methods doing pretty much the same thing. So I changed them into a single method with an OutputStream as a parameter. I wrote a few things to a single OutputStream, e.g. some text, a serialized object, another serialized object, some numerical data ...

但是现在我被卡住了.我忽略了一个事实,即我无法区分已写的不同内容.我为数据创建一个InputStream.我在该流上使用Scanner首先读取文本,然后尝试使用ObjectInputStream读取序列化的对象,但得到EOFException.

But now I'm stuck. I overlooked the fact that I cannot distinguish between the different things that have been written. I create an InputStream for the data. I use a Scanner on that stream to read the text first, and then I tried using an ObjectInputStream to read the serialized objects, but I get an EOFException.

我想扫描仪会先读.如何防止扫描仪提前阅读.
或者更确切地说,我该如何使用针对每个数据组的适当InputStream来读取每个数据组.

I guess that the Scanner reads ahead. How can I prevent the scanner to read ahead.
Or rather, how can I read each group of data using an appropriate InputStream for each of them.

推荐答案

您真的不想尝试使用不同的阅读器从同一流中进行阅读.即使您设法使其在您的计算机上运行,​​当您在其他OS或不同的JVM实现上运行它时,它可能也会损坏.

You really don't want to try using different readers to read from the same stream. Even if you manage to get it working on your machine, it might break when you run it on a different OS or with a different JVM implementation.

您应该选择一种读取和写入数据的方法.由于您在流中使用序列化对象,因此最好将其用于所有内容.您已经在评论中指出,很难通过字符串读取二进制数据并正确解释它.但是,获取String对象,将其写到输出流中,读回并转换为String并不难.

You should choose a single method of reading and writing data. Since you're using serialized objects in the stream you're probably best off using that for everything. You already pointed out in your comments that it would be very difficult to read binary data in through a string and interpret it correctly. However, it's not hard to take a String object, write it out on the output stream, read it back in and cast it as a String.

现在存在解释您的数据的问题.我建议将所有内容写成标签数据对.您首先写出一个Integer(也许是枚举的序数,以使它们在程序中更易于使用),然后写出数据.整数表示流中接下来要传输的数据类型(例如,文本或对象),然后您读入的下一个对象是数据,您知道它是什么类型.如果是Text,则可以将对象转换为String,然后将其传递到Scanner,如果是对象,则只需对对象执行所有操作即可.

Now there's the problem of interpreting your data. I suggest writing everything out in tag-data pairs. You write out an Integer first (maybe the ordinal of an enum to make them easier to use in your program), then you write out your data. The integer represents the type of data that's coming next in the stream (e.g. either Text or Object), and then the next object you read in is the data and you know what type it is. If it is Text you can cast the object to a String, and pass it into a Scanner, and if it's an object then you just do whatever you need to do with the object.

为了使事情更简洁,您可以使用每种数据类型的方法在流周围构建包装器.也许您可以使用getNextObject()方法和getNextTextScanner()方法.每个人都将首先检查流中的下一个Integer标记,以确保它正在读取正确的数据(如果发现不匹配,则抛出异常),然后将返回下一个Object或返回新的Scanner来处理数据字符串.

To make things a bit cleaner you could build a wrapper around the stream with a method for each data type. Maybe you could have a getNextObject() method and a getNextTextScanner() method. Each would first check the next Integer tag in the stream to make sure it's reading the right data (throwing an exception if it finds a mismatch), and then would either return the next Object or return a new Scanner for processing a String of data.

真的,如果可以对两种不同类型的数据使用单独的流,那就更好了.但是,如果您真的在使用相同的流,那么我就是这样做的.

Really, it would be better if you could use separate streams for the two different types of data. But, if you're really stuck using the same stream then that's how I'd do it.

这篇关于如何为每个InputStream使用不同类型的InputStream来读取同一InputStream上的不同数据组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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