//按自定义缓冲区大小读取 |
public static void main(String[] args) throws Exception { |
int bufSize = 1024 ; |
byte [] bs = new byte [bufSize]; |
ByteBuffer byteBuf = ByteBuffer.allocate( 1024 ); |
FileChannel channel = new RandomAccessFile(fff, "r" ).getChannel(); |
while (channel.read(byteBuf) != - 1 ) { |
int size = byteBuf.position(); |
byteBuf.rewind(); |
byteBuf.get(bs); // 把文件当字符串处理,直接打印做为一个例子。 |
System.out.print( new String(bs, 0 , size)); |
byteBuf.clear(); |
} |
} |
//按行读取 |
public static void main2(String[] args) throws Exception { |
BufferedReader br = new BufferedReader( new FileReader(fff)); |
String line = null ; |
while ((line = br.readLine()) != null ) { |
System.out.println(line); |
} |
} |
//读取指定行数 |
public static void main3(String[] args) throws IOException { |
String path = "你要读的文件的路径" ; |
RandomAccessFile br = new RandomAccessFile(path, "rw" ); //这里rw看你了。要是之都就只写r |
String str = null , app = null ; |
int i = 0 ; |
while ((str = br.readLine()) != null ) { |
i++; |
app = app + str; |
if (i >= 100 ) { //假设读取100行 |
i = 0 ; |
// 这里你先对这100行操作,然后继续读 |
app = null ; |
} |
} |
br.close(); |
} |