[英]How can I know if a BufferedReader is chained with a DataInputStream?
In a method I need a BufferedReader wrapping a DataInputStream as a parameter. 在一个方法中,我需要一个包装DataInputStream作为参数的BufferedReader。 I want to declare the method as this : 我想声明方法如下:
public void firstPass(BufferedReader inStream){ // some code ... }
But I don't know how I can check whether inStream is wrapping a DataInputStream. 但我不知道如何检查inStream是否包装DataInputStream。
I've tried 我试过了
public static void firstPass(BufferedReader inStream){
if (inStream instanceof DataInputStream){
}
}
but the code can't compile (Eclispe does not accept the code : "Incompatible conditional operand types BufferedReader and DataInputStream"). 但代码无法编译(Eclispe不接受代码:“不兼容的条件操作数类型BufferedReader和DataInputStream”)。
Why this need ? 为什么需要这个? Because I want to use with the same variable inStream : 因为我想在inStream中使用相同的变量:
So I need a stream which chains both classes. 所以我需要一个链接两个类的流。
I'm programming with Java 7 JDK. 我正在使用Java 7 JDK编程。
Could someone help me please ? 有人可以帮帮我吗? Thanks in advance. 提前致谢。
A DataInputStream can never be a instance of BufferedReader - they are both in separate class hierarchies. DataInputStream永远不能是BufferedReader的实例 - 它们都在不同的类层次结构中。
A BufferedReader wraps another Reader, not a Stream. BufferedReader包装另一个Reader,而不是Stream。
You can bridge from Streams to Readers using an InputStreamReader . 您可以使用InputStreamReader从Streams桥接到Readers。
Reading doubles and lines from the same Reader doesn't really makes sense - one is raw binary data, the other is character data. 从同一个Reader中读取双精度数和行数并没有多大意义 - 一个是原始二进制数据,另一个是字符数据。 Maybe you need to read a textual encoding of the doubles, and parse this using Double.parseDouble(text)
. 也许你需要阅读双打的文本编码,并使用Double.parseDouble(text)
解析它。
A. It is not possible to accomplish what you are trying to accomplish with maybe adding a flag to your method that is accepting the bufferedReader argument 答:不可能通过向接受bufferedReader参数的方法添加标志来完成您要完成的任务。
B. It's not clear how you are passing the DataInputStream to a BufferedReader (probably through InputStreamReader right?) But in any case it seems wrong that you are using BufferedReader and DataInputStream together. B.目前还不清楚你是如何将DataInputStream传递给BufferedReader的(可能是通过InputStreamReader对吗?)但是无论如何你一起使用BufferedReader和DataInputStream似乎是错误的。 BufferedReader is going to use your DataInputStream like a plain InputStream, so I don't know why you would have put the DataInputStream around it BufferedReader将像普通的InputStream一样使用你的DataInputStream,所以我不知道你为什么要把DataInputStream放在它周围
DataInputStream class isn't subtype of class BufferedReader DataInputStream类不是BufferedReader类的子类型
class DataInputStream extends FilterInputStream implements DataInput {}
class BufferedReader extends Reader {}
More info: 更多信息:
BufferedReader
不是DataInputStream
的子类,因此您的错误是合乎逻辑的......
BufferedReaders are for text data and DataInputStream is for binary data. BufferedReaders用于文本数据,DataInputStream用于二进制数据。 You would never wrap one with the other unless you like confusing people. 除非你喜欢混淆别人,否则你永远不会用另一个包裹一个。
You can use a DataInputStream to wrap a BufferedInputStream (It wouldn't make any sense to wrap it the other way) 您可以使用DataInputStream来包装BufferedInputStream(以其他方式包装它没有任何意义)
You can wrap a BufferedReader around an InputStreamReader, but there is no simple way to determine this has been done. 您可以围绕InputStreamReader包装BufferedReader,但没有简单的方法来确定已完成此操作。
You could write 你可以写
BufferedReader br = new BufferedReader(new FileReader(name));
But you would have to use the reflections library to determine what the BufferedReader wraps. 但您必须使用反射库来确定BufferedReader包装的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.