Locking of files

Hi,

Can someone guide me how can we lock the files and then read it. I know we can do it using the FileLock and FileChannel. But I want to do it using FileReader as my file contains charecters and I think using FileReader will be much better than using FileInputStream.

Can a channel be obtained using FileReader?

Please let me know about this.

[370 byte] By [Amana] at [2007-10-2 4:38:07]
# 1
I have no direct answer but why not explore possible solutions. You obviously know what options you have so why not check them out? Have you tried that and stumbled?
uj......a at 2007-7-16 0:11:08 > top of Java-index,Java Essentials,Java Programming...
# 2
But what is better FileReader or FileInputStream for reading charecter files?
Amana at 2007-7-16 0:11:08 > top of Java-index,Java Essentials,Java Programming...
# 3
From the JavaDoc of FileInputStream:FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
Franck_Lefevrea at 2007-7-16 0:11:08 > top of Java-index,Java Essentials,Java Programming...
# 4
Right...but then how to lock the files by using FileReader.
Amana at 2007-7-16 0:11:08 > top of Java-index,Java Essentials,Java Programming...
# 5

The FileReader is actually a convenience class, and as such, can be rather inconvenient when you want to do something that isn't default behavior.

Try constructing an InputStreamReader on top of a FileInputStream. Then you get the character-reading behavior at the same time that you're exposing the input stream behavior you want to be able to deal with.

(Note that when you construct a new FileReader, it's constructing a FileInputStream, too. It's just not giving you an easy way to see it.)

--Dylan

Dylan.Piercea at 2007-7-16 0:11:08 > top of Java-index,Java Essentials,Java Programming...
# 6

Just an idea.

If a file is being read, it cannot be deleted by java or any windows app.

If you want to know if your file is in use in another part of your program just compare them to see if it's the same file, if it is, put your thead in to a loop with a wait() until the file no longer matches, then continue the thread. Sounds right to me.

gaffer_uka at 2007-7-16 0:11:08 > top of Java-index,Java Essentials,Java Programming...
# 7
sorry, that should be sleep() not wait(), i was thinking in another language
gaffer_uka at 2007-7-16 0:11:08 > top of Java-index,Java Essentials,Java Programming...
# 8
Start with a FileInputStream. Get the FileChannel from that for locking purposes. Use an InputStreamReader over the FileInputStream for I/O if you like.
ejpa at 2007-7-16 0:11:08 > top of Java-index,Java Essentials,Java Programming...