java equivalent of .net FileStream

2.1k Views Asked by At

I have a small .Net library that I would like to port to java. Part of the requirement for this library is that it should serialize state into a file on disk and this file should remain "open" so other instances of the library cannot access this file but most create their own instance. In C# I have used the FileStream which is opened with FileShare.None to guard against others reading/writing to the file. When holding the FileStream I can read and write to the file, calling Flush() to write to disk and using SetLength(..) I can shorten an already existing file after write content.

In java I am not certain which class offers the most similar properties. The RandomAccessFile is my best bet but since I am not exactly well-versed in java I might be overlooking some better suited class or some caveat with RandomAccessFile.

1

There are 1 best solutions below

0
On

Check out FileWriter, read the API-Docs to see whether it suits your needs.

FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

Reading your post again, i actually think this might answer your question:

How to prevent file from being overridden when reading and processing it with Java?