日期:2014-05-20  浏览次数:20804 次

字节输入输出流读写文件
这个程序为什么只输出第一行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);