java 文件流问题
来源:百度知道 编辑:UC知道 时间:2024/05/10 11:07:14
D:盘下有两个文档 aa.txt , 内容为 1234 经过下面代码操作之后另一个文档 a.txt 的内容为 1234
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java. io.*;
public class Abc {
public static void main(String[] args) throws Exception
{
int b;
DataInputStream in =new DataInputStream( new FileInputStream("D:\\a.txt"));
DataOutputStream out1 =new DataOutputStream(new FileOutputStream("D:\\aa.txt"));
b=in.readInt();
out1.writeInt(b);
System.out.println(b);
in.close();
out1.close();
}
}
但是为什么 System.out.println(b); 输出的值是 825373492 ,怎么来的?
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java. io.*;
public class Abc {
public static void main(String[] args) throws Exception
{
int b;
DataInputStream in =new DataInputStream( new FileInputStream("D:\\a.txt"));
DataOutputStream out1 =new DataOutputStream(new FileOutputStream("D:\\aa.txt"));
b=in.readInt();
out1.writeInt(b);
System.out.println(b);
in.close();
out1.close();
}
}
但是为什么 System.out.println(b); 输出的值是 825373492 ,怎么来的?
1234存于文本中实际上是字符串
它对应的16进制是31323334(因为1的ascii码是49=0X31,其它类推)
当你把它当成int读进来的时候,他实际读的是0x31323334,这个你就是你在里面赋值的b
我测试过了,确实是等于825373492
和上面说的,实际1234是以字符串形式存在,你这样输出出来是这样的数据,但存到另一个文件里面的确实是1234,这个你可以看到。如果你想也输出1234,就把b定义成String类型,然后用b=in.readLine();输出的就是1234了。你可以试试。
你的输入输出流写反了吧,输入流应该对应的aa.txt,而输出流应该对应a.txt