humansg
humansg

Reputation: 715

Java FileChannel class without using Java stream class

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

Upvotes: 1

Views: 373

Answers (2)

Shashank Kadne
Shashank Kadne

Reputation: 8101

There is no way to access a FileChannel without using either a InputStream, OutputStream, or a RandomAccessFile.

And RandomAccessFile implements DataInput and DataOutput which provides an interface for reading bytes from a binary stream. So I believe even RandomAccessFile is a kind of stream class.

Edit:

I really don't know with what context you are calling a class as a Stream class. RandomAccessFile can 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, RandomAccessFile doesn'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, use RandomAccessFile.

Upvotes: 3

styken
styken

Reputation: 74

I would use JNI if streams are not allowed!

Upvotes: 0

Related Questions