缓冲流
缓冲流(高效率的流)
BufferedOutputstream 缓冲输出字节流
构造方法:
BufferedOutputstream(Outputstream out)
参数: 字节输出流的弗雷 FileOutputStream
想对哪个流高效 就把这个流 传进去
BufferedInputStream 缓冲输入字节流
缓冲字节流
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("/Users/s/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));
}
bis.close();
}
/**
* @throws FileNotFoundException
* @throws IOException
*/
public static void fun1() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("/Users/s/Desktop/Test/ppp.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("HelloWorld".getBytes());
bos.close();
}
}
测试高效流复制文件的快慢
使用模板设计模式
abstract class TestTime{
public String src = "/Users/s/Desktop/Test/d.png";
public String dest = "/Users/s/Desktop/Test/d1.png";
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 MyCopy1 extends TestTime{
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 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();
}
}
缓冲字符流
BufferedWriter
构造方法:
参数: Writer
可以传: FileWriter OutputstreamWriter
特有方法:
newLine() 无关平台型(Mac/Windows)
public class Demo {
public static void main(String[] args) throws IOException {
FileReader fReader = new FileReader("/Users/s/Desktop/Test/ppp.txt");
BufferedReader br = new BufferedReader(fReader);
String string = "";
while ((string = br.readLine()) != null) {
System.out.println(string);
}
br.close();
}
public static void fun1() throws IOException {
FileWriter fWriter = new FileWriter("/Users/s/Desktop/Test/ppp.txt");
BufferedWriter bw = new BufferedWriter(fWriter);
bw.write("扶大厦之将倾");
bw.newLine();
bw.flush();
bw.write("挽狂澜之既倒");
bw.newLine();
bw.flush();
bw.close();
}
}
字符缓冲流复制文件
public class Demo04 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/s/Desktop/Test/ppp.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("/Users/s/Desktop/Test/ppp1.txt");
BufferedWriter bw = new BufferedWriter(fw);
String string = "";
while ((string = br.readLine()) != null) {
bw.write(string);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
关于 流 的一个小总结
1.明确要做什么操作
读数据源
InputStream / Reader
写到数据目的地
Outputstream / Writer
2.明确要操作的是什么内容
文本 音频 图片 等等...
要使用字节流(全能流) FileInputStream FileOutputStream
文本(按什么编码格式读写)
使用字符流 FileWriter FileReader
3.明确流要在什么设备上使用
文本
网络 通过刘 进行数据交互 --- 字节流
4. 是否需要提高效率
Buffered 缓冲流
Properties集合
Properties集合 是双列集合
Properties的父类是 Hashtable
作用: Properties是集合中 唯一一个能和IO流配合的类
读取和写入时
参数可以是字符流
参数也可以是字节流
public class Demo {
public static void main(String[] args) throws IOException {
}
/**
* @throws IOException
*/
public static void fun3() throws IOException {
Properties properties = new Properties();
properties.setProperty("a", "hh");
properties.setProperty("b", "zz1");
properties.setProperty("你好", "再见");
FileWriter fWriter = new FileWriter("/Users/s/Desktop/Test/b.properties");
properties.store(fWriter, "");
fWriter.close();
}
/**
* @throws FileNotFoundException
* @throws IOException
*/
public static void fun2() throws FileNotFoundException, IOException {
Properties p1 = new Properties();
FileReader fr = new FileReader("/Users/s/Desktop/Test/b.properties");
p1.load(fr);
System.out.println(p1);
fr.close();
}
/**
*
*/
public static void fun1() {
Properties properties = new Properties();
properties.setProperty("gender", "女");
Set<String> set = properties.stringPropertyNames();
for (String key : set) {
String value = properties.getProperty(key);
System.out.println(key + " = " + value);
}
}
}
序列化流与反序列化流
序列化: 把文件写入文件中(根据我们现在的学习进度)
反序列化: 从文件中 把对象 读取出来
对象流
ObjectInputStream 反序列化流
ObjectOutputstream 序列化流
注意: 静态成员变量 是不能进行序列化的
序列化 序列是对象 静态成员变量是属于类
序列化相当于是 把 对象进行持久化
先创建一个Person类
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 "Person [name=" + name + ", age=" + age + "]";
}
}
public class Demo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
readObject();
public static void writeObject() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("/Users/s/Desktop/Test/Person.txt");
ObjectOutputstream oos = new ObjectOutputstream(fos);
oos.writeObject(new Person("s", 21);
oos.close;
}
public static void readObject() throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("/Users/s/Desktop/Test/Person.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
System.out.println(object);
ois.close();
}
}
Day.26
http://blog.youkuaiyun.com/ssssssue