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