一、Properties类
1.概念
首先,Properties是一个集合,存储键值对。实现map接口;没有泛型,键值都是字符串,可用于持久化储存数据。
2.主要方法
- void setProperties(String key , String value); //存入键值对,相当于put
- String getProperties(String key); //取出键值对,相当于get
- Set<String> stringPropertyNames(); //用于遍历集合,得到所有键的set集合
- load(InputStream in); :读取指定的文件,将读取到的键值对保存到properties集合中
- load(Reader);重载
- store(OutputStream out , commons); //将键值对永久保存到文件中,commons代表描述信息,就是注释
- store(Writer); //重载
3.代码展示
/*
* Properties练习
*/
public static void testProperties() throws IOException
{
Properties pro = new Properties();
//1.向集合中存对象,注意不要使用中文,因为会进行ASCII编码,
//使用中文会看不懂,但是依然可以存,能够正常load
pro.setProperty("键1", "值1");
pro.setProperty("key2", "value2");
pro.setProperty("键3", "值3");
//2.从集合中取对象
System.out.println(pro.getProperty("键1"));
//3.持久化保存
OutputStream os = new FileOutputStream("d:\\a.txt");
pro.store(os, "");
//4.读取持久化数据
InputStream is = new FileInputStream("d:\\a.txt");
pro = new Properties();
pro.load(is);
//5.遍历Properties
Set<String> set = pro.stringPropertyNames();
for (String string : set) {
System.out.println(string + pro.getProperty(string));
}
//6.打印Properties
System.out.println(pro);
}
二、序列化与反序列化
- 序列化流将Java中的对象,基本数据类型,引用类型等写入到文件中,进行持久化保存
- 反序列化流,可以将序列化保存的文件,再次读取到内存中
- 序列化构造方法:ObjectOutputStream(OutputStream out);
- 反序列化构造方法:ObjectInputStream(InputStream in);
- 写方法: void writeObject(Object obj);
- 读方法:object readObject();
- 瞬态关键字:transient,对成员变量进行保护,防止其被序列化
- 代码展示
/*
* 序列化与反序列化练习
*/
public static void testObjectStream() throws IOException, ClassNotFoundException
{
//0.序列化
//1.创建字节输出流
OutputStream out = new FileOutputStream("d:\\a.txt");
//2.创建对应的序列化流
ObjectOutputStream oos = new ObjectOutputStream(out);
//3.进行序列化,持久储存
//oos.write(new byte[]{97,98});
//oos.write(97);
//oos.writeBytes("123");
oos.writeObject(new Person("张三",17));
oos.close();
//4.进行反序列化读取
InputStream in = new FileInputStream("d:\\a.txt");
ObjectInputStream ois = new ObjectInputStream(in);
//System.out.println(ois.read());
//System.out.println(ois.read());
System.out.println(ois.readObject());
ois.close();
}
import java.io.Serializable;
/**
* @author Mr.GE
* @date 创建时间:2018-4-13 下午2:52:13
* @version 1.0
* @parameter
* @since
* @return
*/
public class Person implements Serializable{
private String name;
public static int age;
public String aaa;
public static final long serialVersonUID = 132134354L;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", 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;
}
}
三、打印流
1.概念
打印流一共有2个:字节打印流printStream 和 字符打印流 printWriter;就是将字符输出到指定的文件,仅仅是为了方便打印和输出,不抛出io异常,但是可能会抛出其他异常
2.构造方法
printStream(File / StringName / OutputStream);
printWriter(File / StringName / OutputStream / Writer);
如果传入的是流~可以开启自刷新功能printWriter(OutputStream out,true);
3.打印方法
print();
println(); //无论是什么,都会原样输出
4.代码展示
/*
* 打印流
*/
public static void func1() throws IOException
{
//1.String filename构造打印流
PrintStream ps = new PrintStream("d:\\a.txt");
//2.File 构造
ps = new PrintStream("d:\\b.txt");
//3.OutputStream构造
FileOutputStream fos = new FileOutputStream("d:\\c.txt");
ps = new PrintStream(fos,true); //如果构造是流,是可以开启自刷新的
ps.println("abc");
ps.print(100);
ps.print(true);
ps.close();
//4.StreamWriter构造,注意,Writer的话,打印流必须使用PrintWriter
//所以建议使用PrintWriter,因为它的构造源比较广泛
FileWriter fw = new FileWriter("d:\\d.txt");
PrintWriter pw = new PrintWriter(fw,true);
pw.print("abc");
pw.close();
}
4.使用打印流复制文件
/*
* 使用打印流复制文件
*/
public static void func2() throws IOException
{
//创建源文件和目的文件
File srcFile = new File("d:\\a.txt");
File desFile = new File("d:\\b.txt");
//创建输入流和对用的编码转换流
InputStream out = new FileInputStream(srcFile);
InputStreamReader fr = new InputStreamReader(out,"gbk");
//创建缓冲流
BufferedReader br = new BufferedReader(fr);
//创建打印流
PrintWriter pw = new PrintWriter(desFile);
String str = "";
while((str = br.readLine()) != null)
{
pw.println(str);
}
pw.close();
br.close();
}
四、IO使用工具类:commons-io
代码展示
/*
* 练习使用第三方工具commons.io.FilenameUtils
*/
public static void func3()
{
//1.获取文件的拓展名
String ext = FilenameUtils.getExtension("hello.java");
System.out.println(ext); //注意:后缀名不带点(.)
//2.获取文件名,不带路径,带拓展名
String name = FilenameUtils.getName("d:\\ccc\\c.java");
System.out.println(name);
//3.判断是否是以特定拓展名的文件,判断文件类型
boolean b = FilenameUtils.isExtension("d:\\ccc\\c.java", "Java");
System.out.println(b); //注意区分大小写
}
/*
* 练习使用第三方工具commons.io.FileUtils
*/
public static void func4() throws IOException
{
//1.将String 写入到文件
File file = new File("d:\\a.txt");
FileUtils.writeStringToFile(file, "我爱Java" +
"\r\n" + "我爱编程");
//2.读取文件
String str = FileUtils.readFileToString(file);
System.out.println(str);
//3.复制文件到文件
FileUtils.copyFile(new File("d:\\a.txt"), new File("d:\\b.txt"));
//4.复制文件夹到文件夹
FileUtils.copyDirectoryToDirectory(new File("D:\\Documents"), new File("E:\\"));
}
全文完!