目录
内存流
此为节点流
ByteArrayInputStream:内存输入流
作用:将运行内存中的数据读取到程序中
ByteArrayOutputStream:内存输出流
作用:将程序中数据存储运行内存中
注意: 内存输入流自带内存默认初始大写为32字节,当不够时会自动扩容
特有方法:toByteArray():通过内存输出流获取其存储数据的数组
FileOutputStream fos = new FileOutputStream("x1.txt");//创建输出流
byte[] by01="".toString().getBytes();//创建by01
fos.write(by01);//将by01地内容输出到x1.txt
FileInputStream fis1= new FileInputStream("x1.txt");//创建输入流,将x1.txt内容输入进程序
ByteArrayOutputStream bos = new ByteArrayOutputStream();//默认大小32byte,创建内存输出流
byte[] by1 = new byte[1024];//创建工具
int len = 0;
// while((len=fis1.read(by1))!=-1) {//如果fis1读到得数据为-1说明读完
// String str = new String(by1,0,len);
// System.out.println(str);
//
// }
while((len=fis1.read(by1))!=-1) {//如果fis1读到得数据为-1说明读完
bos.write(by1,0, len);//将其全部输出到内存中
bos.flush();
}
byte[]array=bos.toByteArray();//获得其内存获得数据
String string=new String(array);//转为字符串
多个流先关输出在关输入
缓冲流
作用:提高读写效率
注意:过滤流(包装流)
BufferedInputStream
作用:提高读的效率
BufferedOutputStream
作用:提高写的效率
其原理是:字节传输数据相当于程序一部分一部分来回传输,而缓冲流相当于把其多个数据先打包到一起,一次性传输,从而减少了来回的次数
注意: 1,包装流在关闭时,会关闭他所包装的节点流
2,默认缓冲区为8kb
普通复制粘贴方法
FileInputStream fis = new FileInputStream("D:\\wordcout.txt");//创建输入流,将文件的东西输入到程序中
FileOutputStream fos = new FileOutputStream("D:\\wordcout2.txt");//创建输出流,将程序输出到文件中
byte[] b1=new byte[1024];
int len = 0;
while ((len=fis.read(b1))!=-1) {//
String str = new String(b1,0,len);
fos.write(b1, 0, len);//将其写入D:\\wordcout2.txt
fos.flush();
}
fos.close();
fis.close();
多个流先关输出在关输入
使用缓冲流
FileInputStream fis2 = new FileInputStream("D:\\wordcout.txt");//创建输入流,将文件的东西输入到程序中
FileOutputStream fos2 = new FileOutputStream("D:\\wordcout3.txt");//创建输出流,将程序输出到文件中
BufferedInputStream bs1= new BufferedInputStream(fis2);//创建缓冲输入流
BufferedOutputStream bo1 = new BufferedOutputStream(fos2);//创建缓存输出流
byte[] b2=new byte[1024];
int len1=0;
while ((len1=bs1.read(b2))!=-1) {
bo1.write(b2, 0, len1);
bo1.flush();
}
bs1.close();
bo1.close();
对象流
作用:读写对象
注意:过滤流(包装流)
ObjectInputStream
特有方法:
readObject():读取对象
注意:要保证文件中有对象
ObjectOutputStream
特有方法:
writeObject():写出对象
注意: 读写的对象所属的类必须实现序列化接口,包括对象的属性的数据类型也需要实现序列化
序列化: 让对象所属的类实现Serializable接口
注意: 1,八个基本数据类型包装类与String都已经实现了Serializable接口
2,不能序列化的属性有:
使用transient修饰的属性为瞬时属性,不参与序列化
使用static修饰的属性,不参与序列化
Student st1 = new Student("z",13);
Student st2 = new Student("m",12);
ArrayList<Student> list = new ArrayList<Student>();//使用Arraylist储存数据
list.add(st1);
list.add(st2);
FileOutputStream fo = new FileOutputStream("D:\\Student.txt");//输出流,程序输出到txt
FileInputStream fs= new FileInputStream("D:\\Student.txt");//txt输入流,读取到程序中
ObjectOutputStream oos = new ObjectOutputStream(fo);//输出流
oos.writeObject(list);//将对象写入D:\\Student.txt
oos.close();
ObjectInputStream ops = new ObjectInputStream(fs);//输入流
ArrayList<Student> list2 = new ArrayList<Student>();
list2=(ArrayList<Student>)ops.readObject();//将其输入到到list2
System.out.println(list2);
字符流
特点
传递的最小单位是char
不能传递音频视频图片等内容,只能传递文本
体系结构
InputStreamReader:从字节流到字符流的桥梁
OutputStreamWriter:从字节流到字符流的桥梁
Reader
提供的方法:
int read():一次读取一个字符,返回值就是读取到的字符,返回值-1表示读取结束
int read(char cbuf[]):一次读取一组字符到数组cbuf中,返回值为读取到的字符长度,-1时 表示结束
int read(char cbuf[], int off, int len):一次读取一组字符到数组cbuf中,从off位置 开始存储,存储len个.返回值为读取到的字符长度,-1表示结束
void close():关流
Writer
提供的方法:
void write(int c):一次写入一个字符
void write(char cbuf[]):一次写入一组字符
void write(char cbuf[], int off, int len):将cbuf中off位置开始,len个字符写入
void write(String str):将一个字符串写入
void write(String str, int off, int len):将一个字符串从off位置开始,len个长度的 字符写入 void flush():冲刷
void close():关流
BufferedWriter:
void newLine():写一行分隔符,行分隔符字符串由系统属性定义
BufferedReader:
public String readLine():读一行文字。结果包含行的内容的字符串,不包括任何终止字符,如果流的结尾已经达到,则为null
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\Student3.txt");//将程序输出到目标文件,输出流
OutputStreamWriter output = new OutputStreamWriter(fos);//输出流
FileInputStream fls = new FileInputStream("D:\\Student.txt");//输入流,将文件输入
InputStreamReader input = new InputStreamReader(fls);//输入流,将byet转为char
char[] ch=new char[1024];
int len=0;
while ((len=input.read(ch))!=-1) {
output.write(ch, 0, len);
output.flush();
String string=ch.toString();
System.out.println(string);
}
output.close();
input.close();
标准输出流
PrintWriter
PrintWriter pWriter = new PrintWriter("D:\\a.txt");
pWriter.print("你好,世界");
pWriter.close();
如果没有 a.txt ,则先创建,有的话写入"你好世界",每次写入会覆盖上次
PrintStream ps = System.out;
ps.println("hello a");
System.out.println("hello a");
编码格式
ASCAII:只有一些国家的文字
Unicode:万国码,在ASCAII上增加多国字符 一个字符2个字节
GBK:加了一些中文,在Unicode基础上增加中文
UTF-8:加了一些中文,在Unicode基础上增加中文UTF-8:对中文的支持比GBK好