一,字节流
字节流的父类(抽象类)
InputStream:字节输入流
public int read(byte[] b,int off,int len){}
OutputStream:字节输出流
public void write(byte[] b,int off,int len){}
文件字节流
使用文件字节输出流写入文件
FileOutputStream:public void write(byte[] b)//一次写多个字节,将b数组中所有字节写出输出流。
public class TestFileOutputStream {
public static void main(String[] args) throws Exception{
//1创建流
FileOutputStream fos=new FileOutputStream("d:\\info.txt",true);
//2写入
String word="我爱java,java是世界上最好的语言\r\n";
for(int i=0;i<10;i++){
fos.write(word.getBytes());//写入硬盘
}
//3关闭
fos.close();
}
}
使用文件字节输入流读取文件
FileInputStream:public int read(byte[] b)//从流中读取多个字节,将读到内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1。
public class TestFileInputStream {
public static void main(String[] args) throws Exception{
//1创建文件字节输入流
FileInputStream fis=new FileInputStream("d:\\aaa.txt");
//2读文件
//2.1单个字节读取
// int data=0;
// while((data=fis.read())!=-1){
// System.out.print((char)data);
// }
//2.2读取多个字节
byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1){
//System.out.println("长度:"+len);
System.out.print(new String(buf,0,len));
}
//3关闭流
fis.close();
}
}
字节缓冲流
缓冲流:BufferedOutputSream/BufferedInputStream
作用:提高IO效率,减少访问磁盘的次数;
数据存储在缓冲区中,flush是将缓存区的内容写入文件中,也可以直接close。
创建缓冲字节输出流
public class TestBufferedOutputStream {
public static void main(String[] args) throws Exception{
//1创建缓冲字节输出流
FileOutputStream fos=new FileOutputStream("d:\\buffer.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
//2写入
String hello="我爱上海,我爱北京,我爱java\r\n";
for(int i=0;i<5;i++){
bos.write(hello.getBytes());//并没有直接写入硬盘,写入缓冲区
//字符串的getBytes()方法是得到一个操作系统默认的编码格式的字节
bos.flush();//刷新缓冲
}
//3关闭:默认刷新,并关闭节点流
bos.close();
// try (OutputStream ostream = fos) {
// System.out.println(".......");
//
// }
}
}
创建缓冲字节输入流
public class TestBufferedInpuStream {
public static void main(String[] args) throws Exception{
//1创建缓冲字节输入流
FileInputStream fis=new FileInputStream("d:\\aaa.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
//2读取
//2.1单个字节读取
// int data=0;
// while((data=bis.read())!=-1){
// System.out.print((char)data);
// }
//2.2读取多个字节
byte[] buf=new byte[1024];
int len=0;
while((len=bis.read(buf))!=-1){
System.out.print(new String(buf,0,len));
}
//3关闭,关闭缓冲流,自动关闭节点流
bis.close();//fis.close();
}
}
对象流(序列化,反序列化)
Address类
public class Address implements Cloneable{
private String city;
private String area;
public Address(String city, String area) {
this.city = city;
this.area = area;
}
public Address() {
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
@Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", area='" + area + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Person类
public class Person implements Externalizable,Cloneable {
//serialVersionUID
private static final long serialVersionUID=1000L;
private String name;
private transient int age;
public static String country="中国";
private Address add;
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("带参构造执行了");
}
public Person() {
System.out.println("无参构造执行了");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAdd() {
return add;
}
public void setAdd(Address add) {
this.add = add;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeObject(age);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name=(String)in.readObject();
age=(int)in.readObject();
}
@Override
public Object clone() throws CloneNotSupportedException {
Person p=(Person)super.clone();
p.setAdd((Address) add.clone());
return p;
}
}
SerializableDemo
/
* 把Person对象使用对象流写入硬盘中。序列化
* 要求:(1)序列化的类必须实现Serializable(自动序列化接口)或Externalizable(手动序列化接口)
* (2)序列化的类要添加一个私有的long类型静态常量:serialVersionUID,保证序列化的类反序列化的类是同一个类。
* 注意实现:
* (1)使用transient修饰的属性,不可以序列化
* (2)静态属性不能序列化
*
* 面试题:使用transient的属性一定不能序列化吗? 能 , 使用 Externalizable 实现。
*/
public class SerializableDemo {
public static void main(String[] args) throws Exception {
writeObject();
readObject();
}
/**
* 序列化
* @throws Exception
*/
public static void writeObject() throws Exception{
//1创建ObjectOutputStream
FileOutputStream fos = new FileOutputStream("d:\\person.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//2创建对象
Person zhuxi = new Person("铸玺", 20);
Person xiongfei = new Person("雄飞", 22);
Person.country="china";
ArrayList<Person> list=new ArrayList<>();
list.add(zhuxi);
list.add(xiongfei);
//3序列化
oos.writeObject(list);
//4关闭
oos.close();
System.out.println("序列化成功");
}
/**
* 反序列化
* @throws Exception
*/
public static void readObject() throws Exception{
//1创建对象输入流
FileInputStream fis=new FileInputStream("d:\\person.bin");
ObjectInputStream ois=new ObjectInputStream(fis);
//2反序列化
// Person person=(Person) ois.readObject();
// Person person2 = (Person) ois.readObject();
// System.out.println(person.toString());
// System.out.println(person2.toString());
ArrayList<Person> list=(ArrayList<Person>) ois.readObject();
//ArrayList<Person> list2=(ArrayList<Person>) ois.readObject();
for (Person person : list) {
System.out.println(person.toString());
}
System.out.println(Person.country);
//3关闭
ois.close();
}
}
二,字符流
字节流的父类(抽象类)
Reader:字符输入流
public int read(char[] b,int off,int len){}
public void write(char[] c){}
文件字符流
创建字符文件输出流
FileWriter:public void write(String str)//一次写多个字符,将b数组中所有字符写入输出流。
public class TestFileWriter {
public static void main(String[] args) throws Exception{
//1创建字符文件输出流
FileWriter fw=new FileWriter("d:\\writer.txt");
//2写入
for(int i=0;i<10;i++){
fw.write("我爱java,java真香\r\n");
fw.flush();
}
//3关闭
fw.close();
}
}
创建字符文件输入流
FileReader:public int read(char[] c)//从流中读取多个字符,将读到内容存入c数组,返回实际读到的字符数;如果达到文件的尾部,则返回-1;
public class TestFileReader {
public static void main(String[] args) throws Exception {
//1创建字符文件输入流
FileReader fr=new FileReader("d:\\aaa.txt");
//2读取
//2.1单个字符
// int data=0;
// while((data=fr.read())!=-1){
// System.out.print((char)data);
// }
//2.2读取多个字符
char[] buf=new char[1024];
int len=0;
while((len=fr.read(buf))!=-1){
System.out.print(new String(buf,0,len));
}
//3关闭
fr.close();
}
}
字符缓冲流
缓冲流:BufferedWriter/BufferedReader
作用:支持输入换行符
可一次写一行,读一行
创建字符缓冲输出流
public static void write() throws Exception{
//1创建字符缓冲输出流
FileWriter fw=new FileWriter("d:\\buffer.txt");
BufferedWriter bw=new BufferedWriter(fw);
//2写入文件
for(int i=0;i<5;i++){
bw.write("好好学习,天天向上");
//实现跨平台
bw.newLine();
//刷新
bw.flush();
}
//3关闭
bw.close();
System.out.println("写入完毕");
}
创建字符缓冲输入流
public static void read() throws Exception{
//1创建字符缓冲输出流
FileReader fr=new FileReader("d:\\buffer.txt");
BufferedReader br=new BufferedReader(fr);
//2读取
//读取一行
String data=null;
while((data=br.readLine())!=null){
System.out.println(data);
}
//3关闭
br.close();
}
转换流
桥转换流:InputStreamReader/OutputStreamWriter
作用:可以将字节流转换为字符流
可设置字符的编码方式
InputStreamReader:字节流通向字符流的桥梁
public static void read() throws Exception{
//1创建转换流
FileInputStream fis=new FileInputStream("d:\\trans.txt");
InputStreamReader isr=new InputStreamReader(fis,"utf-8");
//2读取
char[] buf=new char[1024];
int len=0;
while((len=isr.read(buf))!=-1){
System.out.print(new String(buf,0,len));
}
//3关闭
isr.close();
System.out.println();
}
OutPutStreamWriter:字符流通向字节流的桥梁
public static void write() throws Exception{
//1创建转换流
FileOutputStream fos=new FileOutputStream("d:\\trans.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK");
//2写入
for(int i=0;i<5;i++){
osw.write("上海是一个好地方\r\n");
osw.flush();
}
//3关闭
osw.close();
}
打印流
打印流:PrintStream:字节打印流,PrintWriter:字符打印流。
作用:封装了print()/println()方法,支持写入后换行。
PrintStream类型:System.out默认打印到控制台,重定向标准输出流
public class TestPrint {
public static void main(String[] args) throws Exception{
// //创建打印流
//PrintStream ps=new PrintStream("d:\\print.txt");
// PrintWriter ps=new PrintWriter("d:\\print.txt");
// //打印(原样打印)
// ps.println(97);
// //关闭
// ps.close();
// FileOutputStream fos=new FileOutputStream("d:\\abc.txt");
// fos.write(97);
// fos.close();
//System.out 默认输出到控制台
//System.out.println(97);
//重定向标准输出流
// System.setOut(new PrintStream("d:\\console.txt"));
// System.out.println(97);
//System.in
Scanner input=new Scanner(System.in);
// int data=System.in.read();
//System.in转成字符流
//InputStreamReader isr=new InputStreamReader(System.in);
//int data=isr.read();
//缓冲流
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String data=br.readLine();
System.out.println(data);
}
}
三,随机流
RandomomAccessFile即可以读取文件内容,也可以向文件中写入内容,但是和其他输入/输出流不同的是,程序可以直接跳到文件的任意位置来读写数据。
读写模式:r:只读 rw:读写
作用:快速定位数据,支持并发读写
方便获取二进制文件
public class TestRandomAccessFile {
public static void main(String[] args) throws Exception{
//write();
read();
}
public static void write() throws Exception{
//1创建随机读写文件类。 r read w write
RandomAccessFile raf=new RandomAccessFile("d:\\ran.txt", "rw");
//2写
raf.writeUTF("吴铸玺");
raf.writeInt(20);
raf.writeBoolean(true);
raf.writeDouble(180);
raf.writeUTF("雄飞");
raf.writeInt(18);
raf.writeBoolean(false);
raf.writeDouble(181);
//3关闭
raf.close();
}
public static void read() throws Exception{
//1创建创建随机读写文件类。
RandomAccessFile raf=new RandomAccessFile("d:\\ran.txt", "r");
//2读取
//跳过铸玺
//seek()从文件开头位置设置文件指针的偏移量
// raf.seek(24);
// raf.seek(24);
//跳过指定字节个数
raf.skipBytes(24);
//raf.skipBytes(24);
String name=raf.readUTF();
System.out.println(name);
int age=raf.readInt();
System.out.println(age);
boolean b=raf.readBoolean();
System.out.println(b);
double height=raf.readDouble();
System.out.println(height);
//3关闭
raf.close();
}
}
四,Properties和流相关的方法
public class TestProperties {
public static void main(String[] args) throws Exception{
//Properties和流相关的方法
//创建集合
Properties properties=new Properties();
//添加元素
properties.setProperty("username", "zhangsan");
properties.setProperty("password", "123");
properties.setProperty("address", "上海");
//--------1 list 遍历(输出到流)---------
// properties.list(System.out);
// PrintWriter pw=new PrintWriter("d:\\zhangsan.properties");
// properties.list(pw);
// pw.close();
//--------2 store 保存到文件---------------
//FileWriter fw=new FileWriter("d:\\zhangsan.properties");
FileOutputStream fos=new FileOutputStream("d:\\zhangsan.properties");
properties.store(fos, "用户信息");
fos.close();
//--------3 load 加载文件----------------
System.out.println("-----------------------------");
Properties properties2=new Properties();
//FileReader fr=new FileReader("d:\\zhangsan.properties");
FileInputStream fis=new FileInputStream("d:\\zhangsan.properties");
properties2.load(fis);
fis.close();
properties2.list(System.out);
}
}