Java学习 之 IO流(Properties、序列化与反序列化、打印流、commons-IO)

  1 /*
  2 
  3 Properties类表示一个持久的属性集,可以保存在流中或从流中加载,属性列表中每个键及其对应值都是一个字符串
  4 
  5 特点:
  6 
  7        1、Hashtable的子类,map集合中的方法都可以用
  8 
  9        2、该集合没有泛型,键值都是字符串
 10 
 11        3、是一个持久化的属性集,键值可以存储到集合中,也可以存储到持久化的设备(硬盘,u盘、光盘)上,键值的来源也可以是持久化的设备
 12 
 13 方法:
 14 
 15      void load(InputStream in)把指定流所对应的文件中的数据,读取出来,保存到Properties集合中
 16 
 17      void load(Reader reader)从输入字符流中读取属性列表
 18 
 19      void store(OutputStream out, String comments)把集合中的数据,保存到指定的流所对应的文件中,参数comments代表对描述的信息
 20 
 21     void store(Writer writer , String comments)将Properties表中的属性列表(键和值)写入输出字符
 22 
 23 */
 24 
 25 public class PropertiesDemo1{
 26 
 27    public static void main(String[] args){
 28 
 29     //创建集合对象
 30 
 31    Properties pro = new Properties();
 32 
 33    //添加元素到集合
 34 
 35   pro.setProperty("a" , "1");
 36 
 37   pro.setProperty("b" , "2");
 38 
 39   //遍历集合
 40 
 41  Set<String> keys = pro.stringPropertyNames();
 42 
 43  for(Strng key : keys){
 44 
 45  //通过键,找值
 46 
 47  String value = pro.getProperty(key);
 48 
 49  System.out.println(key +"=="+value);
 50 
 51    }
 52 
 53   }
 54 
 55 }
 56 
 57 /*
 58 
 59 将集合中内容存储到文件
 60 
 61 */
 62 
 63 public class PropertiesDemo2{
 64 
 65    public static void main(String[] args) throws IOException{
 66 
 67       //创建Properties集合
 68 
 69     Properties pro = new Properties();
 70 
 71    //添加元素到集合
 72 
 73   pro.setProperty("name" , "张三");
 74 
 75   pro.setProperty("names" , "李四");
 76 
 77   //创建流
 78 
 79   FileWriter fw = new FileWriter("pro.properties");
 80 
 81   //把集合中的数据存储到流对应的文件中
 82 
 83   pro.store(fw , "");
 84 
 85  //关闭流
 86 
 87  fw.close();
 88 
 89    }
 90 
 91 }
 92 /*
 93 
 94 读取文件中的数据,并保存到集合
 95 
 96 */
 97 
 98 public class PropertiesDemo3{
 99 
100    public static void main(String[] args) throws IOException{
101 
102     //创建集合
103 
104    Properties pro = new Properties();
105 
106   //创建流对象
107 
108   FileInputStream fis = new FileInputStream("pro.porperties");
109 
110   //把流所对应的文件中的数据,读取到集合中
111 
112   pro.load(fis);
113 
114   //关闭流
115 
116   fis.close();
117 
118  //显示集合中的数据
119 
120  System.out.println(pro);
121 
122  }
123 
124 }
125 
126 /*
127 
128 序列化和反序列化:
129 
130     用于从流中读取对象的操作流ObjectInputStream称为反序列化
131 
132     构造方法:ObjectInputSream(InputStream in)创建从指定InputStream读取的ObjectInputStream
133 
134     方法:Object readObject()从ObjectInputStream读取对象
135 
136     用于从流中写入对象的操作流ObjectOutputStream称为序列化
137     构造方法: ObjectOutputStream(OutputStream out)创建写入指定OutputStream的ObjectOutputStream
138 
139     方法: void writeObject(Object obj)将指定的对象写入ObjectOutputStream
140 
141 序列化接口:当一个对象要能被序列化,这个对象所属的类必须实现Serializable接口,否则会发生异常NotSerializableException异常
142 
143                    同时当反序列化对象时,如果对象所属的class文件在序列化之后进行的修改,那么进行反序列化也会发生异常InvalidClassException,发生异常的原因:
144 
145                    1、该类的序列版本号于从流中读取的类描述符的版本号不匹配
146 
147                    2、该类包含未知数据类型
148 
149                    3、该类没有可访问的无参数构造方法
150 
151 Serializable标记接口,该接口给需要序列化的类,提供了一个序列版本号,serialVersionUID,该版本号的目的在于验证序列化的对象和对应类是否版本匹配
152 
153 */
154 
155 public class ObjectStreamDemo{
156 
157    public static void main(String[] args) throws IOException{
158 
159         writeObj() //对象的序列化
160 
161    }
162 
163    public static writeObj() throws IOException{
164 
165       //明确存储对象的文件
166 
167      FileOutputStream fos = new FileOutputStream("temp\obj.object");
168 
169      //给操作文件对象加入写入对象功能
170 
171     ObjectOutputStream oos = new ObjectOutputStream(fos);
172 
173     //调用了写入对象的方法
174 
175    oos.writeObject(new Person("张三" , 20));
176 
177    //关闭资源
178 
179    oos.close();
180 
181    }
182 
183 }
184 
185 public class Person implements Serializable{
186 
187   //给类声明一个序列化版本号
188 
189    private static final long serivalVersionUID = 1L;
190 
191    private String name;
192 
193    private int age;
194 
195    public Person(){
196 
197      super();
198 
199    }
200 
201   public Person(String name , int age){
202 
203      super();
204 
205     this.name = name;
206 
207     this.age = age;
208 
209    public void setName(String name){
210 
211       this.name = name;
212 
213   }
214 
215   public String getName(){
216 
217      return name;
218 
219   }
220 
221   public void setAge(int age){
222 
223     this.age = age;
224 
225  }
226 
227  public int getAge(){
228 
229     return age;
230 
231  }
232 
233  @Override
234 
235  public String toString(){
236 
237    return "Person [name=" + name +" , age = " + age +"]";
238 
239  }
240 
241   }
242 
243 }
244 
245 public class ObjectStreamDemo2{
246 
247     public static void main(String[] args) throws IOException{
248 
249           readObj();
250 
251    }
252 
253    public staitc readObj() throws IOException , ClassNotFoundException{
254 
255      //定义流对象关联存储了对象文件
256 
257     FileInputStream fis = new FileInputStream("temp\obj.object");
258 
259    //建立用于读取对象的功能对象
260 
261    ObjectInputStream ois = new ObjectInputStream(fis);
262 
263    Person obj = (Person)ois.readObj();
264 
265    System.out.println(obj.toString());
266 
267    } 
268 
269 }
270 
271 /*
272 
273 瞬态关键字transient:
274 
275                            当一个类的对象需要被序列化时,某些属性不需要被序列化,不需要序列化的属性可以使用关键字transient修饰
276 
277                           静态修饰也不会被序列化,因为序列化是把对象数据进行持久化存储,而静态的属于类加载时的数据,不会被序列化
278 
279 */
280 
281 /*
282 
283 打印流:
284 
285          字节打印流: PrintStream
286 
287          字符打印流:PrintWriter
288 
289 方法:
290 
291        void print(String str)输出任意类型的数据
292 
293        void println(String str)输出任意类型的数据,自动写入换行操作
294 打印流完成数据自动刷新:
295 
296  构造方法:
297 
298         public PrintWriter(OutputStream out , boolean autoFlush)
299 
300          public PrintWriter(Writer out , boolean autoFlush)
301 
302 */
303 
304 public class PrintWriterDemo{
305 
306     public static void main(String[] args) throws IOException{
307 
308        //创建流
309 
310       PrintWriter fw = new PrintWriter("print.txt");
311 
312       //写数据
313 
314      for(int i =0 ; i < 5 ; i++){
315 
316        fw.println("hello");
317 
318     }
319 
320    //关闭流
321 
322   fw.close();
323 
324    }
325 
326 }
327 
328 public class PrintWriterDemo2{
329 
330     public static void main(String[] args) throws IOException{
331 
332       //创建流
333 
334      PrintWriter pw = new PrintWriter("print.txt" , true);
335 
336     //写数据
337 
338    for(int i = 0 ; i < 5 ; i++){
339 
340      pw.println("hello");
341 
342    }
343 
344   //关闭流
345 
346   pw.close();
347 
348     }
349 
350 }
351 
352 /*
353 
354 commons - io:
355 
356     FilenameUtils:这个工具类用来处理文件名的
357 
358     方法:
359 
360            getExtension(String path)获取文件的扩展名
361 
362            getName()获取文件名
363 
364            isExtension(String fileName , String ext) 判断fileName是否是ext后缀名
365 
366     FileUtils:提供文件操作的方法
367 
368     方法:
369 
370            readFileToString(File file) 读取文件内容,并返回一个String
371 
372            writeStringToFile(File file , String content)将内容content写入到file中
373 
374            copyDirectoryToDirectory(File srcDir , File destDir)文件夹复制
375 
376            copyFile(File srcFile , File destFile)文件夹复制
377 
378 
379 
380 */
原文地址:https://www.cnblogs.com/z97-/p/12761038.html