io流
概念
把不同的输入输出源(键盘,盘符文件,网络)抽象为流,通过流的方式去访问数据,也叫数据流
流的特点
1)流是有序的
2)流是先进先出的
3)流可以控制数据的流动方向
4)流是可以关闭的
5)流的作用主要是用来读取数据和写入数据的
2流的分类
根据流动方向: 输入流 输出流
根据数据单位: 字节流 字符流
根据功能 : 节点流 处理流
2.1 java封装了4个流的基类
输入流 inputStream Reader
输出流 OutputStream Writer
字节流 字符流
3字节流
1)字节流可以处理所有的数据文件: 图片,视频文件 纯文本文件
3.1inputStream
1)字节输入流,主要用来读取字节数据
2)英文在不同的字符编码集中都是一个字节,在gdk中占两个字节,utf—8中中文占三个字节
3)inputStream是个抽象类,如果想用流去处理文件,可以使用它直接子类FileinputStream
###3.1.2方法
package test.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IoTest {
public static void main(String[] args) throws IOException {
InputStream inputStream=new FileInputStream(new File("a.txt"));
//创建一个缓存区对象
byte[] buf=new byte[2];
int len;//长度
//read(buf)方法会将一次性读取到的多个字符,
//存储在参数数组中,并且返回读取到的个数
while((len=inputStream.read(buf))!=-1){
String string=new String(buf,0,len);
System.out.print(string);
}
//关闭流
inputStream.close();
}
//关闭流close()所有的流一旦创建成功
//,就相当于开启了就,在使用流结束后,需要关闭流,否怎会资源浪费
}
3.2OutputStream
- 字节输出流,可以写任何文件的数据
2)通常如果操作的是文件,使用它的子类FileOutputStream来创建对象
package test.io;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class IoTest2 {
public static void main(String[] args) throws IOException {
//如果没有该文件,会自动创建该文件
//OutputStream outputStream=new FileOutputStream(new File("b.txt));
OutputStream outputStream=new FileOutputStream("b.txt",true);//ture是用来决定每一次是覆盖写入还是在后面加着写入
//写
outputStream.write('a');
//outoutStream.write('中');
byte[]bs={'a','b','c'};
byte[] bs1="我爱中国".getBytes();
outputStream.write(bs1);
outputStream.write(bs1,0,8);
//关闭
outputStream.close();
}
}
案例:
将a文件中的数据复制到b文件中去
package test.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Test3 {
public static void main(String[] args) throws IOException {
//创建读取的流和写入的流
InputStream reader=new FileInputStream("a.txt");
OutputStream wirter=new FileOutputStream("b.txt");
//读取
byte[] bs=new byte[12];
int len;
while((len=reader.read(bs))!=-1){
wirter.write(bs,0,len);
}
reader.close();
wirter.close();
}
}
4 字符流
1)可以操作字符的流,比如纯文本的文件
2)ReaderWeiter都是抽象类
4.1Reader
package test.io;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Test4 {
public static void main(String[] args) throws IOException {
Reader reader=new FileReader("a.txt");
//将一次性读取多个字符,存储在缓冲区
char[] buf=new char[7];
int len;
while((len= reader.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
//关闭
reader.close();
}
}
4.2 writer
package test.io;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Test5 {
public static void main(String[] args) throws IOException {
Writer writer=new FileWriter("c.txt");
//关闭
writer.write("a");
writer.write("中");
writer.write("天气不错".toCharArray());
//存入换行
writer.write("\n臧村花");
writer.write("asdfs".toCharArray(),0,3);
writer.write("absf",0,3);
//将缓存区中的数据刷新到文件中,尽量不要用的太频繁
writer.flush();
//关闭流之前,会将所有缓存区中数据全部刷新到文件中,即close之前会执行fluse操作
writer.close();
}
}
5 缓冲流
作用:提高读写效率
字节流缓冲区:BufferedInputStream BufferedOutputStream
字节流缓冲流: BufferedReader BufferedWriter
是可以处理流的流,也叫包装流,高级流 处理流
5.1 BufferedInputStream
package test.io;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Test6 {
public static void main(String[] args) throws IOException {
InputStream in=new FileInputStream("a.txt");
BufferedInputStream inputStream=new BufferedInputStream(in);
byte[] buf=new byte[1024];
int len;
while((len=inputStream.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
inputStream.close();
}
}
5,2 BufferedOutputStream
BufferedOutputStream outputStream = new BufferedOutputStream(new
FileOutputStream("d.txt"));
outputStream.write("我爱中国".getBytes());
outputStream.close();
5.3BufferedReader
BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
String data;
// readLine()方法能一次性读取一行,即他会自行判断换行符,如果有换行符,则将前面的数据进行
返回
while ((data = reader.readLine()) != null) {
System.out.println(data);
}
reader.close();
5.4 BufferedWriter
BufferedWriter writer = new BufferedWriter(new FileWriter("e.txt"));
writer.write("千里冰封,万里雪飘");
// 换行符
writer.newLine();
writer.write("望长城内外,位于茫茫");
writer.flush();
writer.close();
案例:
package com.buffered;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("e.txt"));
String lineStr;
while ((lineStr = reader.readLine()) != null) {
System.out.println(lineStr);
writer.write(lineStr);
writer.newLine();
// 刷新
writer.flush();
}
reader.close();
writer.close();
}
}
6 转换流
1)将不好操作的字节流转变成好操作的字符流,即转换流是个字符流
2)没有将字符流转换成字节流的流
3)InputStreamReader OutputStreamWriter
4)作用:1)好操作 2)可以设置读取或者写的字符编码集
用法
InputStreamReader reader = new InputStreamReader(new
FileInputStream("a.txt"));
char[] cbuf = new char[1024];
int len;
while ((len = reader.read(cbuf)) != -1) {
System.out.println(new String(cbuf, 0, len));
}
reader.close();
案例
InputStreamReader reader = new InputStreamReader(new FileInputStream("a.txt"),
"gbk");
OutputStreamWriter writer = new OutputStreamWriter(new
FileOutputStream("f.txt"), "gbk");
char[] cbuf = new char[1024];
int len;
while ((len = reader.read(cbuf)) != -1) {
System.out.println(new String(cbuf, 0, len));
writer.write(cbuf, 0, len);
writer.flush();
}
reader.close();
writer.close();
InputStreamReader reader = new InputStreamReader(new FileInputStream("a.txt"),
"gbk");
BufferedReader reader2 = new BufferedReader(reader);
OutputStreamWriter writer = new OutputStreamWriter(new
FileOutputStream("f.txt"), "gbk");
BufferedWriter writer2 = new BufferedWriter(writer);
String lineStr;
while ((lineStr = reader2.readLine()) != null) {
writer2.write(lineStr);
writer2.newLine();
writer2.flush();
}
reader.close();
writer.close();
writer2.close();
reader2.close();
BufferedReader reader2 = new BufferedReader(new InputStreamReader(new
FileInputStream("a.txt"), "gbk"));
BufferedWriter writer2 = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream("f.txt"), "gbk"));
String lineStr;
while ((lineStr = reader2.readLine()) != null) {
writer2.write(lineStr);
writer2.newLine();
writer2.flush();
}
writer2.close();
reader2.close();
案例
键盘录入一些话,将这些话存储到文件中
public static void main(String[] args) throws IOException {
// 读取键盘录入的信息(字节信息)
InputStreamReader reader = new InputStreamReader(System.in);
7 流的标准格式
BufferedReader reader2 = new BufferedReader(reader);
// 写的流
BufferedWriter writer = new BufferedWriter(new FileWriter("g.txt"));
String lineStr;
// 每次调用readline方法,键盘录入的一行信息就存储到lineStr中了
// reader2.readLine()相当于一个阻塞方法
while ((lineStr = reader2.readLine()) != null) {
if (lineStr.equalsIgnoreCase("over")) {
break;
}
writer.write(lineStr);
writer.newLine();
writer.flush();
}
reader.close();
reader2.close();
writer.close();
7 流的标准格式
package com.buffered;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
// 读取键盘录入的信息(字节信息)
BufferedReader reader2 = null;
FileReader reader = null;
// 写的流
BufferedWriter writer = null;
try {
reader = new FileReader("h.txt");
reader2 = new BufferedReader(new InputStreamReader(System.in));
writer = new BufferedWriter(new FileWriter("g.txt"));
String lineStr;
// 每次调用readline方法,键盘录入的一行信息就存储到lineStr中了
// reader2.readLine()相当于一个阻塞方法
while ((lineStr = reader2.readLine()) != null) {
if (lineStr.equalsIgnoreCase("over")) {
break;
}
writer.write(lineStr);
writer.newLine();
writer.flush();
}
} catch (IOException e) {
1)在try上方定义流
2)在try中创建流对象和逻辑处理
3)在finally中关闭流,关闭流之前需要判断流是否是null
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (reader2 != null) {
reader2.close();
}
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("over");
}
}
1)在try上方定义流
2)在try中创建流对象和逻辑处理
3)在finally中关闭流,关闭流之前需要判断流是否是null