Java writes double data to a file

Source: Internet
Author: User

public class Readorwriteobject {private FileInputStream fileins = null;//file Input stream private FileOutputStream fileots = null;/ /file output stream private ObjectInputStream objectins = null;//object Input stream private ObjectOutputStream objectots = null;//object output stream private Stri ng filename = null;//The file name to be processed public static Boolean FileRead = True;p ublic static Boolean FileWrite = FALSE;/** * * @para M filename: filename */public readorwriteobject (String filename) {this.filename = filename;} /** * After creating an object, you must first open the file in some way, such as the way you read it, or the way you write * @param filename:the name of the document. * @param bool:whether The file read or write. FileRead or FileWrite * @return */public Boolean openFile (boolean bool) {if (BOOL)//if bool is true,the file is read. {try {this.fileins = new FileInputStream (this.filename); this.objectins = new ObjectInputStream (fileins);} catch ( FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}} else{try {this.fileots = new FileOutputStream (this.filename); this.objectots = new ObjectoutputstreaM (this.fileots);} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}} return true;} /** * Close file stream, object Flow * @return successfully closed returns true */public Boolean CloseFile () {if (null!=this.fileins) {try {this.objectIns.close (); This.fileIns.close (); objectins = Null;fileins = null;} catch (IOException e) {e.printstacktrace ();}} if (null! = This.fileots) {try {this.objectOts.close (); This.fileOts.close (); objectots = null; fileots = null;} catch (Ioex Ception e) {e.printstacktrace ();}} return true;} /** * Flush buffer */public void flush () {try {This.objectOts.flush ()} catch (IOException e) {e.printstacktrace ()}} /** * * @param d The double data that needs to be written to the file * @return The Write succeeds returns true, otherwise it returns false */public Boolean writedouble (double D) {if (this.objectots !=null) {try {this.objectOts.writeDouble (d);} catch (IOException e) {e.printstacktrace ();} return true;} Else{return false;}} /** * Read double type data * @return returns infinity to False */public double readdouble () {double d=0;if (this.objectins!=null) {try {d = this.obj EcTins.readdouble ();//read to end of file, eofexception capture} catch (Eofexception e) {this.closefile (); return double.min_normal;} catch (IOException e) {this.closefile (); E.printstacktrace ();} return D;} Else{return Double.min_normal;}} /** * Example * @param args */public static void main (string[] args) {//write double data String file = "F:\\1.txt"; Readorwriteobject rOd = new Readorwriteobject (file); Rod.openfile (readorwriteobject.filewrite); for (int i =0;i<1000 ; i++) rod.writedouble (i); Rod.flush (); Rod.closefile ();//"-----Split Line-----"//reads double data double d =0; Rod.openfile ( Readorwriteobject.fileread), while (true) {d = rod.readdouble (), if (d = = Double.min_normal) {//Read the file terminator, Double min, The associated stream break has been closed in the file;} Else{system.out.println (d);}} Rod.closefile (); rOd = null;}}

Java writes double data to a file

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.