目录
1.1 InputerStream和OutputStream
1.2 FileInputStream和FileOutputStream类都是节点流
1.3 BufferedInputStream类和BufferedOutputStream类的使用与注意事项
2.1字节输入流与字符输出流的节点流与字节流在使用方法上大同小异,其主要区别如下:
2.2 转换流OutputStreamWriter和InputStreamReader
3.1 Properties 类是 Java 中用于处理配置文件的工具类,它继承自 Hashtable 类,实现了 Map 接口。
一、IO俩大流:字节流与字符流
1、字节流InputStream与OutputStream
1.1 InputerStream和OutputStream
InputerStream和OutputStream都是抽象类,是所有字节流的超类,他的常见子类有:FileInputStream(文件流),Buttered~(缓冲流),Data~(数据流),Object~(对数据进行序列化操作)
1.2 FileInputStream和FileOutputStream类都是节点流
1.2.1 FileInputStream
package com.io.FileInputStream;
import java.io.FileInputStream;
import java.io.IOException;
//读取文件中存在的数据
public class InputStream {
public static void main(String[] args) throws IOException {
//FileInputStream类的创建
FileInputStream fileInputStream = new FileInputStream("D:\\FileText\\Text.txt");
//缓存区buffter
byte[] buffer =new byte[8];
int length =0;
//read方法的使用
while ((length =fileInputStream.read(buffer))!=-1){
System.out.print(new String(buffer,0, length));
}
}
}
1.2.2 FileOutputStream
package com.io.FileOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class OutputStream {
public static void main(String[] args) throws IOException {
//创建可操作文件对象
File file = new File("D:\\FileText\\text.txt");
//true代表在文件后面添加
FileOutputStream outputStream = new FileOutputStream(file,true);
String str="hello,world";
//将字符串已字节流的形式写入文件中
outputStream.write(str.getBytes(),0,5);
}
}
1.3 BufferedInputStream类和BufferedOutputStream类的使用与注意事项
1.3.1 BufferedInputStream
package com.io.Buffered流的运用;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
//字节流读取文件
public class BufferedInputStream_ {
public static void main(String[] args) throws IOException {
String path="D:\\FileText\\text.txt";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
int len=0;
byte[] buffer=new byte[1024];
while ((len=bis.read(buffer))!=-1){
System.out.print(new String(buffer,0,len));
}
}
}
1.3.2 BufferedOutputStream
package com.io.Buffered流的运用;
import java.io.*;
public class BufferedOutputStream_ {
public static void main(String[] args) throws IOException {
String path="D:\\FileText\\text.txt";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path,true));
String str="你好";
bos.write(str.getBytes());
//flush刷新文档内容
//close=flush+关闭流
bos.flush();
}
}
1.4 对象流的使用(序列化和反序列化)
1.4.1 ObjectInputStream 注意:ObjectOutputStream类仅写入那些实现Serializable接口的对象。 这是因为对象在写入流时需要序列化。
package com.io.ObjectStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
//对象处理流序列化与反序列化的使用该字节流可以保存数据类型保存的数据必须实现Serializable接口或者Externalizable
public class ObjectInput {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String str="D:\\FileText\\Object.txt";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(str));
//读取顺序要与写入顺序一致否则会产生异常
System.out.println(ois.readBoolean());
System.out.println(ois.readChar());
System.out.println(ois.readUTF());
System.out.println(ois.readInt());
Dog dog=(Dog)ois.readObject();
System.out.println("dog信息:"+dog);
System.out.println("dog运行类型"+dog.getClass());
System.out.println(dog.getName());
System.out.println(dog.getAge());
System.out.println(dog.toString());
ois.close();
}
}
1.4.2 ObjectOutputStream
package com.io.ObjectStream;
import java.io.*;
public class ObjectOutput {
public static void main(String[] args) throws IOException {
String str="D:\\FileText\\Object.txt";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(str));
oos.writeBoolean(true);
oos.writeChar('H');
oos.writeUTF("你好世界");
oos.writeInt(100);
oos.writeObject(new Dog(18,"旺财"));
oos.flush();
oos.close();
}
}
2、字符输入流Reader和字符输出流Writer
2.1字节输入流与字符输出流的节点流与字节流在使用方法上大同小异,其主要区别如下:
数据类型:字节流以字节为单位进行读写,而字符流以字符为单位进行读写。字节流可以处理任意类型的数据,包括文本、图像、音频等,而字符流主要用于处理文本数据。
编码方式:字节流是以字节的形式直接读写数据,不关心数据的具体编码方式。而字符流是以字符的形式读写数据,会根据指定的字符编码将字符转换为字节进行处理。
处理效率:字节流的处理效率通常比字符流高,因为字节流直接操作底层的字节数据,不需要进行字符编码的转换。
使用场景:字节流适用于处理二进制数据,如文件的复制、网络传输等。字符流适用于处理文本数据,如文件的读写、文本的处理等。
二、IO流的分类
注:下表中带下划线的是抽象类,不能创建对象。粗体部分是节点流,其他就是常用的处理流。
流分类 | 使用分类 | 字节输入流 | 字节输出流 | 字符输入流 | 字符输出流 |
抽象基类 | InputStream | OutputStream | Reader | Writer | |
节点流 | 访问文件 | FileInputStream | FileOutStream | FileReader | FileWriter |
访问数值 | ByteArrayInputStream | ByteArrayOutStream | CharArrayReader | CharArrayWriter | |
访问管道 | PipedInputStream | PipedOutStream | PipedReader | PipedWriter | |
访问字符串 | StringReader | StringWriter | |||
处理流 | 缓冲流 | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
转换流 | InputStreamReader | OutputStreamWriter | |||
对象流 | ObjectInputStream | ObjectOutputStream | |||
抽象基类(过滤) | FilterInputStream | FilterOutputStream | FilterReader | FilterWriter | |
打印流 | PrintStream | PrintWriter | |||
推回输入流 | PushbackInputStream | PushbackReader | |||
特殊流 | DataInputStream | DataOutputStream |
2.1 Buffered的定义Buffered(缓冲流)也可以叫处理流,处理流的构造方法总是要带一个其它流对象做参数。一个流对象经过多次包装,称为流的链接。处理流很好地应用了设计模式中的装饰者模式,如上图所示
【1】对I/O进行缓冲是一种常见的性能优化,缓冲流为I/O流增加了内存缓冲区,增加缓冲区的两个目的:
(1)允许Java的I/O一次不只操作一个字符,这样提高整个系统的性能;
(2)由于有缓冲区,使得在流上执行skip、mark和reset方法都成为可能。
【2】缓冲流:它是要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,
提高了读写的效率,同时增加了一些新的方法。例如:BufferedReader中的readLine方法,
BufferedWriter中的newLine方法
2.2 转换流OutputStreamWriter和InputStreamReader
转换流主要是将字节流转换为字符流然后进行操作,对数据进行编码与解码,可以指定已哪种编码形式进行转换
package com.io.字节流转换为字符流;
import java.io.*;
//运用InputStreamReader和OutputStreamWriter进行字符流与字节流的转换还可以设定编码
public class InputStreamReader_ {
public static void main(String[] args) throws IOException {
String path="D:\\FileText\\text.txt";
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(path));
InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream,"utf-8");//指定编码格式
int len=0;
char[] buf=new char[8];
while ((len=inputStreamReader.read(buf))!=-1){
System.out.print(new String(buf,0,len));
}
}
}
三、Properties类的使用与解析
3.1 Properties 类是 Java 中用于处理配置文件的工具类,它继承自 Hashtable 类,实现了 Map 接口。
主要用于读取和写入属性文件,以键值对的形式存储数据。
配置文件通常以 .properties 为扩展名,其中每一行表示一个属性或配置项。
使用该类可以更好的处理文件:
配置文件管理: 主要用于读取和保存应用程序的配置信息,例如数据库连接信息、用户设置等。
国际化: 用于加载不同语言的资源文件,方便国际化应用程序。
持久化: 可以将配置信息持久化到文件,以便下次程序启动时重新加载。
3.2 Propreties类只能按照格式进行存放数据
Reader
package com.io.Properties类的使用;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Reader {
public static void main(String[] args) throws IOException {
//1.创建properties对象
Properties properties = new Properties();
//2.加载路径
properties.load(new FileInputStream("IOLearn\\src\\mysql.properties"));
//3.得到数据
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String name = properties.getProperty("name");
System.out.println("user:"+user);
System.out.println("password:"+password);
System.out.println("name:"+name);
}
}
Writer
package com.io.Properties类的使用;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
//向配置文件.properties中写入数据
public class Writer {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","123456");
properties.setProperty("name","张三");
properties.store(new FileOutputStream("IOLearn\\src\\mysql.properties"),null);
}
}