- @Test
- public void test(){
- FileInputStream inputStream = null;
- FileChannel inChannel = null;
- IOException exception = null;
- try {
- File file = new File("C://1301.txt");
- inputStream = new FileInputStream(file);
- inChannel = inputStream.getChannel();
- long position = 200;
- inChannel.position(position);
- ByteBuffer buffer = ByteBuffer.allocate(500);
- while(inChannel.size() > inChannel.position()){
- int length = inChannel.read(buffer);
- byte[] array = new byte[length];
- buffer.flip();
- buffer.get(array);
- System.out.print(new String(array));
- buffer.clear();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- exception = e;
- } catch (IOException e) {
- e.printStackTrace();
- exception = e;
- } finally {
- try {
- if(inputStream != null){
- inputStream.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- if(exception == null){
- exception = e;
- }
- }
- if(exception != null){
- throw new RuntimeException(exception);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0810201410518.html
来源: http://www.codesnippet.cn/detail/0810201410518.html