15.IO流
1 Annotation: 注解
-
活在哪:@Retention(RetentionPolicy.RUNTIME) jvm运行时; RetentionPolicy.CLASS存活在.class文件中 ; RetentionPolicy.SOURCE存活源码中
源码中— javac 时编译 —.class ---- jvm
-
修饰什么: @Target(ElementType.FIELD):属性 ; ElementType.METHOD ;
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface NotBlank {
String message() default "不为空";
}
定义:@interface:
public @interface A {
// 有返回值的抽象方法
String value();
String message();
}
@A 修饰所有的: 类上,方法上,属性上,参数上。。。
可以通 @Target(ElementType.FIELD|TYPE|METHOD|CONSTRUCTOR)限制。
存活的地方: @Retention(RetentionPolicy.??)
1. source中 2.class中 3.runtime: jvm中
注解一般和 反射一起用
通过反射能够获取注解。获取注解方法的值
2 字节流
InputStream抽象类:
- int available(); 返回流中还有多少个字节可读, 读完返回0
- close() ; 关闭流
- mark(int); 标记位置,
- reset(); 回到mark位置
- int read(); 每次读一个字节
- int read(byte[] b) ; 把流读入到 byte[]中, 返回值是读了几个字符
// try (InputStream is = new FileInputStream(new File("D:\\abc.txt"));) {
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
InputStream is = null;
try {
is = new FileInputStream(new File("D:\\abc.txt"));
byte[] buffer = new byte[5];
while (is.available() != 0) {
// System.out.println(is.read()); // 一次读一个好累
int len = is.read(buffer); // len※
System.out.print(new String(buffer, 0, len));
}
System.out.println();
is = new FileInputStream(new File("D:\\abc.txt"));
int len;
while ((len = is.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, len));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
OutputStream:
- close();
- flush(); 所有的输出流关闭之前要 推送缓存
- write(byte [] ); 写入数组
- write(byte [] buffer, 0, len ); 写入数组
package com.etc.lesson15;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TestMain3 {
public static void main(String[] args) {
String str = "水水水水水\r\njakld";
try (OutputStream os = new FileOutputStream("D://abc1.txt", true)) {
byte[] buffer = str.getBytes();
os.write(buffer);
os.flush();
System.out.println("文件完事了");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3. 对象流:
对象 变成 某种数据格式 -------可能通过网络-----------接收某种数据个数,再变回对象
对象 与 某种数据格式 的转换叫做序列化和反序列化
- Serializable: 可序列化的, 标记性接口没有方法。(Cloneable)
- 常量serialVersionUID: 用于版本标记
- transient: 不参与序列化属性
- writeObject(Object o); 写入对象
- readObject(); 读对象
package com.etc.lesson15;
import java.io.Serializable;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp implements Serializable {
/**
* 序列化版本号
*/
private static final long serialVersionUID = 1L;
private String ename;
private String job;
private String empno;
// transient不参与序列化
private transient double sal;
private LocalDate hirdate;
}
package com.etc.lesson15;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDate;
public class TestMain5 {
public static void main(String[] args) {
Emp emp = new Emp("SMITH", "MANAGER", "121321", 3.1, LocalDate.now());
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\obj.data"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\obj.data"));) {
oos.writeObject(emp);
oos.flush();
// 反序列化
Object o = ois.readObject();
if (o instanceof Emp) {
Emp emp1 = (Emp) o;
System.out.println(emp1);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4. 字符流:InputStreamReader/OutputStreamReader
InputStreamReader构造方法可以指定 charset: UTF-8|GBK…
InputStreamWriter构造方法可以指定 charset: UTF-8|GBK…
package com.etc.lesson15;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class TestMain7 {
public static void main(String[] args) {
// 装饰模式:
// 包装模式: 包装 字节流, 变成字符流
OutputStreamWriter osw = null;
InputStreamReader isr = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(new File("D:/abc.txt"), true), "GBK");
osw.write("你好");
osw.flush();
isr = new InputStreamReader(new FileInputStream(new File("D:/abc.txt")), "UTF-8");
BufferedReader br = new BufferedReader(isr);
while (br.ready()) {
System.out.println(br.readLine());
}
} catch (UnsupportedEncodingException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
osw.close();
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}