1

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

1
  • By "requirement" do you mean "homework" ? Commented Mar 15, 2012 at 6:12

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Kadne is there a possible way to do I/O without using any stream class in java? I don't think the difficulty is up to using JNI.
0

I would use JNI if streams are not allowed!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.