Why does java's RandomAccessFile not return on linux?
Since none of the commenters chose to write an answer, I'll write one myself.
The problem is that opening a named pipe on linux will block until the other end of the pipe has also been opened. You can either open it in non-blocking mode or open both ends of the pipe at once.
RandomAccessFile access to /dev/raw/raw1 in java
From the raw(8) man page:
Because raw I/O involves direct hardware access to a process's memory, a few extra restrictions must be observed. All I/Os must be correctly aligned in memory and on disk: they must start at a sector offset on disk, they must be an exact number of sectors long, and the data buffer in virtual memory must also be aligned to a multiple of the sector size. The sector size is 512 bytes for most devices.
If you do not obey these restrictions the low-level write(2) system call will return EINVAL (Invalid argument).
You're going to have a tough time getting Java to issue correctly-sized and -aligned write calls given the many layers of abstraction in between it and the OS. The last requirement is particularly sticky: "the data buffer in virtual memory must also be aligned to a multiple of the sector size." No guarantees, but you may have better luck using java.nio
classes instead of java.io
.
Java - RandomAccessFile (Emulating the Linux tail function)
I would think that you would be best served by grabbing a block of bytes based on the file's length, then release the file and parse a ByteArrayInputStream (instead of trying to read directly from the file).
So use RandomAccessFile#read(byte[]), and size the buffer using the returned file length. You won't always show the exact end of the file, but that is to be expected with this sort of polling algorithm.
As an aside, this algorithm is horrible - you are running IO operations in a crazy tight loop - the calls to File#length() will block, but not very much. Expect this routine to take your app to it's knees CPU-wise. I don't necessarily have a better solution for you (well - actually, I do - have the source application write to a stream instead of a file - but I recognize that isn't always feasible).
In addition to the above, you may want to introduce a polling delay (sleep the thread by 100ms each loop - it looks to me like you are displaying to a GUI - a 100ms delay won't hurt anyone, and will greatly improve the performance of the swing operations).
ok - final beef: You are adjusting a Swing component from what (I hope) is code not running on the EDT. Use SwingWorker#invokeLater() to update your text pane.
RandomAccessFile Java - complexity
Seek just positions the internal pointer, it doesn't read anything from the disk.
Inserting text in middle using RandomAccessFile removes some text after that
RandomAccessFile
A random access file behaves like a large array of bytes stored in the
file system.
In fact it does not care about shifting the array elements in the case of write operations (only the pointer is advanced). Such an operation overwrites existing values:
Output operations write bytes starting at the file pointer and advance
the file pointer past the bytes written.
How does Random Access File Work
Maybe you are looking for a tutorial like this?
http://examples.javacodegeeks.com/core-java/io/randomaccessfile/java-randomaccessfile-example/
RandomAccessFile Dangling pointer
EDIT : precision for Windows (thanks to pingw33n)
It is perfectly normal that you get no Exception
when :
- you open a file
- you or someone else deletes the file
- you still access to the file, read what it contained before delete, or write to it
In fact the removal of a file do nothing to the file itself. What is removed is an entry in a directory. And the file will be actually destroyed (and the sectors it uses on disk will be released) only when :
- no more directory entries point to it
- no file descriptors keep it opened
So even if the byte you ask in not buffered in memory, the file system still knows how to get it from disk. By the way, it is a common pattern to create temporary files, that is files that will be deleted on last close.
Of course, you can do what merlin2011 suggests, that is test the presence of the file via its path. But you must know that is the file is deleted and then created again, the path (that was used to open the file) is present, but points to a totaly different object.
So if you really need that the file actually reflects the content of the directory, you cannot keep it opened and must reopen it at each and every acces ... It this is not a fair option you still can:
- ignore modification to directory and file system ; you have a file and you use it, full stop. There are many use cases where this is correct.
- state in you documentation that the directory is yours and nobody else should delete a file in it. And after all you cannot prevent an admin to break its system or kill your app.
This is true for all normal filesystems, all those of Linux or other Unix like systems, NTFS, etc. I am not sure that it is still true for older one such such as CPM or FAT, but they are no longer currently used in production :-). But under Windows, it should not be possible to delete a file currently opened in a java application.
To answer precisely to you 2 questions :
- your pointer is not dangling but still points to a real file (even if nobody else can see it)
- Exception will be thrown in case of file inaccessibility (physical damage to disk or connections, file system errors, etc.). But if only the entry was removed, the file is still accessible
Related Topics
Why This Javac: File Not Found Error
How to Check If a File Is Open by Another Process (Java/Linux)
Setting/Changing the Ctime or "Change Time" Attribute on a File
Obtaining the Thread Id for Java Threads in Linux
Could Not Reserve Enough Space for Object Heap to Start Jvm
Listview Onitemclicklistener Not Responding
Division in Java Always Results in Zero (0)
Understand the R Class in Android
How to Get the First Day of the Current Week and Month
Firebase:Recycler View No Adater Attached , Skipping Layout
Android N Change Language Programmatically
Android - Span_Exclusive_Exclusive Spans Cannot Have a Zero Length
Why Stringbuilder Stops Adding Elements After Using the Null Character
Eclipse Release Heap Back to System
How to List Target Platforms. Please Make Sure the Android Sdk Path Is Correct