CreatFile.java
6.65 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.sitech.ismp.util;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.*;
/**
* Created with IntelliJ IDEA.
* User: ZhouYou
* Date: 15-6-26
* Time: 下午2:30
* To change this template use File | Settings | File Templates.
*/
public class CreatFile<T extends Object> {
private Logger logger = Logger.getLogger("CreatFile");
/**
* 创建一个新的文件
* @param lujing
*/
public void creatFile(String lujing){
File file = new File(lujing);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
System.out.println("-->文件创建成功:"+ lujing);
} catch (IOException e) {
e.printStackTrace();
}
}
/*public static void main(String[] args){
*//*String lujing = "d:\\test\\ss\\ss.txt";
File file = new File(lujing);
CreatFile cf = new CreatFile();
*//**//* file.delete();
CreatFile cf = new CreatFile();
cf.creatFile(lujing);
cf.fileWrite(file,"zhouyou");*//**//*
System.out.println( cf.fileRead(file)) ;*//*
*//* String lujing = "d:\\test\\ss\\ss.txt";
CreatFile cf = new CreatFile();
cf.creatFile(lujing);*//*
*//* SupervisorBean sb2 = new SupervisorBean();
sb2.setHost("lisi");
sb2.setId("456");
SupervisorBean sb3 = new SupervisorBean();
sb3.setHost("wangwu");
sb3.setId("789");
SupervisorBean sb4 = new SupervisorBean();
sb4.setHost("mm");
sb4.setId("444");
SupervisorBean sb = SupervisorBean.getInstance();
sb.setHost("rh28");
sb.setId("123");
sb.getSupervisorBeanM().put("lisi",sb2) ;
sb.getSupervisorBeanM().put("wangwu",sb3) ;
sb.getSupervisorBeanM().put("mm",sb4) ;
String str = "d:\\test\\ss\\sspe.txt";
CreatFile<SupervisorBean> cf = new CreatFile<SupervisorBean>();
cf.serializeObj(sb,str);*//*
*//* cf.creatFile(str);*//*
CreatFile<SupervisorBean> cf = new CreatFile<SupervisorBean>();
String str = "d:\\test\\ss\\sspe.txt";
SupervisorBean sbb = (SupervisorBean)cf.deSerializeObj(str);
System.out.println("-->"+sbb.getSupervisorBeanM().get("lisi").getHost());
System.out.println("-->"+sbb.getSupervisorBeanM().get("wangwu").getHost());
System.out.println("-->"+sbb.getSupervisorBeanM().get("mm").getHost());
System.out.println("123");
*//*try{
FileOutputStream fs = new FileOutputStream("d:\\test\\ss\\sspe.txt");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(sb);
os.close();
}catch(Exception e){
}*//*
*//* try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
new File("d:\\test\\ss\\sspe.txt")));
SupervisorBean person = (SupervisorBean) ois.readObject();
System.out.println("Person对象反序列化成功!"+person.getSupervisorBeanM().get("lisi").getId());
System.out.println("Person对象反序列化成功!"+person.getSupervisorBeanM().get("wangwu").getId());
}catch(Exception e){
System.out.println("error");
}*//*
}*/
/**
* 序列化对象
* @param obj
*/
public void serializeObj(Object obj,String str){
try{
FileOutputStream fs = new FileOutputStream(str);
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(obj);
os.close();
System.out.println("对象序列化成功!");
}catch(Exception e){
System.out.println("对象序列化error");
}
}
/**
* 反序列化对象
* @param str
* @return
*/
public Object deSerializeObj(String str){
Object obj = null;
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
new File(str)));
obj = ois.readObject();
System.out.println("对象反序列化成功!");
}catch(Exception e){
System.out.println("对象反序列化error");
}
return obj;
}
/**
* 往文件中写内容
* @param file
* @param content
*/
public void fileWrite(File file,String content){
try {
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读文件中的内容
* @param file
* @return
*/
public String fileRead(File file){
String string = "";
try {
FileReader fr = new FileReader(file);
BufferedReader bReader = new BufferedReader(fr);
string = bReader.readLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return string;
}
public void deleteFile(String str){
File file = new File(str);
file.delete();
System.out.println("文件删除成功:"+str);
}
/*
将java对象解析为json串
*/
public String javaToJson(Object pp2)throws IOException {
ObjectMapper mapper = new ObjectMapper();
StringWriter sw = new StringWriter();
JsonGenerator gen = null;
try {
gen = new JsonFactory().createJsonGenerator(sw);
mapper.writeValue(gen, pp2);
}catch (Exception e){
e.printStackTrace();
}finally {
gen.close();
}
System.out.println("转换后json:"+sw.toString());
return sw.toString();
}
/*
将json解析为java对象
*/
public Object readJson2Entity(String json,Object tt) {
try {
ObjectMapper mapper = new ObjectMapper();
Object acc = (T)mapper.readValue(json, tt.getClass());
return acc;
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}