缓冲流(高效流):
缓冲字节流:
BufferedOutputStream:缓冲输出字节流
父类:FileOutputStream
构造方法:BufferedOutputStream(OutputStream out)
BufferedInputStream:缓冲输入字节流
父类:FileInputStream
构造方法:BufferedInputStream(InputStream in)
缓冲流复制文件效率比较:
abstract class TestTime {
public String src = "/Users/lanou/Desktop/Test/dp.png";
public String dest = "/Users/lanou/Desktop/Test/dp1.png";
public void printTime() throws IOException {
long start = System.currentTimeMillis();
copFile();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public abstract void copFile() throws IOException;
}
class MyCopy1 extends TestTime {
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStram(src);
FileOutputStream fos = new FileOutputStream(dest);
int len = 0;
while (len = fis.read()) != -1) {
fos.writer(len);
}
fis.close();
fos.close();
}
}
class MyCopy2 extends TestTime {
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStram(src);
BufferedInputStram bis = new BufferedInputStram(fis);
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStram bos = new BufferedOutputStram(fos);
int len = 0;
while (len = bis.read()) != -1) {
bos.writer(len);
}
bis.close();
bos.close();
}
}
class MyCopy3 extends TestTime {
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStram(src);
FileOutputStream fos = new FileOutputStream(dest);
int len = 0;
byte[] b = new byte[1024];
while (len = fis.read(b)) != -1) {
fos.writer(b, 0, len);
}
fis.close();
fos.close();
}
}
class MyCopy4 extends TestTime {
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStram(src);
BufferedInputStram bis = new BufferedInputStram(fis);
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStram bos = new BufferedOutputStram(fos);
int len = 0;
byte[] b = new byte[1024];
while (len = bis.read(b)) != -1) {
bos.writer(b, 0, len);
}
bis.close();
bos.close();
}
}
缓冲字符流:
BufferedWriter:缓冲输出字符流
父类:Writer
构造方法:BufferedWriter(Writer out)
特有方法:newLine()
相当于换行符,Mac、Windows都可以使用,无关平台性
BufferedReader:缓冲输入字符流
父类:Reader
构造方法:BufferedReader(Reader in)
特有方法:readLine()
具有换行作用,但是读取源文件时,无法将换行读取出来,如需实现换行,需自行添加换行
案例:
public static void fun() throws IOException {
FileReader fr = new FileReader("/Users/lanou/Desktop/Test/ppp.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test/ppp1.txt");
BufferedWriter bw = new BufferedWriter(fw);
String string = " ";
while((string = br.readLine()) != null) {
bw.write(string);
bw.flush();
}
br.close();
bw.close();
}
字节流、字符流、转换流、缓冲流的总结:
1.明确是读取源文件,还是写到数据目的;
2.对文本、音频、图片等进行操作使用字节流,对文本操作使用字符流
3.确认是否要对读写进行效率提升
Properties集合:
是一个双列集合,它的父类是Hashtable,它是没有泛型的
作用:可以和IO流配合使用
注意:该集合key和value最好都使用字符串
public static void read() throws FileNotFoundException, IOException {
Properties properties = new Properties();
FileReader fr = new FileReader("/Users/lanou/Desktop/Test/xzb.properties");
properties.load(fr);
System.out.println(properties);
fr.close();
}
public static void write() throws IOException {
Properties properties = new Properties();
properties.setProperty("a", "haha");
properties.setPorperty("张三", "哈哈哈");
properties.setPorperty("b", "haha");
FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test/xzb.properties");
properties.store(fw, "");
fw.close();
}
序列化流与反序列化流:
ObjectOutputStream:序列化,把对象写进文件中
ObjectInputStraem:反序列化,把对象从文件中读取出来
序列化相当于把对象进行持久化
注意:静态变量是不能进行实例化的
在对对象进行实例化时,要实现Serializable接口,该接口时标记性接口
进行反序列化时,要依赖.class文件进行读取
案例:
public class Person implements Serializable {
/**
* 序列化使用序列号
* 只要写了这个号 在编译时,系统就不会重新计算序列号
*/
private static final long serialVersionUID = 1L;
private transient String name;
private 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 "姓名:" + name + ", 年龄:" + age;
}
}
public static void WriteObject() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/person.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Person("dp", 15));
oos.close();
}
public static void ReadObject() throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/person.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
System.out.println(obj);
ois.close();
}