-1

I am doing following: - creating an empty file - locking file - write to file - reading back the content

public class TempClass {
public static void main(String[] args) throws Exception{
    File file = new File("c:/newfile.txt");
    String content = "This is the text content123";

        if (!file.exists()) {
            file.createNewFile();
        }

        // get the content in bytes
        byte[] contentInBytes = content.getBytes();

        FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel();

        FileLock lock = fileChannel.lock();

        //write to file 
        fileChannel.write(ByteBuffer.wrap (contentInBytes));

        //force any updates to this channel's file                  
        fileChannel.force(false);

        //reading back file contents
        Double dFileSize = Math.floor(fileChannel.size());
        int fileSize = dFileSize.intValue();
        ByteBuffer readBuff = ByteBuffer.allocate(fileSize);
        fileChannel.read(readBuff);

        for (byte b : readBuff.array()) {
            System.out.print((char)b);
        } 
        lock.release();
       }    

}

However I can see the file is correctly written with the content I specified, but when I read it back, it prints square characters for all the actual characters in the file. That square character is char equivalent of the byte 0:

System.out.print((char)((byte)0));

Whats wrong here?

1 Answer 1

2

When reading back the file, you did not reset the position in which the FileChannel is currently at, thus when performing

fileChannel.read(readBuff);

Nothing is being allocated to the buffer as the FileChannel is positioned at the end of the file (causing your print code to show the 0-initialized values).

Perform:

fileChannel.position(0);

to reset the FileChannel to the start of the file.

8
  • yess great you just pin pointed, I have to fileChannel.position(0); after fileChannel.write() and before fileChannel.read(readBuff);
    – Mahesha999
    Nov 7, 2013 at 9:25
  • is there any way that I can delete the file while I am having lock on it, since I just want read the file and then delete it without further allowing anyone else it to read it again.
    – Mahesha999
    Nov 7, 2013 at 9:26
  • Well, just use the File.delete() method after you release the lock... I highly doubt some program will gain the lock between the lock release and the delete method if they are positions right after each other. FileChannel is reserved for performing I/O to the file, to delete it, use the File object itself.
    – initramfs
    Nov 7, 2013 at 9:29
  • so thats bad right? we cannot ensure this? cannot securely deleting file? is there any other way out for this? I think that is possible since to gain such control over file if someone continuously runs code to access it in loop?
    – Mahesha999
    Nov 7, 2013 at 9:32
  • @Mahesha999 Well... I'm not clear if you can perform the delete while having the FileChannel/FileLock open... Best way to find out is to try it... If it works, then you achieved your desired purpose, if not, then the b est alternative is what I've stated earlier.
    – initramfs
    Nov 7, 2013 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.