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。
相关推荐
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....
- 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. ...
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 ...
// Create a new SerialPort object with default settings. _serialPort = new SerialPort(); // Allow the user to set the appropriate properties. _serialPort.PortName = comboBoxPort.Text; _...
lblMessage.Text = "该用户不存在或用户ID输入错误,请检查后重新输入!"; } } catch (Exception ee) { Response.Write("[removed]alert('" + ee.Message.ToString() + "')[removed]"); } finally ...
// 将从Excel表格读取到的数据与配置文件筛选成可以插入到数据库的数据 public List, Object>> parseExcelData( List, String>> excelDataMap, Map, List<ExcelMap>> dbfield) { List, Object>> list = new ...
"*" 表示子元素可以出现多个,也可以不出现,(0到多) <!ELEMENT studinfo (age|brithday)> Enumerated:枚举类型,由“|”分隔的可能的子元素,在可能出现的子元素中只能有一个出现。 EMPTY:元素内容为空时...
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. ...
说明:本实例只是简单地使用了“异或”运算符计算两个整型数值以达到加密的目的,所以本实例只可以 对整型数值进行加密运算,并不适合其他数据的加密。 设 计过程 (1)打开Visual Studio 2008 开发环境,新建一个...
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 ...
我感觉大家可以在这个基础之上进行扩展,最少可以看看他的实现思路 源码分享给大家了 sufeinet.com即时通信_云骞.zip (3.25 MB, 下载次数: 1078) ReceiveResponsesHandler 类,这个主要是用来接收和维护长连接的 ...
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 ...
myTcpListener = new TcpListener(IPAddress.Parse(tbxFtpServerIp.Text), int.Parse(tbxFtpServerPort.Text)); // 开始监听传入的请求 myTcpListener.Start(); AddInfo("启动FTP服务成功!"); AddInfo("Ftp...
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对象表示方法】,它是一个轻量...
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对象表示方法】,它是一个轻量...
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 ...
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 ...
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 ...
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,...