Import java.io.*;p Ublic class Intfile {private String filename; Public intfile (String filename) {this.filename = filename; } public void WriteToFile () throws IOException//writes the Fibonacci sequence value to the specified file {fileoutputstream Fout = New FileOutputStream (This.filename); DataOutputStream dout = new DataOutputStream (fout); Short i=0,j=1; Do {dout.writeint (i); Writes an integer dout.writeint (j) to the output stream; i = (short) (I+J); j = (short) (I+J); } while (i>0); Dout.close (); Turn off Data Flow fout.close () first; Close file stream} public void ReadFromFile () throws IOException//reads an integer from the specified file {fileinputstream fin = new FileInputStream (This.filename); DataInputStream din = new DataInputStream (FIN); System.out.println (this.filename+ ":"); while (true) The input stream does not end when try {int i = Din.readint ();//Reads an integer from the input stream System.out.print (i+ ""); } catch (Eofexception e) {break; } din.close (); Turn off Data Flow fin.close () first; Close file stream again} public static void Main (String args[]) throws IOException {Intfile afile = new Intfile ("Fibint File.dat "); Afile.writetofile (); Afile.readfromfile (); The results of the}}/* program run as follows: fibintfile.dat:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 1094 6 17711 28657 The programming instructions are as follows: 1, the ReadInt () method throws a Eofexception exception when it reaches the end of the input stream. */
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java------file input, output