学习目标:
区分字节和字符文件的一般方式
通过文本文档的方式打开如果是乱码那就是字节文件,如果可以读懂就是字符文件
1.字节流
FileInputStream
FileOutputStream
BufferedInputStream
BufferedOutPutStream
2.字符流
FileWriter
FileReader
BufferedFileWriter
BufferedFileReader
3.Properties
4.序列化与反序列化
序列化:将对象转换为字节流的过程
反序列化:将字节流转换为对象的过程
学习内容:
FileInputStream(“文件路径”)字节输入流
字节输入流将指定文件以字节的形式从磁盘读到内存中,使用read()函数一次读一个,如果读到文件末尾返回-1
try (InputStream fs = new FileInputStream("C:\\Users\\wbx\\Desktop\\write.jpg")) {
int a=0;
while((a=fs.read())!=-1) {
System.out.println(a);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
通过指定字符数组的形式读文件内容一次读入指定大小的字节
底层采用native方法readbytes实现
public static void main(String[] args) {
try (InputStream fs = new FileInputStream("C:\\Users\\wbx\\Desktop\\write.jpg")) {
int len=0;
byte[] b=new byte[16];
while((len=fs.read(b))!=-1) {
System.out.println(len+" "+Arrays.toString(b));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream(“文件路径”)字节输出流
字节输出流将内存中的内容以字节的形式从内存写到磁盘中,通过os.write(b, 0, len)方法将字节数组b中从下标0到下标len的字节内容写到指定位置的磁盘中
try (InputStream fs = new FileInputStream("C:\\Users\\wbx\\Desktop\\222.jpg");
OutputStream os=new FileOutputStream("C:\\Users\\wbx\\Desktop\\p.jpg");
) {
int len=-1;
byte[] b=new byte[16];//通过指定字节数组大小的方式
while((len=fs.read(b))!=-1) {//返回读到的字节个数
os.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
BufferedInputStream os1=new BufferedInputStream(new FileInputStream(“文件路径”));
带有8192字节数组大小的缓冲区
如果自己指定数组则每次读指定大小的字节放到8192的缓冲区中,当指定的字节数组大小接近8192那么BufferedInputStream 的也就没意义了
BufferedOutputStream io1=new BufferedOutputStream(new FileOutputStream(“文件路径”));
==可以通过构造方法指定缓冲区的大小 ==
带有8192字节数组大小的缓冲区
如果自己指定数组则每次读指定大小的字节放到8192的缓冲区中,当指定的字节数组大小接近8192那么BufferedOutputStream 的也就没意义了
所说的减少io次数,是说减少io这个过程,因为每次写的时候就写缓冲区中的内容
网页下载图片的案列
try (BufferedInputStream in = new BufferedInputStream(ur2.openStream());//放入图片地址的字节流
BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream("E:\\doubanJpg\\"+System.currentTimeMillis()+".jpg"))) {
int len=-1;
byte[] b=new byte[1024];
while((len=in.read(b))!=-1) {
ou.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileReader字符输入流
它每次读取的是字符通过read()每次读取一个,read©每次读取c数组大小个字符,读取结束返回-1
public static void main(String[] args) throws IOException {
Reader r=new FileReader("C:\\Users\\wbx\\Desktop\\Path.txt");
// int c=-1;
// while((c=r.read())!=-1) {
// System.out.print((char)c);
// }
int len=-1;
char[] c=new char[10];
while((len=r.read(c))!=-1) {
System.out.println(c);
System.out.println("****************");
}
}
FileWriter字符输出流
new FileWriter(“C:\Users\wbx\Desktop\123.txt”,true),通过构造方法为true可以追加内容,每次写入一个字符
运行一次追加一次
FileWriter f=new FileWriter("C:\\Users\\wbx\\Desktop\\123.txt",true);
f.write("a");
f.write(new char[] {'q','w','e'});//每次追加qwe
f.write(97);//将97转换为字符写入
f.close();
BufferedWriter
与FileWriter一样只不过指定了一个8192大小的字符数组
BufferedWriter b=new BufferedWriter(new FileWriter("C:\\Users\\wbx\\Desktop\\123.txt",true));
for (int i = 0; i < 30; i++) {
//每次写入一个字符串
b.write(String.format("第%d行:%s", i+1,UUID.randomUUID().toString()));
//写入一个换行
b.newLine();
}
BufferedReader
与FileReader一样只不过指定了一个8192大小的字符数组,通过readline每次读取一行,结束返回null
BufferedReader b=new BufferedReader(new FileReader("C:\\Users\\wbx\\Desktop\\Path.txt"));
String s=null;
while((s=b.readLine())!=null) {
System.out.println(s);
BufferedReader 和BufferedWriter进行文本内容的复制
== 将两个对象写到try()中可以自动关闭流==
public static void main(String[] args) {
try (BufferedReader in = new BufferedReader(new FileReader("C:/Users/wbx/Desktop/123.txt"));
BufferedWriter ou = new BufferedWriter(new FileWriter("C:/Users/wbx/Desktop/1234.txt"))) {
String result=null;
while((result=in.readLine())!=null) {
ou.write(result);
ou.newLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties
文件的后缀名为properties的文件内容以key=value的形式存储
获取内容
//key=value形式存储
Properties pro=new Properties();//继承Hashtable<Object,Object>
pro.load(new FileInputStream("C:\\Users\\wbx\\Desktop\\write.properties"));//字符字节流都可以
System.out.println(pro);//所有的键值对
//获取可以为dd的值
String str1=pro.get("dd").toString();//返回object对象
//获取可以为dd的值,如果没有则返回默认值default
String str2=pro.getOrDefault("dd", "default").toString();//避免key为空
写入内容,通常key-value的类型为字符串
Properties pro=new Properties();
pro.put("san", "o");
pro.put("si", ";");
pro.put("g", "a");
pro.store(new FileOutputStream("C:\\Users\\wbx\\Desktop\\writ.properties"), "this is pratice!");//第二个参数为注释
特别方法
String classPath=Class.class.getResource("/").toString();//获取当前类所在的工程的bin目录地址
System.out.println(classPath);
InputStream in=Class.class.getResourceAsStream("/1.propertice");//获取bin目录下文件的流对象,为键值对
System.out.println(in);
序列化与反序列化
一个类要被序列化它必须实现Serializable接口
小点
序列化是用来保存数据的 序列化前后自定义的类的结构不能改变 否则会反序列化失败
只要指定private static final long serialVersionUID 那么序列化前后id不变那么反序列化不会失败
如果指定id,但是反序列化前后类的结构不同就不会出现期望结果
(比如没有了toString方法那么就会使用object的String方法不能看到内容),但是不会抛异常
使用transient该关键字修饰的变量不会被序列化
序列化前后类结构进行比较多了输出少了为null
理解
序列化使用来保存数据的
可以把序列化和反序列化理解为QQ客户端两个人在发消息,因为两个人都下载了qq,因此两个人QQ的内部结构相同,就相当于java中两个人使用相同的类结构,一个人通过序列化发数据发送过去,另一个人通过反序列化得到数据
举例
ObjectOutputStream序列化
ObjectInputStream 反序列化
public class XuLieHua2 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//序列化
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("C:\\Users\\wbx\\Desktop\\obje.txt"));
out.writeObject(new Test("wangboxin",8));
//反序列化
ObjectInputStream in=new ObjectInputStream(new FileInputStream("C:\\Users\\wbx\\Desktop\\obje.txt"));
Test ns=(Test)in.readObject();
System.out.println(ns);
}
}
class Test implements Serializable{
private static final long serialVersionUID = 9038519828850227431L;
public transient String s;//使用transient该关键字修饰的变量不会被序列化
protected double b;
public Test() {
}
public Test(String s, double b) {
super();
this.s = s;
this.b = b;
}
@Override
public String toString() {
return "jiang [s=" + s + ", b=" + b + "]";
}
}