Determine how to read and write non-text data in Java. 

 

For example,

if a datafile contained the characters AB, reading the datafile will produce the integer 16,706 or 16,961.

Writing the integers 16,706 or 16,961 to a file, when opening with Notepad (or VI) would show AB or BA.

 

 

Camron:

You can write non-text data in Java by using various input/output streams found in java.io . Here are two example programs that use DataInputStream, DataOutputStream , FileInputStream, and FileOutputStream to read and write non-text data.

 

 

 

import java.io.*;
public class dataWrite
{
public static void main(String[]args)
{
try
{
//"data.txt" will be created in the same directory as the .class file
DataOutputStream out = new DataOutputStream(new FileOutputStream("data.txt"));
out.writeShort(16706); //Writes AB
}
catch (IOException e)
{
System.out.println("error");
}
}
}

import java.io.*;
public class dataRead
{
public static void main(String[]args)
{
try
{
//"data.txt" must be in the same directory as the .class file
DataInputStream in = new DataInputStream(new FileInputStream("data.txt"));
int get = in.readShort(); //Reads a short from data.txt
System.out.println(get); //Should print 16706 (if data.txt contains "AB")
}
catch (IOException e)
{
System.out.println("error");
}
}
}

 // int x = 16706;

// write out x to a datafile withoput format.

// open the file in notepad

// do you see  AB or BA?

// 

You see AB

 





Armando Fonseca March 18, 2008.

 

How can we read and write non-text data in java? we can do this in many ways:

 

  • To get file properties use the File class.
  • To read binary files we can use one of the three read methods of a FileInputStream.
  • To read primitive types we construct a DataInputStream from the basic FileInputStream and use readInt and other similars methods.
  • To read objects we construct an ObjectInputStream and use the readObject method. Such objects must implement the Serializabe interface.
  • To seek specific locations without having to process the file sequeantly, RandomAccessFile used for both input and output.
  • Analogous classes and methods exist for writing non-text ouputs.

 

Now some remarks before you go!

 

 REMARK1-->We are also  reading bytes from a platform, therefore there is a dependency on i/o bytes.  The abstract InputStream class contains three read methods. the method

 

    public native int read() throws IOException;

 

    Where the modifier native indicates that java implements this method in a platform dependent manner. reads a single byte. The int return type guarantees that the return value     will be positive.

 

    public int read (byte [] b) throws IOException;

 

    reads into a byter array. It may not fill the array if not enough input bytes are avaliable. The three argument version of read:

 

 

    public int read(byte [] b, int off, int len) throws IOException;

 

    reads into an array of butes, with the second argument specifying the starting offset in the file , and the third giving the number of bytes to read. the last two read methods           return the number of butes read, or -1 if at the end of the file.

 

REMARK2-->Because files represent external resources there is always a possibility of hardware failure or corruption or deletion by other users, so you must catch the IOException that would be thrown when such an error occurs. Omitting  the try-catch code will cause compiler error.

 

REMARK3-->We read and displays bytes from standard input by default, but can read from a file by entering its name on the command line. Java declares the standard input stream, System.in, usually the keyboard, as an InputStream. The FileInputStream let us read from a file.

 

 

 

 

 

 

 

 

 

 

 

 


Page Information

  • 5 months ago [history]
  • View page source
  • You're not logged in
  • No tags yet learn more

Wiki Information

Recent PBwiki Blog Posts