shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.walker.infrastructure.utils;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
public class ObjectStreamUtils {
 
    private static final Logger logger = LoggerFactory.getLogger(ObjectStreamUtils.class);
    
    /**
     * 把对象写入文件
     * @param filePath
     * @param obj
     * @date 2012-7-6
     */
    public static void writeObjectToFile(String filePath, Object obj){
        
        File file = new File(filePath);
        ObjectOutputStream oos = null;
        FileOutputStream fos = null;
        
        try {
            fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            logger.error("saveObjectToFile():File not found! filePath = " + filePath);
            throw new com.walker.infrastructure.core.FileNotFoundException(e);
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            logger.error("File save error!");
            e.printStackTrace();
            
        } finally {
            try {
                if(oos != null){
                    oos.close();
                }
                if(fos != null){
                    fos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 从文件读出对象,完成后把文件删除
     * @param filePath
     * @return
     * @date 2012-7-6
     */
    public static Object readObjectFromFile(String filePath){
        
        File file = new File(filePath);
        
        if(!file.exists()){
            return null;
        }
        ObjectInputStream ois = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            file.delete();
            return ois.readObject();
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            logger.warn("File not found in readObject!");
            return null;
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        } catch (ClassNotFoundException ex){
            
        } finally {
            try {
                if(ois != null){
                    ois.close();
                }
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
}