头号知识点:
按住Ctrl键查看源代码时,点击Attach Source,在弹出的选择框中External Location,选在Jar包中的带Source文件,切记!
Properties类:
Properties 类表示了一个持久的属性集!Properties可保存在流中或从流中加载,属性列表中每个键及其对应值都是一个字符串!
1;Hashtable的子类,map集合中的方法都可以用!
2;该集合没有泛型。键值都是字符串!
3;它是一个可以持久化的属性集,键值可以存储到集合中,也可以存储到持久化的设备(硬盘,光盘)上,键值的来源也可以是持久化的设备!
4;有和流技术相结合的方法。
//可以套进Buffered高效流!
序列化流与反序列化流:
可以从流中读取和保存对象!
对象序列化流ObjectOutputStream:
ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream!可以使用 ObjectInputStream 读取重构对象!通过在流中使用文件可以实现对象的持久存储!
注意:只能将支持 java.io.Serializable 接口的对象写入流中!
对象反序列化流ObjectInputStream:
ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化!支持 java.io.Serializable接口的对象才能从流读取!
序列化接口:
当一个对象要能被序列化,这个对象所属的类必须实现Serializable接口!否则会发生异常NotSerializableException异常!
//这个类只有一个空的Serializable方法,目的是确立一个标识!
当成员属性被Static修饰的时候,不能被序列化,transient可以实现同样的效果,切更专业!
打印流:
字节打印流:PrintStream
字符打印流:PrintWriter
只有目的没有数据源,不会跑出IO异常!
开启文件自动刷新写入功能:
public PrintWriter(OutputStream out, boolean autoFlush)
public PrintWriter(Writer out, boolean autoFlush)
//true开启autoFlush自动刷新功能!
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.properties; import java.util.Properties; import java.util.Set; public class Demo01 {public static void main(String[] args) {Properties pro=new Properties();//插入数据:pro.setProperty("Driver","com.mysql.jdbc.driver");pro.setProperty("username","root");pro.setProperty("password","123456");//取数据,获取键集,用的是Set所以是无序的:Set<String> set=pro.stringPropertyNames();for(String Str:set){System.out.println(Str+"……"+pro.getProperty(Str));}} }package com.oracle.properties; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import java.util.Set; public class Demo02 {public static void main(String[] args) throws IOException {//从Properties文件中读取键值对:Properties prop=new Properties();//明确数据源:FileInputStream fis=new FileInputStream("F:\\IOTest\\PropertiesTest.properties");//从IO流中读取键值对: prop.load(fis);//关流(没报黄线可以不用关): fis.close();//遍历prop:Set<String> set=prop.stringPropertyNames();for(String Str:set){System.out.println(Str+"……"+prop.getProperty(Str));}} }package com.oracle.properties; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class Demo03 {public static void main(String[] args) throws IOException {//通过Properties的Store方法将键值对写入properties文件://明确目的地:FileOutputStream fos=new FileOutputStream("F:\\IOTest\\PropertiesTest.properties");//创建Properties对象:Properties prop=new Properties();prop.setProperty("name","神王盖伦");prop.setProperty("age","84");prop.store(fos,"You are Pig");} }package com.oracle.properties; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Demo04 {public static void main(String[] args) throws IOException {//创建对象:Person peo=new Person("张三",18);//明确目的地:FileOutputStream fos=new FileOutputStream("F:\\IOTest\\Person.txt");//创建序列化流:ObjectOutputStream oos=new ObjectOutputStream(fos);//写入对象: oos.writeObject(peo);//关流(只关外层流): oos.close();} }package com.oracle.properties; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class Demo05 {public static void main(String[] args) throws IOException, ClassNotFoundException {//明确数据源:FileInputStream fis=new FileInputStream("F:\\IOTest\\Person.txt");//创建反序列化对象:ObjectInputStream ois=new ObjectInputStream(fis);//这个反序列化对象的readObject方法返回一个Object对象:Object obj=ois.readObject();//创建Person对象接收向下转型的Object对象:Person getPeo=(Person)obj;//由于重写了toString方法,直接打印该对象: System.out.println(getPeo);//关流: ois.close();} }package com.oracle.properties; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; public class Demo06 {public static void main(String[] args) throws IOException {//明确数据目的:FileOutputStream fos=new FileOutputStream("F:\\IOTest\\GetYou.txt");PrintStream ps=new PrintStream(fos);ps.print("张三");ps.println("nihao");ps.println(true);//释放资源: ps.close();} }package com.oracle.properties; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Demo07 {public static void main(String[] args) throws IOException {//打印流复制://明确数据源:FileReader fr=new FileReader("F:\\IOTest\\GetYou.txt");BufferedReader br=new BufferedReader(fr);//新建文件:File file=new File("F:\\IOTest\\GetYa.txt");file.createNewFile();//明确数据目的:FileWriter fw=new FileWriter("F:\\IOTest\\GetYa.txt");PrintWriter pw=new PrintWriter(fw,true);//遍历br文件:String line=null;while((line=br.readLine())!=null){fw.write(line);}//释放资源: br.close();pw.close();} }package com.oracle.properties; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; public class Demo08 {public static void main(String[] args) {method01();}public static void method01(){String name=FilenameUtils.getExtension("F:\\IOTest");System.out.println(name);String filename=FilenameUtils.getName("F:\\IOTest\\GetYa.txt");System.out.println(filename);boolean IsJava=FilenameUtils.isExtension("F:\\IOTest\\GetYa.txt",".java");System.out.println(IsJava);}public static void method02() throws IOException{//顶层还是流:FileUtils.copyDirectory(new File("F:\\IOTest"),new File("D:\\"));} }package com.oracle.properties; import java.io.Serializable; public class Person implements Serializable {private String name;//static修饰的属于类,不属于对象,在main方法之前的静态区的初值=0:private transient int age;//瞬态关键字,导致此属性不能被序列化//设置不变的反序列化号,这样修改类中成员变量的访问修饰符时,反序列化时不会受影响:private static final long serialVersionUID=38237489l;//有参构造:public Person(String name, int age) {super();this.name = name;this.age = age;}//无参构造:public Person() {}//GET+SET方法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;}//重写toString方法:public String toString() {return "Person [name=" + name + ", age=" + age + "]";}//不想被序列化的属性,用static修饰,或者用专业的transient! }