缓冲流
使用缓冲流的好处是,能够更高效的读写信息,
原理是将数据先缓冲起来,然后一起写入或者读取出来。
缓冲输出字符流
BufferedWriter
public static void write() throws IOException {
FileWriter fw = new FileWriter("要写入的文件位置");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("明月几时有 把酒问青天");
bw.newLine();
bw.flush();
bw.write("明月几时有 把酒问青天");
bw.newLine();
bw.flush();
bw.close();
}
缓冲输入字符流
BufferedReader
public static void read() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("读取的文件路径");
BufferedReader br = new BufferedReader(fr);
String string = "";
while ((string = br.readLine()) != null) {
System.out.println(string);
}
br.close();
}
缓冲字符流 复制文件
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("输入的文件路径");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("输出的文件路径");
BufferedWriter bw = new BufferedWriter(fw);
String string = "";
while ((string = br.readLine()) != null) {
bw.write(string);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
缓冲输出字节流
BufferedOutputStream
public static void fun() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("/Users/C/Desktop/Test/ppp.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("helloWorld".getBytes());
bos.close();
}
缓冲输入字节流
public static void fun() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("/Users/C/Desktop/Test/ppp.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[1024];
int len = 0;
while ((len = bis.read(b)) != -1) {
System.out.println(new String(b, 0, len));
}
}
测试缓冲流能高效多少
创建抽象类来计算时间
abstract class TestTime{
public String src = "要复制的文件路径";
public String dest = "复制玩存放的文件路径";
public void printTime() throws IOException {
long start = System.currentTimeMillis();
copyFile();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public abstract void copyFile() throws IOException;
}
"分别测试: "
"字节流" 单字节形式 复制文件
数组形式 复制文件
"缓冲流" 单字节形式 复制文件
数组形式 复制文件
使用字节流 以字节形式 复制文件
class ByteCopy1 extends TestTime {
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
int len = 0;
while ((len = fis.read()) != -1) {
fos.write(len);
}
fis.close();
fos.close();
}
}
使用字节流 以数组形式 复制文件
class ByteCopy2 extends TestTime {
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
int len = 0;
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fis.close();
fos.close();
}
}
使用缓冲流 以字节形式 复制文件
class Mycopy extends TestTime{
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(src);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len = 0;
while ((len = bis.read()) != -1) {
bos.write(len);
}
bis.close();
bos.close();
}
}
使用缓冲流 以数组形式 复制文件
class Mycopy2 extends TestTime{
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(src);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(dest);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len = 0;
byte[] b = new byte[1024];
while ((len = bis.read(b)) != -1) {
bos.write(b,0,len);
}
bis.close();
bos.close();
}
}
测试后 发现缓冲流能大幅提升效率
序列化流 与 反序列化流
序列化
把对象写进文件中
ObjectOutputStream 序列化流
public static void writeObject() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("序列化的文件路径");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(new Person("zhangsan", 16));
oos.close();
}
反序列化
从文件中把对象读取出来
public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("读取的序列化文件路径");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
System.out.println(obj);
ois.close();
}
Properties 集合 (双列集合) 父类Hashtable
作用: Properties是集合中唯一一个能和IO流配合的类
读取和写入时参数
可以使字符流
也可以字节流
public static void fun() {
Properties properties = new Properties();
properties.put("name",1);
properties.setProperty("gender", "nv");
Set set = properties.stringPropertyNames();
for (String key : set) {
String value = properties.getProperty(key);
System.out.println(key + "==" + value);
}
}
public static void fun() throws FileNotFoundException, IOException {
Properties properties = new Properties();
FileReader fr = new FileReader("/Users/Desktop/Test/xieru.txt");
properties.load(fr);
System.out.println(properties);
fr.close();
}
public static void fun() throws IOException {
Properties properties = new Properties();
properties.setProperty("a", "haha");
properties.setProperty("b", "666");
properties.setProperty("c", "5415");
FileWriter fw = new FileWriter("/Users/Desktop/Test/fun.properties");
properties.store(fw, "");
fw.close();
}