为什么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
大哥们讲讲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
int readInt()
throws IOException读取四个输入字节并返回一个 int 值。设 a 为第一个读取字节,b 为第二个读取字节,c 为第三个读取字节,d 为第四个读取字节。返回的值是:
(((a & 0xff) << 24) | ((b & 0xff) << 16) |
((c & 0xff) << 8) | (d & 0xff))
此方法适用于读取用接口 DataOutput 的 writeInt 方法写入的字节。 返回:
读取的 int 值。
////////////////////////////////////////////////////////////////
我也看不懂了,你看看吧
同时希望高手给点通俗易懂的解释,我基础很差,抱歉
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的用法和别的不大一样
改了你的代码 简单示范一下 深入的我也不很明白
每个字节对应的十进制是 49 13 10 13
字符 '1' 回车'\r' 换行'\n' 回车'\r'
所以文件里看到的是 1
控制台输出的是 822938125
不过,有没有办法使readInt读的就是数字呢?
比如我要写一个读两个数字求和的程序,可以用DataInputStream.readInt实现吗?
除了用scanner.nextInt,大侠们一般用什么读入数字呢?
...
Scanner sc=new Scanner(System.in);
int c=sc.nextInt();