I want to do input/output from a file. My requirement is not to use any java stream classes.
I’ve done some research and most probably I will need to use a FileChannel.
However, how can I get a FileChannel object of a particular file without using the Java Stream classes?
Is it by using RandomAccessFile? By using RandomAccessFile class, do I fulfil the requirement of “not using any java stream classes”?
There is no way to access a
FileChannelwithout using either aInputStream,OutputStream, or aRandomAccessFile.And
RandomAccessFileimplementsDataInputandDataOutputwhich provides an interface for reading bytes from a binary stream. So I believe evenRandomAccessFileis a kind of stream class.Edit:
I really don’t know with what context you are calling a class as a Stream class.
RandomAccessFilecan still be use in your case, as the reading and writing to stream is very much abstract to user. It shows how to read or write files in a non-sequentially manner.In general, we classify IO stream into following categories..
Byte Streams handle I/O of raw binary data.
Character Streams handle I/O of character data, automatically
handling translation to and from the local character set.
Buffered Streams optimize input and output by reducing the number of
calls to the native API.
Scanning and Formatting allows a program to read and write formatted
text.
I/O from the Command Line describes the Standard Streams and the
Console object.
Data Streams handle binary I/O of primitive data type and String
values.
Object Streams handle binary I/O of objects.
However,
RandomAccessFiledoesn’t belong to any of the above categories.It comes under I/O mechanism introduced in the JDK 7 release(NIO). It will come under Channel I/O and not Stream I/O. So, useRandomAccessFile.