JAVA中String(data,0,rs)

byte[] data= new byte[64];FileInputStream fis=new FileInputStream(file);while((rs=fis.read(data))>0){……dataStr +=new String(data,0,rs);……}求解释String中 data、0、rs分别是干什么的,特别是0和 rs。谢谢!!!
2026年09月25日 19:34
有2个网友回答
网友(1):

data就是你定义的byte类型的数组,0是起始位置,rs指的是结束位置。也就是说,每次读取一定字节数的数据,然后写入到一个新字符串中。rs就是新字符串的长度,也就是结束位置。每次读取64个字节。然后每次将新字符串添加到dataStr中。完成读取文件的操作。

网友(2):

API中的解释:
Constructs a new String by decoding the specified subarray of bytes
using the platform's default charset. The length of the new String
is a function of the charset, and hence may not be equal to the length of the
subarray.
通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。新 String
的长度是字符集的函数,因此可能不等于该子数组的长度。
参数:
bytes - 要解码为字符的 byte,就是你的data变量
offset - 要解码的第一个 byte 的索引,就是你的0
length - 要解码的 byte 数,就是你的rs