-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?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189

1 Answers1

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.

initramfs
  • 8,275
  • 2
  • 36
  • 58
  • yess great you just pin pointed, I have to `fileChannel.position(0);` after `fileChannel.write()` and before `fileChannel.read(readBuff);` – Mahesha999 Nov 07 '13 at 09: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 07 '13 at 09: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 07 '13 at 09: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 07 '13 at 09: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 07 '13 at 09:35
  • what if someone writes a loop to continuously gain file access. If loop attempts after releasing lock and deleting file, it may copy paste? – Mahesha999 Nov 07 '13 at 09:36
  • @Mahesha999 First of all, try deleting the file using `File.delete()` when the lock is engaged and see if it works. If not, then yes, your proposed scenario is possible but very unlikely. Is your program so security-critical that it needs immediate file erasure? If so, rather than deleting the file, using the lock you already acquired, first blank the file by writing over it with zeros. Then attempt to delete afterwards... Even if someone managed to copy-paste, they would have copy/pasted a blank file. – initramfs Nov 07 '13 at 09:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40713/discussion-between-mahesha999-and-cpu-terminator) – Mahesha999 Nov 07 '13 at 09:39