[Java iO] _ data operation flow notes
Objectives of this chapter:
Measure the test taker's knowledge about the functions of dataoutputstream and datainputstream.
You can use dataoutputstream and datainputstream to write and read data.
Data Operation Flow
Two platform-independent data operation flows are provided in the IO package:
Dataoutputstream)
Datainputstream)
Generally, the data output stream outputs the data in a certain format, and then reads the data in a certain format through the data input stream.
Dataoutputstream is a subclass of outputstream. Its definition is as follows:
Public class dataoutputstream extends filteroutputstream implements dataoutput
The dataoutput interface defines a series of writexxx () operations that can be written into various data types.
The Definition Format of this interface should be roughly understood for later learning.
To use dataoutputstream to write data, you must specify the data output format.
Import Java. io. dataoutputstream; import Java. io. file; import Java. io. fileoutputstream; public class dataoutputstreamdemo {public static void main (string ARGs []) throws exception {// All exceptions throw dataoutputstream dos = NULL; // declare the data output stream object file F = new file ("D:" + file. separator + "order.txt"); // file storage path dos = new dataoutputstream (New fileoutputstream (f )); // instantiate the data output stream object string Names [] = {"shirt", "glove", "Scarf"}; // product name float Prices [] = {98.3f, 30.3f, 50.5f}; // commodity price int Nums [] = {3, 2, 1}; // commodity quantity for (INT I = 0; I <names. length; I ++) {// cyclically output dos. writechars (Names [I]); // write the DOS string. writechar ('\ t'); // write the DOS separator. writefloat (Prices [I]); // write price dos. writechar ('\ t'); // write the DOS separator. writeint (Nums [I]); // Number of writes dos. writechar ('\ n'); // line feed} dos. close (); // close the output stream }};
Import Java. io. datainputstream; import Java. io. file; import Java. io. fileinputstream; public class datainputstreamdemo {public static void main (string ARGs []) throws exception {// All exceptions throw datainputstream Dis = NULL; // declare the data input stream object file F = new file ("D:" + file. separator + "order.txt"); // file storage path Dis = new datainputstream (New fileinputstream (f); // instantiate the data input stream object string name = NULL; // receiving name float pric E = 0.0f; // receipt price int num = 0; // receipt quantity char temp [] = NULL; // receipt product name int Len = 0; // Save the number of read data char C = 0; // '\ u0000' try {While (true) {temp = new char [200]; // open space Len = 0; while (C = dis. readchar ())! = '\ T') {// receiving content temp [Len] = C; Len ++; // read Length plus 1} name = new string (temp, 0, len); // convert the character array to string price = dis. readfloat (); // read the price dis. readchar (); // read \ t num = dis. readint (); // read int dis. readchar (); // read \ n system. out. printf ("Name: % s; Price: % 5.2f; quantity: % d \ n", name, price, num) ;}} catch (exception e) {} dis. close ();}};