write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流 红字是什么意思?int count;
byte b[] = new byte[512];
count = System.in.read(b);
FileOutputStream rt = new FileOutputStream("wren4256.txt");
rt.write(b,0,count);
上面的程序,当我输入111回车的时候,count的值为5,怎么回事
完整程序:
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.*;
public class wrenwren
{
public static void main(String args[]) throws IOException
{
try{
//InputStreamReader isr = new InputStreamReader(System.in);
//BufferedReader br = new BufferedReader(isr); System.out.println("何か入力してください:");
int count;
byte b[] = new byte[512];
count = System.in.read(b);
FileOutputStream rt = new FileOutputStream("wren4256.txt");
System.out.print("**************************************count:"+count);
rt.write(b,0,count);
System.out.println("成功");
File ft = new File("wren4256.txt");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM大鵬dd日hh時mm分");
System.out.println("getPath"+ft.getPath());
System.out.println("getAbsolutePath" + ft.getAbsolutePath());
System.out.println("getName" + ft.getName());
System.out.println("getParent" + ft.getParent());
System.out.println("length" + ft.length());
System.out.println("lastModified" + sdf.format(new Date(ft.lastModified())));
System.out.println("list" + ft.list());
rt.close();
}
catch (IOException e) { System.out.println(e); }
catch (Exception e) { System.out.println(e); }
}

解决方案 »

  1.   

    System.in.read(b); 
    这个东西的返回值不明白哦,说是读取一个char,然后返回这个char的int型,那我输入111回车的时候,最后的应该是回车,还是1呢?
      

  2.   


    你理解错了一点!System.in.read(b);  这个方法的返回值是 你读取到的字符数量!!!System.in.read(); 这个方法才是 :说是读取一个char,然后返回这个char的int型
      

  3.   

    偏移量 off 和 len 个字节   解释:  举例 对于一个byte[] 来说  如果你调用 write(byte[] b, int off, int len)  方法  那么你读取到的byte 会顺序写入到b中的 从b[off]开始 到b[off+len] 的 位置上面!在这里 偏移量 也就是起始位置( >=0 )    len 也就是可以写入的 最大 长度!
      

  4.   

    off - 开始写入字符处的偏移量
    len - 要写入的字符数