参数int off不代表输入文件中的随机点。它实际上是 byte[ ] 中的偏移量,从 byte[ ]中写入数据的位置到len个字节。例如,假设你写了
byte buf[15];
read(buf, 5, 10);
这将从输入文件的开头读取数据,而不是从文件的第 5 个字节读取数据。但是数组buf[ ]将从第 5 个字节填充到最后一个字节 (5+10)。
要交叉检查,您可以为参数 off使用一些不同的值。无论您为off提供什么值,数据都将始终从文件的开头读取(如果您没有明确使用 seek)。
这里要注意的一点是数组的大小不能小于 off+len。
运行此示例以获得清晰的理解:
public class ReadHdfsFile {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/core-site.xml"));
conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/hdfs-site.xml"));
FileSystem fs = FileSystem.get(conf);
FSDataInputStream in = fs.open(new Path("/demo.txt"));
//Filling the array b1 from the 5th byte
int charPos = 0;
byte[] b1 = new byte[10];
int bytesRead = in.read(b1, 5, 5);
System.out.println("Bytes Read : " + bytesRead);
String s = new String(b1, "UTF-8");
System.out.println("Printing char by char(you'll see first 5 bytes as blank)...");
for(char c : s.toCharArray()){
System.out.println("Character " + ++charPos + " : " + c);
}
System.out.println();
System.out.println("Changing offset value....");
//Filling the array b2 from the 10th byte
in.seek(0);
charPos = 0;
byte[] b2 = new byte[15];
bytesRead = in.read(b2, 10, 5);
System.out.println("Bytes Read : " + bytesRead);
s = new String(b2, "UTF-8");
System.out.println("Printing char by char(you'll see first 10 bytes as blank)...");
for(char c : s.toCharArray()){
System.out.println("Character " + ++charPos + " : " + c);
}
System.out.println("DONE!!!");
in.close();
fs.close();
}
}
高温高压