Java IO与NIO对象序列化写入文件效力粗略斗劲
添加时间:2013-6-16 点击量:
粗略地测试了一下io与nio之间对于对象序列化读入文件的效力,不过我nio的写法可能有失偏颇,首要我没找到通道对象读写的办法。代码如下:
1 import java.io.ByteArrayOutputStream;
2 import java.io.FileNotFoundException;
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5 import java.io.ObjectOutputStream;
6 import java.nio.ByteBuffer;
7 import java.nio.channels.FileChannel;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13
14 public class OtoBtoF {
15 public static void writeObject() {
16 long s = System.currentTimeMillis();
17 try {
18
19 List<Map<String,Object>> table=new ArrayList<Map<String,Object>>();
20 for(int i=0;i<1;i++){
21 Map<String,Object> map = new HashMap<String,Object>();
22 Double d1=Math.random();
23 Double d2=Math.random();
24 Double d3=Math.random();
25 map.put(name, d1.toString());
26 map.put(id, d2.toString());
27 map.put(age, d3);
28 table.add(map);
29 }
30
31 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
32 //BufferedOutputStream bo=new BufferedOutputStream(outStream);
33 ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);
34
35 objectOutputStream.writeObject(table);
36 objectOutputStream.close();
37 FileOutputStream fout=new FileOutputStream(test.txt);
38 FileChannel cha=fout.getChannel();
39 ByteBuffer bb=ByteBuffer.wrap(outStream.toByteArray());
40 cha.write(bb);
41
42 outStream.close();
43 System.out.println(successful+(System.currentTimeMillis()-s));
44 } catch (FileNotFoundException e) {
45 e.printStackTrace();
46 } catch (IOException e) {
47 e.printStackTrace();
48 }
49 }
50
51 public static void main(String[] args) {
52 // TODO Auto-generated method stub
53 OtoBtoF.writeObject();
54 System.out.println();
55 }
56 }
NIO
1 import java.io.BufferedInputStream;
2 import java.io.BufferedOutputStream;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14
15 public class OtoF {
16 public static void writeObject() {
17 try {
18 long s = System.currentTimeMillis();
19 List<Map<String,Object>> table=new ArrayList<Map<String,Object>>();
20 for(int i=0;i<1;i++){
21 Map<String,Object> map = new HashMap<String,Object>();
22 Double d1=Math.random();
23 Double d2=Math.random();
24 Double d3=Math.random();
25 map.put(name, d1.toString());
26 map.put(id, d2.toString());
27 map.put(age, d3);
28 table.add(map);
29 }
30
31 FileOutputStream outStream = new FileOutputStream(test.txt);
32 BufferedOutputStream bo=new BufferedOutputStream(outStream);
33 ObjectOutputStream objectOutputStream = new ObjectOutputStream(bo);
34
35 objectOutputStream.writeObject(table);
36 objectOutputStream.close();
37 bo.close();
38 outStream.close();
39 System.out.println(successful+(System.currentTimeMillis()-s));
40 } catch (FileNotFoundException e) {
41 e.printStackTrace();
42 } catch (IOException e) {
43 e.printStackTrace();
44 }
45 }
46
47 public static void main(String[] args) {
48 // TODO Auto-generated method stub
49 OtoF.writeObject();
50 System.out.println();
51 }
52 }
IO
经由过程调剂for轮回的次数来批改凑集的大小来测试,测试成果如下(名称后是均匀时候):
时候读书,不要想着实用,更不要有功利心。读书只为了自身的修养。邂逅一本好书如同邂逅一位知己,邂逅一个完美之人。有时心生敬意,有时怦然心动。仿佛你心底埋藏多年的话,作者替你说了出来,你们在时光深处倾心相遇的一瞬间,情投意合,心旷神怡。
粗略地测试了一下io与nio之间对于对象序列化读入文件的效力,不过我nio的写法可能有失偏颇,首要我没找到通道对象读写的办法。代码如下:
1 import java.io.ByteArrayOutputStream;
2 import java.io.FileNotFoundException;
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5 import java.io.ObjectOutputStream;
6 import java.nio.ByteBuffer;
7 import java.nio.channels.FileChannel;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13
14 public class OtoBtoF {
15 public static void writeObject() {
16 long s = System.currentTimeMillis();
17 try {
18
19 List<Map<String,Object>> table=new ArrayList<Map<String,Object>>();
20 for(int i=0;i<1;i++){
21 Map<String,Object> map = new HashMap<String,Object>();
22 Double d1=Math.random();
23 Double d2=Math.random();
24 Double d3=Math.random();
25 map.put(name, d1.toString());
26 map.put(id, d2.toString());
27 map.put(age, d3);
28 table.add(map);
29 }
30
31 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
32 //BufferedOutputStream bo=new BufferedOutputStream(outStream);
33 ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);
34
35 objectOutputStream.writeObject(table);
36 objectOutputStream.close();
37 FileOutputStream fout=new FileOutputStream(test.txt);
38 FileChannel cha=fout.getChannel();
39 ByteBuffer bb=ByteBuffer.wrap(outStream.toByteArray());
40 cha.write(bb);
41
42 outStream.close();
43 System.out.println(successful+(System.currentTimeMillis()-s));
44 } catch (FileNotFoundException e) {
45 e.printStackTrace();
46 } catch (IOException e) {
47 e.printStackTrace();
48 }
49 }
50
51 public static void main(String[] args) {
52 // TODO Auto-generated method stub
53 OtoBtoF.writeObject();
54 System.out.println();
55 }
56 }
NIO
1 import java.io.BufferedInputStream;
2 import java.io.BufferedOutputStream;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14
15 public class OtoF {
16 public static void writeObject() {
17 try {
18 long s = System.currentTimeMillis();
19 List<Map<String,Object>> table=new ArrayList<Map<String,Object>>();
20 for(int i=0;i<1;i++){
21 Map<String,Object> map = new HashMap<String,Object>();
22 Double d1=Math.random();
23 Double d2=Math.random();
24 Double d3=Math.random();
25 map.put(name, d1.toString());
26 map.put(id, d2.toString());
27 map.put(age, d3);
28 table.add(map);
29 }
30
31 FileOutputStream outStream = new FileOutputStream(test.txt);
32 BufferedOutputStream bo=new BufferedOutputStream(outStream);
33 ObjectOutputStream objectOutputStream = new ObjectOutputStream(bo);
34
35 objectOutputStream.writeObject(table);
36 objectOutputStream.close();
37 bo.close();
38 outStream.close();
39 System.out.println(successful+(System.currentTimeMillis()-s));
40 } catch (FileNotFoundException e) {
41 e.printStackTrace();
42 } catch (IOException e) {
43 e.printStackTrace();
44 }
45 }
46
47 public static void main(String[] args) {
48 // TODO Auto-generated method stub
49 OtoF.writeObject();
50 System.out.println();
51 }
52 }
IO
经由过程调剂for轮回的次数来批改凑集的大小来测试,测试成果如下(名称后是均匀时候):
时候读书,不要想着实用,更不要有功利心。读书只为了自身的修养。邂逅一本好书如同邂逅一位知己,邂逅一个完美之人。有时心生敬意,有时怦然心动。仿佛你心底埋藏多年的话,作者替你说了出来,你们在时光深处倾心相遇的一瞬间,情投意合,心旷神怡。 |