Java把double数据写入文件中

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 ;
	}
}

时间: 2024-07-30 10:19:46

Java把double数据写入文件中的相关文章

PHP中Post和Get获取数据写入文件中

有时候Post或者Get传过来的数据我们不知道它是个什么样的形式,它可能是JSON格式或者就是简单提交过来的数据,这时候我们可以把他写入到文本中,就可以看到传过来的数据是什么格式了. $val = ""; $currentDateTime = date('YmdHis',time()); $currentDate = date('Ymd',time()); $fileName = "ioslog/".$currentDate;//文件名称 @$data = fope

[转]java将字符串写入文件中

Java代码   import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.io.RandomAccessFile; publi

Redis 中文入库成功,读取数据写入文件乱码问题

最近需要用到redis ,但是在编码这个问题上,纠结了很久. 需求 : 每天一个进程将中文文件入库到redis中(不定时更新) ,另外几个进程读取redis中的信息 ,并处理数据结果. 使用的redis模块 : redis-py 问题 : 入库正常,读取数据成功,以GBK编码写入文件出现异常. 通过以下参数连接 redis : client  = redis.StrictRedis(host='localhost', port=6379, db=0, password="***") 从

【C#/WPF】保存BitmapImage数据到文件中

原文:[C#/WPF]保存BitmapImage数据到文件中 参考: http://stackoverflow.com/questions/35804375/how-do-i-save-a-bitmapimage-from-memory-into-a-file-in-wpf-c /// <summary> /// 把内存里的BitmapImage数据保存到硬盘中 /// </summary> /// <param name="bitmapImage">

接口测试-将数据写入csv中

将接口测试的返回值,写入到表格中 # 将testresult的数据写入csv中# w为覆盖写,a为追加写 import csv testresult = {"接口名称":"登录接口","测试结果":"测试通过"}file = open("test2.csv","w")for key,value,in testresult.items(): print(key,value) file.wr

java实现写大量数据到文件中

生成.txt文件 生成.csv文件 生成.xls文件 package com.test; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.text.NumberFormat

C语言中链表任意位置怎么插入数据?然后写入文件中?

链表插入示意图:(图是个人所画)因为链表指针指来指去,难以理解,所以辅助画图更加方便. 定义的结构体: struct student { char ID[11]; //学生学号 char name[20]; //学生姓名 struct student *next; //next 指针 指向 struct student 类型的变量 }stu; 看我写的代码,代码中有详细解释: /*************** 函数功能: 插入出勤学生 返回:指向链表表头的指针 /***************/

Java千万级别数据生成文件思路和优化

一年前写过一个百万级别数据库数据生成配置xml文件的程序,程序目的是用来把数据库里面的数据生成xml文件.程序可以配置多少文件生成到一个文件中去. 程序刚开始设计的时候说的是最多百万级别数据,最多50W数据生成到一个xml文件里面去,所以在做测试的时候自己也只是造了100W的数据并没有做过多数据量的测试,然后问题就来了....由于程序使用的局点数据量巨大,需要生成xml文件的客户资料接近千万级别的程度,而现场对程序的配置大约是100W条数据生成一个xml文件里面去,程序在这样的大数据量下面偶尔会

打印流 -可将数据写入文件/可改变输出方向

打印流 java.lang.Object 继承者 java.io.OutputStream 继承者 java.io.FilterOutputStream 继承者 java.io.PrintStream 与其他输出流不同,PrintStream 永远不会抛出 IOException: 打印的所有字符都使用平台的默认字符编码转换为字节.有很多OutputStream的成员方法 构造方法: PrintStream(File file) 输出的目的地是一个文件: PrintStream(File fil