JAVASE12
一、
IO流中的功能流
1、缓冲流
格式:功能流(节点流);
首先我们来介绍缓冲流:
缓冲流可以增强节点流的读写效率,并且能提高节点流的性能
//字节流是没有新增方法的,我们可以使用多态
BufferedInputStream //字节输入流缓冲流
BufferedInputStream(InputStream)//格式
BufferedOutputStream //字节输出流缓冲流
BufferedOutputStream(OutputStream)//格式
//字符流有新增方法,不能使用多态
BufferedReader //字符输入缓冲流
BufferedReader(Reader)
String readLine() //读一行文字。
BufferedWriter //字符输出流的缓冲流
BufferedWriter(Writer)
void newLine() //写一个行分隔符
示例:
public static void main(String[] args) throws Exception {
ReaderBuffered();
}
//字节流缓冲流
public static void InputStreamBuffered() throws Exception {
//1.获取流
InputStream is = new BufferedInputStream(new FileInputStream("F://test.txt"));
OutputStream os = new BufferedOutputStream(new FileOutputStream("F://haha.txt"));
//2.读写
byte[] arr = new byte[1024];
int len = -1;
while((len=is.read(arr))!=-1){
os.write(arr,0,len);
}
//3.刷出
os.flush();
//4.关闭流
os.close();
is.close();
}
//字符流缓冲流使用
public static void ReaderBuffered() throws Exception {
//1.获取流
BufferedReader is = new BufferedReader(new FileReader("F://test.txt"));
BufferedWriter os = new BufferedWriter(new FileWriter("F://haha.txt"));
//2.读写
String msg = null;//我们用到readLine方法需要用String类型接受
while((msg=is.readLine())!=null){
os.write(msg);
os.newLine(); //换行
}
//3.刷出
os.flush();
//4.关闭流
os.close();
is.close();
}
2、转换流
是一个功能流
它是从字节流到字符流的桥接器
InputStreamReader和OutputStreamWriter作为它的输入和输出类名
作用:我们在java程序输入和输出过程很有可能造成字符乱码问题,我们可以根据此来设置编码格式
构造器:
InputStreamReader(InputStream in)// 创建一个使用默认字符集的InputStreamReader。
InputStreamReader(InputStream in, String charsetName) //创建一个使用指定charset的InputStreamReader。
示例:
public static void main(String[] args) throws IOException {
//1.构建流
BufferedReader re = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream("D://test.txt")),"GBK"));
//这里我们用字符流增强流的新方法
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("D://A.txt")),"GBK"));
String msg = null;
while((msg = re.readLine())!=null){
System.out.println(msg);
bw.write(msg);
}
//刷出
wr.flush();
//关闭流
wr.close();
re.close();
}
3、基本数据类型流或者称Data流
它可以基本数据类型的4类8种以及String类型的输入和输出操作
格式:Data流(节点流)
它的功能:能够让节点流具有传输基本数据类型和String数据的能力
DataInputStream //基本数据类型输入流
DataOutputStream //基本数据类型输出流
//有新增方法
这里我们再提醒一句: 有新增方法就不能使用多态
注意:如果遇到EOFException 无法正常读取成功说明你这个文件不是源文件(可能是你拷贝的文件)
示例:
public static void main(String[] args) throws IOException {
writeData("F:/desc2.txt")//调用输出,需要先输出才能输入
readData("F:/desc2.txt");//调用输入
}
//写出到文件: 数据+数据类型
public static void writeData(String desc) throws IOException {
//1.获取流
DataOutputStream ot = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(desc)));
//2.输出
int i = 1;
boolean flag = false;
char ch = 'a';
String str = "红绿灯";
//新增方法
ot.writeInt(i);
ot.writeBoolean(flag);
ot.writeChar(ch);
ot.writeUTF(str);
//3.刷出
ot.flush();
//4.关闭
out.close();
}
//读入: 数据+数据类型
public static void readData(String src) throws IOException {
//1.获取流
DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(src)));
//2.读入
int i = in.readInt();
boolean b = in.readBoolean();
char c = in.readChar();
String s = in.readUTF();
//这里我们需要知道按顺序读入,不能乱读
System.out.println(i+"-->"+b+"--->"+c+"-->"+s);
//3.关闭
in.close();
}
4、对象或称Object流
它包括所有类型(基本数据类型和引用数据类型)
这里我们需要知道什么叫做序列化!
序列化: java对象类型的信息状态转换成为一个可存储,可传输的信息状态的过程
反序列化(输入的过程)
ObjectInputStream //对象字节输入流 | 反序列化输入流
//新增方法 Object readObject() 从ObjectInputStream中读取一个对象。
ObjectOutputStream //对象字节输出流 | 序列化输出流
//新增方法 void writeObject(Object obj) 将指定的对象写入ObjectOutputStream。
这里我们需要注意一些事项:
-
读写顺序要保持一致
-
不是所有的类型的对象都能够序列化 实现java.io.Serializable空接口(我们继承这个接口才能序列化,不继承不能序列化,要求: 类必须实现Serializable空接口)
-
我们在应用过程要知道不是所有的属性都需要序列化的,这是我们可以在属性前加上 transient 关键字,修饰的字段不会被序列化
-
static静态修饰的成员不会被序列化(但是可以显示在输出语句中且可以通过set设置器改变)
-
父类实现了序列化接口,子类所有的内容都能序列化
-
子类实现了序列化接口,子类只能序列化自己的内容,不能序列化父类中继承的内容
这里我们还需要知道序列号 : 控制版本的更新(就是让系统识别你的序列号,同一序列号就为同一版本,如果版本不同,会报错)
如果之前的数据的序列号与当前类型的序列号id不一致,证明版本不统一,如果想做版本兼容,需要手动提供一个序列号id,保证一直不变,就可以实现兼容
示例:
//创建一个Student类
public class Student implements Serializable {
//private static final long serialVersionUID = -8407352356667496115L;
private int id;
private String name;
private static String subject;
private transient String school;
private int age;
private int hahaha;
public Student() {
}
public Student(int id, String name,String subject, String school, int age) {
this.id = id;
this.name = name;
this.subject = subject;
this.school = school;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", school='" + school + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id &&
age == student.age &&
Objects.equals(name, student.name) &&
Objects.equals(school, student.school);
}
@Override
public int hashCode() {
return Objects.hash(id, name, school, age);
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
testWrite("D:/b.txt");
testRead("D:/b.txt");
}
//写出序列化
public static void testWrite(String desc) throws IOException {
//1.构建流
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(desc)));
//2.准备数据
int[] arr = {1,2,3,4,5};
Student s = new Student(1001,"周杰伦","软件工程","清华大学",19);
//3.写出
out.writeObject(arr);
out.writeObject(s);
//4.刷出
out.flush();
//5.关闭
out.close();
s.setSubject("计算机科学与技术");
s.setName("zhangsan");
}
//反序列化
public static void testRead(String src) throws IOException, ClassNotFoundException {
//1.构建流
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(src)));
//2..写出
int[] arr2 = (int[]) in.readObject();
Student s = (Student) in.readObject();
//如果这里改变Student对象的属性个数,则序列号不同无法读,如手动设置序列号则虚拟机认为是一个版本则会读出有的属性
System.out.println(Arrays.toString(arr2));
System.out.println(s);
//3.关闭
in.close();
}
二、
commons-Io
这是一个组件
使用过程:
1.下载jar包
2.把jar集成到项目中
3.使用
示例:
public static void main(String[] args) throws IOException {
//文件的拷贝
//IOUtils.copy(InputStream input, OutputStream output) 自定义使用什么流
//FileUtils.copyFile(File srcFile, File destFile) // 复制文件
FileUtils.copyFile(new File("D:/test.txt"),new File("D:/c.txt"));
IOUtils.copy(new FileInputStream("D:/test.txt"), new FileOutputStream("D:/d.txt"));
//FilenameUtils.getExtension(String filename)
System.out.println(FilenameUtils.getExtension("D://haha123.txt.java"));
//FileUtils.copyDirectory(File srcDir, File destDir) // 复制文件夹(文件夹里面的文件内容/也会复制) 注意:目的地需要完整路径
//FileUtils.copyDirectory(new File("D://AAA"),new File("D://HEHE/AAA"));
//FileUtils.copyDirectory(File srcDir, File destDir, FileFilter filter) // 复制文件夹,
//带有文件过滤功能 了解
FileUtils.copyDirectory(new File("D://AAA"),new File("D://HEHE/AAA"), FileFilterUtils.fileFileFilter());
}
这里介绍一个只需了解的节点流
//字节数组流
ByteArrayInputStream // 节点流 一端是程序 另一个端是字节数组
//相对于文件流
FileInputStream //一段是程序,另一端是文件 节点流
三、
集合:容器
数组:
- 长度一旦确定不可改变
- 存储的数据类型要求相同
- 有序,有索引 (0~length-1)
容器:
- 长度根据数据的多少动态的伸缩
- 可以存储任意类型的数据,只能存储引用数据类型
本期学习重点:
ArrayList
HashMap
示例:
public static void main(String[] args) {
//1.一个集合对象
ArrayList ls = new ArrayList();
//2.储存数据
ls.add("哈哈");
ls.add(false);
ls.add(123);
ls.add('c');
ls.add(new int[]{1,2,3});
System.out.println(ls);
ls.add("hehe");
System.out.println(ls);
//移除"哈哈"
ls.remove("哈哈");
System.out.println(ls);
System.out.println(ls.size());
}