Properties类
Properties类介绍
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
特点:
1、Hashtable的子类,map集合中的方法都可以用。
2、该集合没有泛型。键值都是字符串。
3、它是一个可以持久化的属性集。键值可以存储到集合中,也可以存储到持久化的设备(硬盘、U盘、光盘)上。键值的来源也可以是持久化的设备。
4、有和流技术相结合的方法。
代码演示:
/* * * Properties集合,它是唯一一个能与IO流交互的集合 * * 需求:向Properties集合中添加元素,并遍历 * * 方法: * public Object setProperty(String key, String value)调用 Hashtable 的方法 put。 * public Set<String> stringPropertyNames()返回此属性列表中的键集, * public String getProperty(String key)用指定的键在此属性列表中搜索属性 */ package com.oracle.demo01; import java.util.Properties; import java.util.Set; public class PropertiesDemo { public static void main(String[] args) { Properties pro = new Properties(); pro.setProperty("a", "1"); pro.setProperty("b", "2"); pro.setProperty("c", "3"); pro.setProperty("d", "4"); System.out.println(pro.getProperty("d")); Set<String> s = pro.stringPropertyNames(); for (String str : s) { // 通过遍历键获取对应的值 String key = str; String value = pro.getProperty(key); System.out.println(key + "..." + value); } } }
将集合中内容存储到文件
练习:使用Properties集合,完成把集合内容存储到IO流所对应文件中的操作
public static void output() throws IOException { //1,创建Properties集合 Properties pro=new Properties(); //2,添加元素到集合 pro.setProperty("吴彦祖一号", "吃外卖"); pro.setProperty("吴彦祖二号", "吃大餐"); pro.setProperty("吴彦祖三号", "吃零食"); //3,创建流 FileWriter fw=new FileWriter("d:\test\demo.properties"); //4,把集合中的数据存储到流所对应的文件中 pro.store(fw, ""); //5,关闭流 fw.close(); }
读取文件中的数据,并保存到集合
从属性集文件prop.properties 中取出数据,保存到集合中
public static void input() throws IOException { //1,创建集合 Properties pro=new Properties(); //2,创建流对象 FileReader fr=new FileReader("d:\test\demo.properties"); //3,把流所对应文件中的数据 读取到集合中 pro.load(fr); //关闭流 fr.close(); Set<String> set=pro.stringPropertyNames(); Iterator<String> it=set.iterator(); while(it.hasNext()) { String key=it.next(); String value=pro.getProperty(key); System.out.println(key+"........"+value); //在文件中#表示注释 } }
序列化流与反序列化流
用于从流中读取对象的操作流 ObjectInputStream,称为反序列化流。
用于向流中写入对象的操作流 ObjectOutputStream,称为序列化流。
特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。
如图所示,
对象序列化流ObjectOutputStream
注意:只能将支持 java.io.Serializable 接口的对象写入流中
将一个对象存储到持久化(硬盘)的设备上,代码演示:
package com.oracle.demo012; import java.io.Serializable; public class Person implements Serializable{ private String name; //静态成员变量不能序列化,因为静态不属于对象,只属于自己的类,并且有默认值 private int age; //我不想让我的成员变量被序列化,也不想他变为静态的 //用TRANSIENT关键字去解决,瞬态关键字 private transient String sex; //作用:阻止成员变量序列化 //Serializable接口的含义:标记型接口(什么也没有的接口) //作用:对你的Person类做一个标记,只要有这个标记,就可以序列化 static final long serialVersionUid=852369L; public Person() { } public Person(String name, int age,String sex) { super(); this.name = name; this.age = age; this.sex=sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name+"+name+".........."+age+"..........."+sex; } }
测试类:
package com.oracle.demo012; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException { //output(); input(); } public static void output() throws IOException { //1,明确存储对象的文件。 FileOutputStream fos=new FileOutputStream("d:\test\Person.txt"); //2,给操作文件对象加入写入对象功能。 ObjectOutputStream oos=new ObjectOutputStream(fos); Person p=new Person("吴彦祖",20,"男"); //3,调用了写入对象的方法。 oos.writeObject(p); //4,关闭资源。 oos.close(); }
对象反序列化流ObjectInputStream
代码演示:
public class ObjectStreamDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { readObj();//对象的反序列化。 } public static void readObj() throws IOException, ClassNotFoundException { //1,定义流对象关联存储了对象文件。 FileInputStream fis = new FileInputStream("tempfile\obj.object"); //2,建立用于读取对象的功能对象。 ObjectInputStream ois = new ObjectInputStream(fis); Person obj = (Person)ois.readObject(); System.out.println(obj.toString()); } }
序列化接口
当一个对象要能被序列化,这个对象所属的类必须实现Serializable接口。否则会发生异常NotSerializableException异常。
同时当反序列化对象时,如果对象所属的class文件在序列化之后进行的修改,那么进行反序列化也会发生异常InvalidClassException。发生这个异常的原因如下:
该类的序列版本号与从流中读取的类描述符的版本号不匹配。
该类包含未知数据类型。
该类没有可访问的无参数构造方法。
Serializable标记接口。该接口给需要序列化的类,提供了一个序列版本号。serialVersionUID. 该版本号的目的在于验证序列化的对象和对应类是否版本匹配。
瞬态关键字transient
当一个类的对象需要被序列化时,某些属性不需要被序列化,这时不需要序列化的属性可以使用关键字transient修饰。只要被transient修饰了,序列化时这个属性就不会琲序列化了。
同时静态修饰也不会被序列化,因为序列化是把对象数据进行持久化存储,而静态的属于类加载时的数据,不会被序列化。
代码演示如下:
public class Person implements Serializable { /* * 给类显示声明一个序列版本号。 */ private static final long serialVersionUID = 1L; private static String name; private transient/*瞬态*/ int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
打印流
打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式.。
打印流根据流的分类:
字节打印流 PrintStream
字符打印流 PrintWriter
方法:
void print(String str): 输出任意类型的数据。
void println(String str): 输出任意类型的数据,自动写入换行操作。
代码演示:
package com.oracle.demo03; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; /* * 打印流: * 1.PrintWriter(自动刷新)(file,output,String,Writer)--灵活性更强 * 2.PrintStream(File,Output,String) * 这两个流中的方法一模一样 * 特点: * 1.该流不负责数据源,只负责数据目的 * 2.为其他输出流添加功能 * 3.永远不抛IOException * * */ public class PrintDemo { public static void main(String[] args) throws IOException { //method_3(); copy(); } public static void method_1() throws FileNotFoundException { File file=new File("d:\test\wenjian.txt"); PrintWriter pw=new PrintWriter(file); pw.println(100); pw.flush(); pw.write(100); pw.close(); } //输出语句如果是char[]数组的问题 public static void method_2() { int[] arr= {1}; char[] ch= {'a','b'}; } public static void method_3() throws FileNotFoundException { //打印流的自动刷新功能 //条件:1:输出数据目的必须是流对象 //2.必须调用的方法是println、printf、format其中一个 File file=new File("d:\test\wenjian.txt"); FileOutputStream fos=new FileOutputStream(file); PrintWriter pw=new PrintWriter(fos,true); pw.println("五一去哪玩"); pw.println("去哪玩"); pw.println("玩"); pw.close(); } //复制文本文件 public static void copy() throws IOException { //读取数据:BufferedReader+FileReader //写入目的地:PrintWriter+println 自动刷新功能 BufferedReader br=new BufferedReader(new FileReader("d:\test\wenjian.txt")); PrintWriter pw=new PrintWriter(new FileWriter("d:\test\a\boring.txt"),true); String line=null; while((line=br.readLine())!=null) { pw.println(line); } pw.close(); br.close(); } }
commons-IO
1.加入classpath的第三方jar包内的class文件才能在项目中使用
2.创建lib文件夹
3.将commons-io.jar拷贝到lib文件夹
4.右键点击commons-io.jar,Build Path→Add to Build Path
FilenameUtils
这个工具类是用来处理文件名(译者注:包含文件路径)的,他可以轻松解决不同操作系统文件名称规范不同的问题
常用方法:
getExtension(String path):获取文件的扩展名;
getName():获取文件名;
isExtension(String fileName,String ext):判断fileName是否是ext后缀名;
FileUtils
提供文件操作(移动文件,读取文件,检查文件是否存在等等)的方法。
常用方法:
readFileToString(File file):读取文件内容,并返回一个String;
writeStringToFile(File file,String content):将内容content写入到file中;
copyDirectoryToDirectory(File srcDir,File destDir);文件夹复制
copyFile(File srcFile,File destFile);文件夹复制
package com.oracle.demo03; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; public class FileUtileDemo { public static void main(String[] args) throws IOException { FileNmeUtilMethod(); } public static void FileNmeUtilMethod() throws IOException { FilenameUtils fun = new FilenameUtils(); // 获取扩展名 String name1 = fun.getExtension("d:\test\demo.txt"); // 获取文件名 String name2 = fun.getName("d:\test\demo.txt"); // 判断后缀名 boolean b = fun.isExtension("d:\test\demo.txt", "txt"); FileUtils fu = new FileUtils(); // 读取文件内容 String content = fu.readFileToString(new File("d:\test\wenjian.txt")); System.out.println(content); // 将字符串写入文件 fu.writeStringToFile(new File("d:\test\demo.txt"), "今天下雨了"); // 复制文件夹 fu.copyDirectoryToDirectory(new File("d:\test"), new File("e:\users")); // 复制文件 fu.copyFile(new File("d:\test\366.gif"), new File("e:\users\bbb.gif")); } }