首页 / JAVA / Java把double数据写入文件中
Java把double数据写入文件中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java把double数据写入文件中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2902字,纯文字阅读大概需要5分钟。
内容图文
![Java把double数据写入文件中](/upload/InfoBanner/zyjiaocheng/1334/76de6a7196024bb4bfde3a0e638adb07.jpg)
public class ReadOrWriteObject { private FileInputStream fileIns = null ;//文件输入流 private FileOutputStream fileOts = null;//文件输出流 private ObjectInputStream objectIns = null ;//对象输入流 private ObjectOutputStream objectOts = null ;//对象输出流 private String fileName = null;//待处理的文件名 public static Boolean FileRead = true ; public static Boolean FileWrite = false ; /** * * @param fileName:文件名 */ public ReadOrWriteObject(String fileName){ this.fileName = fileName ; } /** * 创建对象之后,首先必须以某种方式打开文件,如读的方式,或者写的方式 * @param fileName: The name of the file. * @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 ; } /** * 关闭文件流,对象流 * @return 成功关闭返回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 (IOException e) { e.printStackTrace(); } } return true ; } /** * 刷新缓冲区 */ public void flush(){ try { this.objectOts.flush(); } catch (IOException e) { e.printStackTrace(); } } /** * * @param d 需要写入文件的double数据 * @return 写入成功则返回true,否则返回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 ; } } /** * 读取double型数据 * @return 返回无穷小为false */ public double readDouble(){ double d=0; if(this.objectIns!=null){ try { d = this.objectIns.readDouble();//读取到文件末尾,被EOFException捕获 } 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) { //写入Double数据 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(); //"-----分割线-----" //读取Double数据 double d =0 ; rOd.openFile(ReadOrWriteObject.FileRead); while(true){ d = rOd.readDouble() ; if(d == Double.MIN_NORMAL){//读取文件的终止符,双精度最小值,在文件中已经关闭了相关的流 break; } else{ System.out.println(d); } } rOd.closeFile(); rOd = null ; } }
原文:http://blog.csdn.net/u014600432/article/details/42150729
内容总结
以上是互联网集市为您收集整理的Java把double数据写入文件中全部内容,希望文章能够帮你解决Java把double数据写入文件中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。