Read and write file by character stream in java I/O

Monday, December 28th, 2015 (0 Comment)

We had to learn and use the byte stream to read and write file. However, in some cases the byte stream is not the ideal to manage data entry type character, so the java has given character stream pattern serving for data entry type character on stream.

We will use two abstract classes Reader and Writer. Reader layer for data input, Writer classes for the data output. These classes Reader and Writer derived from manipulating Unicode character streams.

You can find more detail on the internet to understand about these classes.

In here, we focus on the examples easier to understand about read and write file by character stream.

Usually to read/write files people use byte streams, but for our character streams can also be performed. The advantage of using character streams is that they work directly on the Unicode characters. So character streams is the best choice to keep the text as Unicode.

1. Class: Read and write file

The two classes used for Read and write file data to the file’s character are FileReader and FileWriter.

Example: Read the text from the keyboard and write them down file named “test.txt“. The read and write ends when the user enters the string “stop”.

Input from Console:

Output: write to file at D:\\lvtutorial\\test.txt

Example: read and display the contents of the file “test.txt” on screen

Input: read file at D:\\lvtutorial\\test.txt

Output: display on screen

2. Random Access Files

Besides the handling of import on style sequential file through the stream, java also supports random access the contents of a file using RandomAccessFile. RandomAccessFile is not InputStream or OutputStream but it implements interfaces DataInput, DataOutput (which defines the method of I/O base). RandomAccessFile support cursor positioning problem files in a file that uses the methods seek.

Example: illustrates the random access file. Program will write 6 number of doubles into files, then read random order

Output:

 

Tags: , , ,

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Leave a Reply

Your email address will not be published. Required fields are marked *