`

FileChannel结合BufferedReader的readLine实现大文件快速定位

阅读更多

 

nio的这段代码,应该属于j2se基础

 

                File f = new File(file);
		FileInputStream in = new FileInputStream(f);
		FileChannel channel = in.getChannel();

		channel.position(2342198 - 1125); //要定位的位置

                System.out.println(channel.position());
		Channels.newReader(channel, "utf-8");
		BufferedReader reader=new BufferedReader(Channels.newReader(channel, "UTF-8"));
		
		String line;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
			System.out.println(channel.position());
		}
		reader.close();

根据不同的字符集设置
//Channels.newReader(channel, "ISO-8859-1");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics