write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流

wren4255 2008-04-08 09:03:06
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); }
}
}
...全文
303 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
panxuan 2008-04-08
  • 打赏
  • 举报
回复
off - 开始写入字符处的偏移量
len - 要写入的字符数
caiming250 2008-04-08
  • 打赏
  • 举报
回复
偏移量 offlen 个字节 解释:

举例 对于一个byte[] 来说 如果你调用 write(byte[] b, int off, int len) 方法 那么你读取到的byte 会顺序写入到b中的 从b[off]开始 到b[off+len] 的 位置上面!

在这里 偏移量 也就是起始位置( >=0 ) len 也就是可以写入的 最大 长度!
caiming250 2008-04-08
  • 打赏
  • 举报
回复

你理解错了一点!

System.in.read(b); 这个方法的返回值是 你读取到的字符数量!!!

System.in.read(); 这个方法才是 :说是读取一个char,然后返回这个char的int型
wren4255 2008-04-08
  • 打赏
  • 举报
回复
谢谢哦,各位帮帮忙!!!!!!!!!!!!!!
wren4255 2008-04-08
  • 打赏
  • 举报
回复
System.in.read(b);
这个东西的返回值不明白哦,说是读取一个char,然后返回这个char的int型,那我输入111回车的时候,最后的应该是回车,还是1呢?

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧