Properties类java
特色:mysql
一、Hashtable的子类,map集合中的方法均可以用;sql
二、该集合没有泛型,键值都是字符串;ide
三、是一个能够持久化的属性集,键值能够存到集合中,也可存到持久化的设备上,键值的来源也但是持久化的设备;this
四、有和流技术相结合的方法:spa
代码演示:code
1 public static void main(String[] args) { 2 Properties pro=new Properties(); 3 //存数据
4 pro.setProperty("driver", "com.mysql.jdbc.driver"); 5 pro.setProperty("username", "root"); 6 pro.setProperty("password", "123456"); 7 //取数据 8 //获取键集
9 Set<String> set= pro.stringPropertyNames(); 10 for(String s:set){ 11 System.out.println(s+"..."+pro.getProperty(s)); 12 } 13 } 14
15
16 public class Demo02 { 17 public static void main(String[] args) throws IOException { 18 //从properties文件中读取键值对
19 Properties pro=new Properties(); 20 FileInputStream fis=new FileInputStream("x:\\test\\demo1.properties"); 21 pro.load(fis); 22 fis.close(); 23 Set<String> set=pro.stringPropertyNames(); 24 //遍历
25 for(String s:set){ 26 System.out.println(s+"..."+pro.getProperty(s)); 27 } 28 } 29
30
31 public static void main(String[] args) throws IOException { 32 //经过properties向文件中写键值对 33 //明确目的地
34 FileOutputStream fos=new FileOutputStream("x:\\test\\demo2.properties"); 35 Properties pro=new Properties(); 36 pro.setProperty("name", "xuezhiqian"); 37 pro.setProperty("age", "34"); 38 pro.store(fos, ""); 39 }
序列化流与饭序列化流:对象
读取对象的操做流ObjectInputStream:反序列化流blog
写入对象的操做流ObjectOutputStream:序列化流资源
特色:用于操做对象,可将对象写入到文件中,也可从文件中读取对象
代码演示:
1 //序列化
2 public class Demo01 { 3 public static void main(String[] args) throws IOException { 4 Person p=new Person("张三",18); 5 //明确目的地
6 FileOutputStream fos=new FileOutputStream("x:\\test\\person.txt"); 7 //建立序列化流
8 ObjectOutputStream oos=new ObjectOutputStream(fos); 9 //写入对象
10 oos.writeObject(p); 11 //释放资源
12 oos.close(); 13 }
瞬态关键字 transient
1 public class Person implements Serializable{ 2 private String name; 3 private transient int age;//瞬态关键字
4 private static final long serialVersionUID=123456789L; 5 public Person(String name, int age) { 6 super(); 7 this.name = name; 8 this.age = age; 9 } 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public int getAge() { 17 return age; 18 } 19 public void setAge(int age) { 20 this.age = age; 21 } 22 @Override 23 public String toString() { 24 return "Person [name=" + name + ", age=" + age + "]"; 25 }


1 //反序列化
2 public class Demo02 { 3 public static void main(String[] args) throws IOException, ClassNotFoundException { 4 //明确数据源
5 FileInputStream fis=new FileInputStream("x:\\test\\person.txt"); 6 //建立反序列化对象
7 ObjectInputStream ois=new ObjectInputStream(fis); 8 //反序列化
9 Object obj=ois.readObject(); 10 Person p=(Person)obj; 11 System.out.println(p); 12
13
14 }
打印流:(怎么写的怎么打印;不会有IOExecption异常)
字节打印流:PrintStream;
字符打印流:PrintWriter;
方法:void print(String str):输出任意类型的数据
void println(String str):输出任意类型的数据,自动写入换行操做
代码演示:
1 public static void main(String[] args) throws IOException { 2 //打印流复制 3 //明确数据源
4 FileReader fr=new FileReader("x:\\test\\test.txt"); 5 BufferedReader br=new BufferedReader(fr); 6 //明确数据目的
7 FileWriter fw=new FileWriter("x:\\test\\d\\pig.txt"); 8 PrintWriter pw=new PrintWriter(fw,true); 9 //复制
10 String line=null; 11 while((line=br.readLine())!=null){ 12 pw.println(line); 13 } 14 //释放资源
15 br.close(); 16 pw.close(); 17 }
commons-IO:
导入jar包
FilenameUtils:用来处理文件
经常使用方法:
getExtension(String path):获取文件的扩展名
getName():获取文件名
isExtension(String filename,String ext):判断filename是不是ext后缀名
FileUtils:
经常使用方法:
readFileToString(File file):读取文件内容,返回一个String
writeString(File file,String content):将内容写到field中
copyDirectoryToDirectory(File srcDir,File destDir);文件夹复制
copyFile(File srcFile,File destFile);文件复制
代码演示:
1 public class Demo01 { 2 public static void main(String[] args) throws IOException { 3 //method1();
4 method2(); 5 } 6 public static void method1(){ 7 //获取某文件扩展名
8 String name=FilenameUtils.getExtension("x:\\test"); 9 System.out.println(name); 10 //获取某文件名
11 String filename=FilenameUtils.getName("x:\\test\\test.txt"); 12 System.out.println(filename); 13 //判断某文件是否以什么为结尾
14 boolean flag=FilenameUtils.isExtension("aaa.java", "java"); 15 System.out.println(flag); 16 } 17 public static void method2() throws IOException{ 18 FileUtils.copyDirectoryToDirectory(new File("x:\\test"), new File("c:\\")); 19
20 } 21
22 }