java输入输出弱智问题

kentcyq 2007-02-09 11:40:04
为什么readInt()进来是的那么怪的一个数字呢,还只能用writeInt()输出
大哥们讲讲writeInt和system.in.println的区别把

import java.io.*;
public class testmy {
public static void main(String[] args) throws IOException{
InputStream stdin = new FileInputStream ( "a.txt" );
DataInputStream in = new DataInputStream ( stdin );
OutputStream fout = new FileOutputStream ( "b.txt");
DataOutputStream out = new DataOutputStream ( fout );
int i ;
i = in.readInt() ;
System.out.println( i );
out.writeInt ( i );
}

}
运行结果:

输入:
a.txt : 1

输出:
b.txt : 1
屏幕:822938125
...全文
500 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
beexk 2007-02-15
  • 打赏
  • 举报
回复
呵呵。忘了你说“除了用scanner.nextInt”了呢。不好意思啊。。
beexk 2007-02-15
  • 打赏
  • 举报
回复
java 1.5

import java.util.*
...
Scanner sc=new Scanner(System.in);
int c=sc.nextInt();
kentcyq 2007-02-14
  • 打赏
  • 举报
回复
谢谢大家的解释:)
不过,有没有办法使readInt读的就是数字呢?
比如我要写一个读两个数字求和的程序,可以用DataInputStream.readInt实现吗?
除了用scanner.nextInt,大侠们一般用什么读入数字呢?
SAsura 2007-02-11
  • 打赏
  • 举报
回复
楼上的楼上正解
szm880828 2007-02-11
  • 打赏
  • 举报
回复
关注。。。。
dash_running 2007-02-10
  • 打赏
  • 举报
回复
writeInt(int i)把i按四个字节,二进制形式写到输出流里,此处是文件输出流

822938125转换为二进制是 110001 00001101 00001010 00001101
每个字节对应的十进制是 49 13 10 13
字符 '1' 回车'\r' 换行'\n' 回车'\r'
所以文件里看到的是 1
控制台输出的是 822938125
dprk 2007-02-10
  • 打赏
  • 举报
回复
import java.io.*;

public class testmy {
public static void main(String[] args) throws IOException {


DataOutputStream f1 = new DataOutputStream(
new FileOutputStream("a.txt") );
f1.writeInt(12);


InputStream stdin = new FileInputStream("a.txt");
DataInputStream in = new DataInputStream(stdin);
System.out.println( in.readInt() );

f1.writeInt( 123 );

int i = in.readInt();
System.out.println( i );


f1.close();

/* OutputStream fout = new FileOutputStream("b.txt");
DataOutputStream out = new DataOutputStream(fout);
int i;
i = in.readInt();
System.out.println(i);
out.writeInt(i);*/
}

}
DataInputStream 和 DataInputStram的用法和别的不大一样
改了你的代码 简单示范一下 深入的我也不很明白
For_suzhen 2007-02-10
  • 打赏
  • 举报
回复
System.out===〉“标准”输出流
int readInt()
throws IOException读取四个输入字节并返回一个 int 值。设 a 为第一个读取字节,b 为第二个读取字节,c 为第三个读取字节,d 为第四个读取字节。返回的值是:


(((a & 0xff) << 24) | ((b & 0xff) << 16) |
((c & 0xff) << 8) | (d & 0xff))
此方法适用于读取用接口 DataOutput 的 writeInt 方法写入的字节。

返回:
读取的 int 值。
////////////////////////////////////////////////////////////////
我也看不懂了,你看看吧
同时希望高手给点通俗易懂的解释,我基础很差,抱歉

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧