数据输出流 [英] datainput stream

查看:75
本文介绍了数据输出流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧


i试图从键盘上读取一个带有DataInputStream的整数

有人可以解释下面代码的行为????


< code>

DataInputStream dis = new DataInputStream(System.in);

DataOutputStream dout = new DataOutputStream(System.out );

a = dis.readInt();

System.out.println(" using system.out a =" + a);

dout.writeInt(a);

a ++;

System.out.println(" using system.out a =" + a);

dout.writeInt(a);

dout.flush();

< / code>

$ b当输入56作为输入时,输出结果为
$ b< output>

使用system.out a = 892734730

56

使用system.out a = 892734731

56


< / output>


不合逻辑,如果readInt读取一个整数(56),变量a

应存储56&不是其他一些价值....

DataInput读取的格式是什么?

当使用DataOutput时56显示corectly也是字符

''?''它来自哪里:O?


如何使用DataInputStream在int变量中存储值或者

它不是这意味着;)?

i了解流以不同的格式存储它,System.out

不理解

但如果我们使用变量''a''在某些计算中(当然我们会

否则你为什么要读它;))然后整个计算

会出错

我缺少什么?


amey

解决方案

" Amey Samant" <是***** @ yahoo.com>在消息中写道

news:66 ************************** @ posting.google.c om ...

嗨所有

我试图从键盘读取整数
DataInputStream可以有人解释下面代码的行为吗?

< code>
DataInputStream dis = new DataInputStream(System.in);
DataOutputStream dout = new DataOutputStream(System.out);
a = dis。 readInt();
System.out.println(" using system.out a =" + a);
dout.writeInt(a);
a ++;
系统.out.println(" using system.out a =" + a);
dout.writeInt(a);
dout.flush();
< / code> <当输入56为输入时,输出结果如下
< output>
使用system.out a = 892734730
56
使用system.out a = 892734731

< / output>

如果readInt读取一个整数(56),
变量a应该存储是不合逻辑的56&没有其他一些价值....
DataInput读取的格式是什么?


它被设计为读入一个编码为二进制整数的值。来自''System.in''的

值是*不是*编码的,因此你会得到

的随机或无意义值。


来自''System.in''的数据被编码为字符。当

执行交互式控制台I / O时,您会发现输入只是由换行符终止的一系列字符。鉴于此,

执行此类I / O的常用方法如下:

BufferedReader br =

new BufferedReader(

新的InputStreamReader(System.in));


字符串输入;


while((input = br .readLine())!= null)

{

...

System.out.println(" Input Line:" +输入);

...

int i;


尝试

{

i = Integer.parseInt(输入);

System.out.println("转换整数值:" + i);

} < br $>
catch(NumberFormatException e)

{

//错误...

}

...

}

当使用DataOutput时,56也会显示出字符''''它来自哪里:O?

如何使用DataInputStream在int变量中存储值,或者它不适用于那个;)?


它实际上是意在解码数据最初使用

''DataOutputStream''写的!虽然结果看起来很奇怪,但它的行为确实应该是* b $ b *。如前所述,问题在于''readInt''提供了错误的数据类型的数据。
我知道流以不同的格式存储它
System.out不明白但是如果我们在某些计算中使用变量
''a'(当然我们将为什么
你想读它;))然后整个计算将会出错我错过了什么?




这是一个困扰许多Java新手的问题 - 生活和学习:)!


我希望这会有所帮助。


Anthony Borla


hi

它被设计为读入一个编码为二进制整数的值。来自''System.in''的
值不是*编码的,因此您会看到
是随机或无意义的值。


来自''System.in''的数据被编码为字符。当执行交互式控制台I / O时,您会发现输入仅仅是由换行符终止的一系列字符。


我没有遵循这个,我的意思是System.in是InputStream的对象

这是ByteStream&不是字符流所以你的观点是什么?

这实际上意味着解码最初使用
''DataOutputStream''编写的数据!虽然结果看起来很奇怪,但它的行为确实*应该如此。如前所述,问题是错误的数据类型已经提供给''readInt''。



http://java.sun.com/docs/books/tutor。 ..ataIODemo.java


这是SUN的I / O教程中的一个例子

相信我,当我下载这个,编译和放大器;运行它DIDNT工作

用readInt()读取的输入显示为?????

试试吧&让我知道如果你得到同样的东西

i使用WinXP,j2se 1.4.1_02


amey


< blockquote>

" Amey Samant" <是***** @ yahoo.com>在消息中写道

news:66 ************************** @ posting.google.c om ...

它被设计为读入一个编码为二进制整数的值。来自''System.in''的值是*不*
如此编码,因此你得到的似乎是随机或无意义的值。

相反,来自''System.in''的数据被编码为
字符。在执行交互式控制台的I / O时,您会发现输入只是由换行符终止的一系列
字符。



我没有按照这个,我的意思是System.in是InputStream的一个对象,它是ByteStream&不是一个角色
流你的意思是什么???




我的观点,以及我打扰回复你的查询的唯一原因是

可以帮助您解决问题,并且,作为副产品,帮助教你一点关于溪流的b $ b。


试试我发布的代码 - 它说明了一个已知的,高效的,可靠的
方法,用于从字符输入中交互式地提取''int''值

stream。

它实际上意味着解码最初使用''DataOutputStream''编写的数据!虽然结果看起来很奇怪,但它的行为正好*应该如此。如前所述,
问题是readInt提供了错误类型的数据。



http://java.sun.com/ docs / books / tutor ... dot1 / DataIODem

o.java
这是来自SUN的I / O教程的一个例子
相信我的时候我下载了这个,编译和&运行它DIDNT
工作用readInt()读取的输入看起来像?????
尝试它&让我知道如果你得到同样的东西
我使用WinXP,j2se 1.4.1_02




不,我没有时间进行这样的实验。我相信我已经超过了b

$ b *发布有用的代码,你可以修改和使用

*简要介绍一下为什么遇到这样的困难


我建议你重新阅读教程,注意两个流的原因

和读者[角色流]类存在。


Anthony Borla


hi all

i was trying to read an integer from keyboard with DataInputStream
can someone explain the behaviour of the following code ????

<code>
DataInputStream dis=new DataInputStream(System.in);
DataOutputStream dout = new DataOutputStream(System.out);
a=dis.readInt();
System.out.println("Using system.out a = "+a);
dout.writeInt(a);
a++;
System.out.println("Using system.out a = "+a);
dout.writeInt(a);
dout.flush();
</code>

the output is as under when 56 was entered as input
<output>
Using system.out a = 892734730
56
Using system.out a = 892734731
56

</output>

isnt it logical that if readInt reads an integer (56) , variable a
should store 56 & not some other value ....
what format does the DataInput reads in ?
coz when DataOutput is used the 56 appears corectly also the character
'' '' where did it come from :O ?

how do you store value in an int variable using DataInputStream or is
it not meant for that ;) ?
i understand that stream stores it in different format that System.out
does not understand
but if we use the variable ''a'' in some computation (of course we will
otherwise why will you want to read it ;) )then the whole computation
will go wrong
what am i missing ?

amey

解决方案

"Amey Samant" <am*****@yahoo.com> wrote in message
news:66**************************@posting.google.c om...

hi all

I was trying to read an integer from keyboard with
DataInputStream can someone explain the behaviour
of the following code ????

<code>
DataInputStream dis=new DataInputStream(System.in);
DataOutputStream dout = new DataOutputStream(System.out);
a=dis.readInt();
System.out.println("Using system.out a = "+a);
dout.writeInt(a);
a++;
System.out.println("Using system.out a = "+a);
dout.writeInt(a);
dout.flush();
</code>

the output is as under when 56 was entered as input
<output>
Using system.out a = 892734730
56
Using system.out a = 892734731
56

</output>

isnt it logical that if readInt reads an integer (56) ,
variable a should store 56 & not some other value ....
what format does the DataInput reads in ?

It is designed to read in a value which is encoded as a binary integer. The
values from ''System.in'' are *not* so-encoded, therefore you get what appear
to be random or nonsense values.

The data from ''System.in'' is, instead, encoded as characters. When
performing interactive console I/O you will find that input is merely a
series of characters terminated by the newline character. Given this, the
usual method for performing such I/O is something like:

BufferedReader br =
new BufferedReader(
new InputStreamReader(System.in));

String input;

while ((input = br.readLine()) != null)
{
...
System.out.println("Input Line: " + input);
...
int i;

try
{
i = Integer.parseInt(input);
System.out.println("Converted integer value: " + i);
}
catch (NumberFormatException e)
{
// error ...
}
...
}

coz when DataOutput is used the 56 appears corectly also
the character '' '' where did it come from :O ?

how do you store value in an int variable using
DataInputStream or is it not meant for that ;) ?

It''s actually meant to decode data that was originally written using a
''DataOutputStream'' ! Although the results look strange, it is behaving
*exactly* as it should. The problem, as mentioned earlier, is that the wrong
type of data has been supplied to ''readInt''.

i understand that stream stores it in different format that
System.out does not understand but if we use the variable
''a'' in some computation (of course we will otherwise why
will you want to read it ;) )then the whole computation
will go wrong what am i missing ?



It''s a problem that traps many Java novices - live and learn :) !

I hope this helps.

Anthony Borla


hi

It is designed to read in a value which is encoded as a binary integer. The
values from ''System.in'' are *not* so-encoded, therefore you get what appear
to be random or nonsense values.
The data from ''System.in'' is, instead, encoded as characters. When
performing interactive console I/O you will find that input is merely a
series of characters terminated by the newline character.
i did not follow this, i mean System.in is an object of InputStream
which is ByteStream & not a character stream so what is your point ???
It''s actually meant to decode data that was originally written using a
''DataOutputStream'' ! Although the results look strange, it is behaving
*exactly* as it should. The problem, as mentioned earlier, is that the wrong
type of data has been supplied to ''readInt''.



http://java.sun.com/docs/books/tutor...ataIODemo.java

this is an example from SUN s tutorial on I/O
believe me when i downloaded this, compiled & run it DIDNT work
the input read with readInt() appears like "?????"
try it & let me know if you get the same thing
i use WinXP , j2se 1.4.1_02

amey



"Amey Samant" <am*****@yahoo.com> wrote in message
news:66**************************@posting.google.c om...

It is designed to read in a value which is encoded as a
binary integer. The values from ''System.in'' are *not*
so-encoded, therefore you get what appear to be
random or nonsense values.

The data from ''System.in'' is, instead, encoded as
characters. When performing interactive console
I/O you will find that input is merely a series of
characters terminated by the newline character.



i did not follow this, i mean System.in is an object of
InputStream which is ByteStream & not a character
stream so what is your point ???



My point, and the only reason for my bothering to respond to your query, was
to help you solve your problem, and, as a by-product, help teach you a
little about streams.

Try the code I posted - it illustrates a known, efficient, and reliable
method for interactively extracting ''int'' values from a character input
stream.

It''s actually meant to decode data that was originally
written using a ''DataOutputStream'' ! Although the results
look strange, it is behaving *exactly* as it should. The
problem, as mentioned earlier, is that the wrong type of
data has been supplied to ''readInt''.



http://java.sun.com/docs/books/tutor...dot1/DataIODem
o.java
this is an example from SUN s tutorial on I/O
believe me when i downloaded this, compiled & run it DIDNT
work the input read with readInt() appears like "?????"
try it & let me know if you get the same thing
i use WinXP , j2se 1.4.1_02



No, I don''t have the time to conduct such experiments. I believe I''ve been
more than helpful:

* Posted useful code which you can modify and use
* Briefly described why you were experiencing such
difficulties

I suggest that you re-read the tutorials noting the reasons why both stream
and reader [character stream] classes exist.

Anthony Borla


这篇关于数据输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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