6.20
一、座右铭
我的故事你说,我的文字我落,我值几两你定,我去何方我挑。
二、知识回顾
2.1 Java集合体系
-
Collection
- List
- Vector
- ArrayList
- LinkedList
- Set
- HashSet
- LinkedHashSet
- TreeSet
- Queue
- Collections–工具类
- List
-
Map
- HashMap
- LinkedHashMap
- Hashtable———Properties
- TreeMap
2.2 JavaIO流—java.io
-
IO流的分类
- 流向:输入和输出流
- 流的大小:字节流和字符流
- 流的功能:节点流和处理流
-
File文件对象类
-
JavaIO流的四大基类—抽象类
-
InputStream:字节输入流
方法名 read()|read(byte[]):int close() available() -
OutputStream:字节输出流
方法名 write(int | byte[]) close() InputStream、OutputStream:
字节流,处理数据时,一个字节一个字节的处理
-
Reader:字符输入流
方法名 read()|read(char[]):int close() -
Writer:字符输出流
方法名 wrtie(int|char[]|String) flush() close() Reader、Writer:
字符流,处理数据时,一个字符一个字符的进行处理,字符流处理需要使用到编码集,只有纯文本类型的数据才有编码集的概念
-
-
JavaIO流中的常用实现类
-
节点流:直接连接数据源的流
-
数组流:连接的数据源是一个数组
-
字节数组流
-
ByteArrayInputStream
-
ByteArrayOutputStream
–输出的目的地是一个字节数组,只不过这个字节数组不需要我们创建传递,因为这个流底层封装了一个字节数组用于结束字节数据的输出代码示例:
// ByteArrayInputStream bais = new ByteArrayInputStream("中国加油".getBytes("UTF-8")); // byte[] buf = new byte[12]; // bais.read(buf); // System.out.println(new String(buf,"UTF-8")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write("中国加油".getBytes("UTF-8")); String string = baos.toString("UTF-8"); System.out.println(string);//中国加油
-
-
字符数组流
CharArrayReader
CharArrayWriter
-
-
文件流:连接的数据源是一个文件
-
字节流
-
FileInputStream
-
new FileInputStream(File)
-
new FileInputStream(String path)
-
【注意】:文件必须存在,不存在报错
-
-
FileOutputStream
-
new FileOutputStream(File|String)
—默认是覆盖写 -
new FileOutputStream(File|String,boolean append)
-
【注意】:文件可以不用提前存在,如果文件不存在会自动创建
FileInputStream、FileOutputStream:
什么文件都可以处理
代码示例:
FileOutputStream fos = new FileOutputStream("a.txt",true); fos.write("中国加油".getBytes()); fos.write("指鼠为鸭".getBytes()); System.out.println("执行结束");//执行结束
FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("wc.txt"); fos = new FileOutputStream("wc_new.txt"); int n; while((n = fis.read()) != -1) { if(n >=97 && n <=122) { n = n-32; } fos.write(n); } System.out.println("转换完成!");//转换完成! } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fis != null) { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
-
-
-
字符流
-
FileReader
new FileReader(String|File)
-
FileWriter
-
new FileWriter(File|String)
—默认是覆盖写 -
new FileWriter(File|String,boolean append)
-
写出数据之后,必须flush()刷新
FileReader、FileWriter:
只能处理纯文本文件
-
-
-
-
网络流
-
-
处理流:对节点流进行包装,提供一些更加强大的功能–处理速度快。
-
缓冲流:缓冲流包装另外一个流,缓冲流内部维护一个缓冲池,使用了缓冲流,一次读取多个字节、多个字符到缓冲流的内部的缓冲池当中。
-
字节缓冲流
BufferedInputStream
BufferedOutputStream
BufferedInputStream、BufferedOutputStream:
缓冲了一个8192的字节数组
-
字符缓冲流
BufferedReader
——提供了一个特殊的方法:readLine()
BufferedWriter
提供了一个特殊的方法:newLine()
不管是字节的还是字符的缓冲流,一定要flush
代码示例:
BufferedReader br = new BufferedReader(new FileReader("wc.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("wc_n1.txt")); String line = null; while((line = br.readLine()) != null) { line = line.toUpperCase(); bw.write(line); bw.newLine(); bw.flush(); } bw.close(); br.close(); System.out.println("输出完成");//输出完成
-
-
字节转字符流:以指定的编码集将一个字节流转换为字符流进行处理,加快我们的处理速度—纯文本类型的IO流有效
-
InputStreamReader(InputStream,charset)
-
OutputStreamWriter(OutputStream,charset)
代码示例:
// FileOutputStream fos = new FileOutputStream("demo.txt"); // fos.write("中国加油".getBytes("UTF-8")); // fos.close(); // System.out.println("写出完成"); FileInputStream fis = new FileInputStream("demo.txt"); InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); BufferedReader br = new BufferedReader(isr); String line = null; while((line = br.readLine()) != null) { System.out.println(line);//中国加油 }
-
-
对象流–只有字节流
-
Java中的序列化(Java对象转换成二进制编码)和反序列化(Java二进制编码转换为Java对象)机制的问题
-
序列化和反序列化需要用到Java的两个IO流
ObjectInputStream
ObjectOutputStream
-
我们的Java对象如果想要序列化,那么必须得声明它能序列化,Java中对象默认不具备序列化的能力。如果想要具备,那么必须实现两个接口其中一个即可,序列化就是把对象的属性值转换称为二进制
- Serializable:static或者transient修饰的属性不会序列化
- Externalizable:必须重写两个方法,一个序列化写出的方法,一个反序列化读取的方法
- 序列化版本id,是为了告诉编译器,序列化的二进制和我们当前项目中的类是否为同一个版本,如果是不同的版本,那么报错
-
-
打印流–只有输出流
-
PrintStream
-
PrintWriter
PrintStream、PrintWriter:
提供了一系列重载的print和println方法用于输出数据
代码示例:
PrintStream ps = new PrintStream(new FileOutputStream("a.txt")); ps.println("zs"); ps.println(12); ps.flush(); ps.close(); System.out.println("zs"); System.setOut(new PrintStream(new FileOutputStream("a.txt"))); System.out.println("ls");
-
-
Java中标准输入和标准输出流
方法名 System.in System.out System.setIn(InputStream) System.setOut(OutputStream) -
序列化
代码示例:
/** * 序列化的代码 */ // Student s = new Student("zs", 20, "男"); // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.txt")); // oos.writeObject(s); // oos.flush(); // oos.close(); // System.out.println("对象序列化完成");//对象序列化完成 /** * 反序列化的代码 */ ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.txt")); Object object = ois.readObject(); System.out.println(object);//Student [studentName=zs, studentAge=20, studentSex=null]
package com.sxuek.study.serialize; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.io.Serializable; import java.util.Objects; /** * model/pojo/entries/vo/po * JavaBean类:Java很干净的类称之为JavaBean * 私有化的属性、公开的getter/setter、hashCode、equals、toString、构造器 * @author 11018 * */ public class Student implements Externalizable{ private static final long serialVersionUID = 1L; private String studentName; private transient Integer studentAge; private String studentSex; public Student() { super(); } public Student(String studentName, Integer studentAge, String studentSex) { super(); this.studentName = studentName; this.studentAge = studentAge; this.studentSex = studentSex; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Integer getStudentAge() { return studentAge; } public void setStudentAge(Integer studentAge) { this.studentAge = studentAge; } public String getStudentSex() { return studentSex; } public void setStudentSex(String studentSex) { this.studentSex = studentSex; } @Override public int hashCode() { return Objects.hash(studentAge, studentName, studentSex); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; return Objects.equals(studentAge, other.studentAge) && Objects.equals(studentName, other.studentName) && Objects.equals(studentSex, other.studentSex); } @Override public String toString() { return "Student [studentName=" + studentName + ", studentAge=" + studentAge + ", studentSex=" + studentSex + "]"; } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(studentName); out.writeInt(studentAge); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { studentName = in.readUTF(); studentAge = in.readInt(); } }
-
-
2.3 方法递归–慎用
方法迭代其实就是一种循环,只不过这个循环比较特殊,循环我们只知道循环的终止条件,而循环的次数我们是不太清楚的
使用方法:
- 将需要重复性执行的代码抽取到一个方法中
- 方法中需要对重复性执行的代码进行改造,有一个递归入口(自己调用自己的一个逻辑)、递归出口
代码示例
/**
* 使用方法递归计算∑100
* @author 11018
*
*/
public class RecursionStudy {
public static void main(String[] args) {
System.out.println(sum(100));
}
public static int sum(int num) {
if(num == 1) {
return 1;
}else {
return num+sum(num-1);
}
}
}
三、Java注解Annotation
注解也是Java中一种比较特殊的存在,一般可以声明在任何一个位置,用于给我们的代码提供一些说明或者提供一些功能。@Override @Deprecated
注解使用一般分为三种情况的注解
-
只是用来进行解释说明的注解,这个注解没有多余的操作代码。
-
在我们编译时期,可以根据注解生成多余的class二进制代码文件需要借助一些插件才能完成
-
在运行时期,我们根据注解对代码进行相关的修正或者赋值操作Java的反射机制进行操作
注解可以声明在Java代码的任何一个位置。注触我们也可以自己声明一个
语法:
@元注解 访问控制修饰符 @interface 注解名{ 属性类型 属性名() default 默认值; }
代码示例:
@MyAnno("ls")
private String name;
@MyAnno("zs")
public String test(@MyAnno("zs")int age) {
return "";
}
元注解是Java提供的用来修饰注解的注解,用来表示我们自己声明的注解可以在哪些位置声明,可以在什么时期存在,能不能被子类继承,或者说注解能不能被JavaDOC文档识别
注解名称 | 注解说明 |
---|---|
@Target | 注解声明在哪些位置 |
@Retention | 声明注解可以在什么时期存在 |
@Inherited | 子类可以继承父类的注解 |
@Documented | JavaDOC文档注释可以识别这个注解 |
代码示例:
@Target({ElementType.METHOD,ElementType.PARAMETER,ElementType.FIELD})
@Retention(RetentionPolicy.CLASS)
@Inherited
@Documented
public @interface MyAnno {
String value();
}
注解中可以有一个属性,也可以有多个属性,如果只有一个属性,建议属性名为value,因为这样的话使用注解时可以不用写属性名,否则必须声明属性名
Lombok插件
lombok插件就是基于注解帮助我们快速生成JavaBean类的一个工具,工具中提供了一大堆注解,注解可以帮助我们Java代码在编译时期生成JavaBean的方法和构造器等内容
lombok插件使用前需要两步操作
1、在项目引入lombok的第三方工具jar包—在项目中能找到lombok的相关注解代码
2、还必须在eclipse中安装一个lombok的编译插件。
lombok提供了一大堆注解用来生成JavaBean
注解名称 |
---|
@Data |
@ToString |
@Getter |
@Setter |
@EqualsAndHashCode |
@NoArgsConstructor |
@AllArgsConstructor |
@Builder |
代码示例:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Student {
private String name;
private Integer age;
}
Student s = new Student();
Student s1 = new Student("zs",20);
s.setName("ls");
System.out.println(s);
Student student = Student.builder().name("zs").build();