How to use java.nio.channels.FileChannel to read to ByteBuffer achieve similiar behavior like BufferedReader#readLine()?

The reason is that you need to use FileOutpuStream instead of FileInputStream. Please try this code.

Up vote 1 down vote favorite share g+ share fb share tw.

I want to use java.nio.channels. FileChannel to read from a file, but I want to read line per line like BufferedReader#readLine() does. The reason why I need to use java.nio.channels.

FileChannel instead of java. Io is because I need to put a lock on a file, and read line by line from that lock file. So I am force to use java.nio.channels.FileChannel.

Please help EDIT Here is my code trying to use FileInputStream to get the FileChannel public static void main(String args){ File file = new File("C:\\dev\\harry\\data. Txt"); FileInputStream inputStream = null; BufferedReader bufferedReader = null; FileChannel channel = null; FileLock lock = null; try{ inputStream = new FileInputStream(file); channel = inputStream.getChannel(); lock = channel.lock(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String data; while((data = bufferedReader.readLine())! = null){ System.out.

Println(data); } }catch(IOException e){ e.printStackTrace(); }finally{ try { lock.release(); channel.close(); if(bufferedReader! = null) bufferedReader.close(); if(inputStream! = null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } when the code is here lock = channel.lock();, it is immediately go to the finally and lock is still null, so lock.release() generate NullPointerException.

I am not sure why. Java nio bytebuffer filechannel link|improve this question edited Jun 14 '11 at 16:01 asked Jun 14 '11 at 15:44Thang Pham1,87722980 81% accept rate.

The reason is that you need to use FileOutpuStream instead of FileInputStream. Please try this code: FileOutputStream outStream = null; BufferedWriter bufWriter = null; FileChannel channel = null; FileLock lock = null; try{ outStream = new FileOutputStream(file); channel = outStream.getChannel(); lock = channel.lock(); bufWriter = new BufferedWriter(new OutputStreamWriter(outStream)); }catch(IOException e){ e.printStackTrace(); } This code works fine for me. The NUllPointerException is actually hiding the real exception i.e.

NotWritableChannelException. For locking I think we need to use OutputStream instead of InputStream.

I try that, for some reason, it does not work well when I try to lock the file with FileInputStream. Not sure why, – Thang Pham Jun 14 '11 at 15:54 I remeber I have used this before without any problems..can you tell me what is nto working – Suraj Chandran Jun 14 '11 at 15:55 @Suraj: I have update my post, with my code using FileInputStream, can you take a look? – Thang Pham Jun 14 '11 at 16:01 The reason is that you need to use OutputStream instead of INputStream, I will update the answer – Suraj Chandran Jun 14 '11 at 16:07 But I try to read from the file, not write.

I cant read using OutputStream, or am I missing something here? – Thang Pham Jun 14 '11 at 16:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions