1.转换流
1.1作用
实现了字节流到字符流的转换,解决中文乱码的问题
编码:
(1)中文编码:GB2312(采用两个字节保存字符汉字,英文和数字一个字节)
GBK(在简体中文windows操作系统中,ANSI编码代表GBK)(采用两个字节保存字符汉字,英文数字一个字节)
GB18030(中文是两个或四个字节,英文数字都是一个字节)
(2)unicode编码 (包含每个国家的所有字符,国际通用,浪费空间)
utf-8编码 (为了节省空间 使用1、2、3个字节 [EF BB BF]记事本添加的BOM头,编码的标记)
utf-16 (使用两个字节)
utf-32 (使用4个字节)
1.2 InputStreamReader类
字节字符转换输入流,将字节输入流转换为字符输入流
public static void main(String[] args) throws Exception{
//1.创建转换流
FileInputStream fis=new FileInputStream("g:\\gp4.txt");
InputStreamReader isr=new InputStreamReader(fis,"gbk");//可以选择适合的编码格式
//2.读取文件
char[] buf=new char[1024];
int len=-1;
while ((len=isr.read(buf))!=-1){
String str=new String(buf,0,len);
System.out.println(str);
}
//3.关闭
isr.close();
}
1.3 OutputStreamWriter类
字符转换输出流,将内存中的字符转换为字节保存到硬盘中
public static void main(String[] args) throws Exception{
//1.创建转换输出流
FileOutputStream fos=new FileOutputStream("g:\\bobo.txt");
OutputStreamWriter osw =new OutputStreamWriter(fos,"gbk");
//2.写入
for (int i = 0; i <10 ; i++) {
osw.write("今天晚上放假\r\n");
osw.flush();
}
//3.关闭
osw.close();
System.out.println("写入完毕");
}
2.缓冲流
作用:主要是为了增强基础流的功能而存在的,提高流的工作效率(读写效率)
2.1 BufferedInputStream类
public static void main(String[] args) throws Exception{
//创建对象
FileInputStream fis=new FileInputStream("hello.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//2.读取
byte[] buf = new byte[1024 * 4];
int len=0;
while ((len=bis.read(buf))!=-1){
String str=new String(buf,0,len);
System.out.println(str);
}
bis.close();
}
2.2 BufferedOutputStream类
public static void main(String[] args) throws Exception{
//1.创建缓冲流
FileOutputStream fos=new FileOutputStream("out.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
//2.写入
for (int i = 0; i <10 ; i++) {
bos.write("好好学习\r\n".getBytes());
bos.flush();
}
bos.close();
}
2.3 BufferedReader类
public static void main(String[] args) throws Exception{
//1.创建对象
FileReader fr=new FileReader("out.txt");
BufferedReader br=new BufferedReader(fr);
//2.写入
//2.1使用char数组
/* char[] buf=new char[1024];
int len=-1;
while ((len=br.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}*/
//2.2使用readLine();读取一行
// String line=br.readLine();
//读取多行
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
//3.关闭
br.close();
}
2.4 BufferedWriter类
public static void main(String[] args) throws Exception{
//1.创建对象
FileWriter fw=new FileWriter("buf.txt");
BufferedWriter bw=new BufferedWriter(fw);
//2.写入
for (int i = 0; i <10 ; i++) {
bw.write("我爱java");
bw.newLine();//行终止符
bw.flush();
}
//3.关闭
bw.close();
System.out.println("写入完成");
}
3.内存流
之前都是内存与硬盘的交互,而内存流输入和输出都是从文件来的,可以把输入和输出位置设置在内存上
3.1字节数组输入流(ByteArrayInputStream)
public static void main(String[] args) throws Exception{
byte[] data ="abcdefg".getBytes();
//1.创建
ByteArrayInputStream bais = new ByteArrayInputStream(data);
//2.读取
int d=-1;
while ((d=bais.read())!=-1){
System.out.print(d+" ");
}
//3.关闭
bais.close();
}
3.2 字节数组输出流(ByteArrayOutputStream)
public static void main(String[] args) throws Exception{
//1.创建
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//2.写入
for (int i = 0; i <10 ; i++) {
baos.write("hello".getBytes());
}
//3.关闭
baos.close();
//4.获取数组
String s=baos.toString();
System.out.println(s);
}
3.3 应用:完成一个字母大小写转换程序,使用内存流
public static void main(String[] args) throws Exception{
String str="HELLO WORLD";
//1.创建流
ByteArrayInputStream bais=new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//2.写入
int d=-1;
while ((d=bais.read())!=-1){
char c=Character.toLowerCase((char)d);
baos.write(c);
}
//3.关闭
bais.close();
baos.close();
//4.获取
String str2=baos.toString();
System.out.println(str2);
}
4.标准输入输出流
Java的标准输入\输出流分别通过System.in和System.out实现,默认情况下分别代表的是键盘和显示器
public static void main(String[] args) throws Exception{
/* //获取字节流
InputStream is = System.in;
//创建转换流
InputStreamReader isr = new InputStreamReader(is);
//创建缓冲流
BufferedReader br=new BufferedReader(isr);*/
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//2.读取
String line=null;
while (true){
line=br.readLine();
System.out.println(line);
if(line.equals("over")){
break;
}
}
//3.关闭
br.close();
}
PrintStream类:为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式
PrintWriter类:向文本输出流打印对象的格式化表示形式。此类实现了PrintStream中的所有print方法。
public static void main(String[] args) throws Exception{
//1.创建打印流
PrintStream ps = new PrintStream("print.txt");
PrintWriter pw=new PrintWriter("print2.txt");
//2.打印
ps.write(10);
ps.println(10);
ps.println(false);
ps.println("我爱编程");
pw.write(97);
pw.println();
pw.println(97);
pw.println(3.14);
pw.println(true);
pw.println("我爱java");
//3.关闭
ps.close();
pw.close();
}
5.对象流
流中流动的数据是对象
序列化:将一个对象写入本地文件中(输出)
反序列化:将本地文件中的对象读取出来(输入)
对象流:ObjectInputStream 对象输入流 ObjectOutputStream 对象输出流
注意: (1)序列化对象类型必须实现Serializable接口,否则不能序列化;
(2)如果想将多个对象序列化到本地,可以借助集合;
(3) 序列化版本id:private static final long serialVersionUID=1000L; (用来判断序列化和反序列化是否为同一个类)
public static void main(String[] args) throws Exception{
// writeObject();
readObject();
}
public static void writeObject() throws Exception{
//1.创建流
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("stu.bin"));
//2.序列化
Student s1=new Student("少波",20);
Student s2=new Student("波波",18);
Student s3=new Student("西西",17);
ArrayList<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
Student.country="china";
oos.writeObject(list);//
//3.关闭
oos.close();
System.out.println("序列化完成");
}
public static void readObject()throws Exception{
//1.创建流
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("stu.bin"));
//2.读取
ArrayList<Student> list=(ArrayList<Student>)ois.readObject();
for (Student student : list) {
System.out.println(student);
}
//3.关闭
ois.close();
}
6.RandomAccessFile类
用来访问那些保存数据记录的文件,可以用seek()方法来访问,并进行读写,这些记录的大小和位置必须是可知的。此类仅限于操作文件
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("file.txt", "rw");
// 以下向file文件中写数据
file.writeInt(20);// 占4个字节
file.writeDouble(8.236598);// 占8个字节
//这个长度写在当前文件指针的前两个字节处,可用readShort()读取
file.writeUTF("这是一个UTF字符串");
file.writeBoolean(true);// 占1个字节
file.writeShort(395);// 占2个字节
file.writeLong(2325451l);// 占8个字节
file.writeUTF("又是一个UTF字符串");
file.writeFloat(35.5f);// 占4个字节
file.writeChar('a');// 占2个字节
//把文件指针位置设置到文件起始处
file.seek(0);
// 以下从file文件中读数据,要注意文件指针的位置
System.out.println("——————从file文件指定位置读数据——————");
System.out.println(file.readInt());
System.out.println(file.readDouble());
System.out.println(file.readUTF());
//将文件指针跳过3个字节,本例中即跳过了一个boolean值和short值。
file.skipBytes(3);
System.out.println(file.readLong());
//跳过文件中“又是一个UTF字符串”所占字节
//注意readShort()方法会移动文件指针,所以不用写2。
file.skipBytes(file.readShort());
System.out.println(file.readFloat());
// 以下演示文件复制操作
System.out.println("——————文件复制(从file到fileCopy)——————");
file.seek(0);
RandomAccessFile fileCopy = new RandomAccessFile("fileCopy.txt", "rw");
int len = (int) file.length();// 取得文件长度(字节数)
byte[] b = new byte[len];
//全部读取
file.readFully(b);
fileCopy.write(b);
System.out.println("复制完成!");
}
7.Properties类
是Map接口的一个实现类,Properties集合中的元素也是以键值对的形式存在的,和流有关系
8.装饰者设计模式
装饰模式:是在不必改变原类文件和继承的情况下,动态地扩展一个对象的功能
应用场景:需要扩展一个类的功能,或给一个类添加附加职责
public class StrongPerson {
private Person person;
//用父类类型来实现功能
public StrongPerson(Person person) {
this.person = person;
}
public void eat(){
System.out.println("喝两口");
person.eat();
System.out.println("抽两口");
System.out.println("睡一会");
System.out.println("玩一会");
}
}
9. 大礼包