Java中缓冲数组读取文件怎样定位每次读取的位置

byte[] buf=new byte[1024]
while(length=fileInputStream.read(buf)!=-1)
{
...
}
每次对文件读取1024字节的,但是是什么原因确保下次读取1024个字节的时候,读取的位置是从上一次读完的位置开始的

被读取的文件可以放在硬盘的任意位置。 只要你新建文件IO流对象的时候把文件的物理路径写对就行了。代码例子如下:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* file IO流读取并输出文件
* @author young
*
*/
public class FileIO {
public static void main(String[] args) throws IOException {

FileInputStream fis = new FileInputStream("F:/workspace/one/src/filecode/FileIO.java");// 要读的文件路径

InputStreamReader isr = new InputStreamReader(fis);// 字符流

BufferedReader br = new BufferedReader(isr); // 缓冲
String line = null;

while ((line = br.readLine()) != null) {// 字符不等于空
System.out.println(line);// 一行一行地输出
}
br.close();// 关闭文件
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-06-24
java使用read()方法进行读文件中的四个字节保存在数组总的示例如下:

public static void main(String[] arg) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("E:/test.txt"));
int[] list = new int[20];
int i = 0;
String line = null;
while ((line = reader.readLine()) != null) {
String[] vStrs = line.split(" ");
for (String str : vStrs) {
list[i++] = Integer.parseInt(str);
}
}
System.out.println(Arrays.toString(list));
}追问

怎么定位文件中的读取位置,请用汉语描述一下

本回答被网友采纳
相似回答