Java------file input, output

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.