首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

字节输入输出流读写资料

2013-04-21 
字节输入输出流读写文件这个程序为什么只输出第一行123456789File filenew File(d:/1.txt)try{file.cr

字节输入输出流读写文件
这个程序为什么只输出第一行123456789
字节输入输出流读写资料

File file=new File("d:/1.txt");
try
{
    file.createNewFile();
    OutputStream out=new FileOutputStream(file);
    out.write("12345789\r\n一二三四五六七八九".getBytes());
    InputStream in=new FileInputStream(file);
    int tempByte;
    while((tempByte=in.read())!=-1)
    {
System.out.write(tempByte);
    }
    
}
catch (IOException e)
{
    e.printStackTrace();
} io流
[解决办法]
其实你的已经输出来了。
out.write("12345789\r\n一二三四五六七八九".getBytes());
in.read()读一个byte,虽然是返回int,但实际上是从内存中读一个byte数据。
显示出来的时候,不是完整汉字。
你改为以下,看一下内存中的值就明白什么意思了。

输出为这个值吧:
System.out.println("\t" + tempByte);

热点排行