`

write object, read object 不一定比自己parse快速,只是简单,可以扩展好

    博客分类:
  • Java
 
阅读更多

write object, read object 不一定比自己parse快速,只是简单,可以扩展好。

如例子,文件“c:/temp/f.txt”是一个5万多行的文本文件。

 

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ReadObjWriteObj {

	private static String orginalFile = "c:/temp/f.txt";
	private static String objFile = "c:/temp/obj.txt"; 

	public static void main(String[] args) {
		Long start = System.currentTimeMillis();
		List<Object> list = (List<Object>) read(orginalFile);
		System.out.println(System.currentTimeMillis() - start);
		
		saveObjs(list, objFile);
		
		start = System.currentTimeMillis();
		list = readObjs(objFile);
		System.out.println(System.currentTimeMillis() - start);
	}

	public static List<Object> read(String fileName) {
		List<Object> list = new ArrayList<Object>();
		BufferedReader reader = null;
		String line;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
			while ((line = reader.readLine()) != null) {
				list.add(line);
			}
		} catch (Exception e) {
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static void saveObjs(List<Object> list, String fileName) {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(fileName));
			for(Object o : list) {
				oos.writeObject(o);
			}
		} catch (Exception e) {
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static List<Object> readObjs(String fileName) {
		List<Object> list = new ArrayList<Object>();
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(fileName));
			Object o;
			while((o=ois.readObject()) != null) {
				list.add(o);
			}
		} catch (Exception e) {
		} finally {
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

}

 

 

运行结果:

68
756

parse使用的68ms,read对象需要756ms。

分享到:
评论

相关推荐

    delphi superobject

    It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999....

    superobject-master.zip

    - It is easy for humans to read and write. - It is easy for machines to parse and generate. - It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December ...

    前台的转换

    It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. ...

    【官方源码】HtmlAgilityPack V1.5.1

    This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library ...

    Epson C#编程源码

    // Create a new SerialPort object with default settings. _serialPort = new SerialPort(); // Allow the user to set the appropriate properties. _serialPort.PortName = comboBoxPort.Text; _...

    在线报名系统 net

    lblMessage.Text = "该用户不存在或用户ID输入错误,请检查后重新输入!"; } } catch (Exception ee) { Response.Write("[removed]alert&#40;'" + ee.Message.ToString(&#41; + "')[removed]"); } finally ...

    Excel POI读取封装(文件+示范代码)

    // 将从Excel表格读取到的数据与配置文件筛选成可以插入到数据库的数据 public List, Object&gt;&gt; parseExcelData( List, String&gt;&gt; excelDataMap, Map, List&lt;ExcelMap&gt;&gt; dbfield) { List, Object&gt;&gt; list = new ...

    xml入门教程/xml入门教程

    "*" 表示子元素可以出现多个,也可以不出现,(0到多) &lt;!ELEMENT studinfo (age|brithday)&gt; Enumerated:枚举类型,由“|”分隔的可能的子元素,在可能出现的子元素中只能有一个出现。 EMPTY:元素内容为空时...

    superxmlparser xml json 解析

    It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. ...

    c# 加密和解密相关代码

    说明:本实例只是简单地使用了“异或”运算符计算两个整型数值以达到加密的目的,所以本实例只可以 对整型数值进行加密运算,并不适合其他数据的加密。 设 计过程 (1)打开Visual Studio 2008 开发环境,新建一个...

    Html Agility Pack 1.4.6

    This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library ...

    C#网页版+客户端版聊天软件

    我感觉大家可以在这个基础之上进行扩展,最少可以看看他的实现思路 源码分享给大家了 sufeinet.com即时通信_云骞.zip (3.25 MB, 下载次数: 1078) ReceiveResponsesHandler 类,这个主要是用来接收和维护长连接的 ...

    AJAX and PHP.pdf

    always know how to parse them. Also, not everyone likes AJAX. While some are developing enterprise architectures using JavaScript, others prefer not to use it at all. When the hype is over, most ...

    FTP服务器 C#

    myTcpListener = new TcpListener(IPAddress.Parse(tbxFtpServerIp.Text), int.Parse(tbxFtpServerPort.Text)); // 开始监听传入的请求 myTcpListener.Start(); AddInfo("启动FTP服务成功!"); AddInfo("Ftp...

    C#中实现Json序列化与反序列化的几种方式

    It is easy for humans to read and write and easy for machines to parse and generate. JSON is a text format that is completely language independent. 翻译:Json【javascript对象表示方法】,它是一个轻量...

    JSON键值对序列化和反序列化解析

    It is easy for humans to read and write and easy for machines to parse and generate. JSON is a text format that is completely language independent. 翻译:Json【javascript对象表示方法】,它是一个轻量...

    DbfDotNet_version_1.0_Source

    In Dbf.Net you don't have to write any code, if you don't want to wait for the garbage collector to collect your individual you can call SaveChanges. Dbf.Net ADO.Net Collapse Copy Codevoid ...

    acpi控制笔记本风扇转速

    memory, instead of performing a bytewise read. (Region must be of type SystemMemory, see below.) Fixed the Load ASL operator for the case where the source operand is a region field. A buffer object ...

    python3.6.5参考手册 chm

    urllib.parse mailbox turtledemo Multi-threading Optimizations Unicode Codecs Documentation IDLE Code Repository Build and C API Changes Porting to Python 3.2 What’s New In Python 3.1 PEP ...

    PHP Web 2.0 Mashup Projects.pdf

    XML-based parsers to parse the three formats, we have used parsers from PEAR, one for each of the three formats. Using these PEAR packages, we create an object-oriented abstraction of these formats,...

Global site tag (gtag.js) - Google Analytics