(一)流的定义
流是程序和设备之间嫁接起来用于数据传输的一根管道,该管道上有许多按钮用于实现不同的功能,而这根管道就是流。(流是可以实现输入输出的类,即流一定是类但类不一定是流)
(二)流的分类
1、按数据流的方向不同,可分为输出流、输入流;
2、按处理数据单位不同,可分为字节流、字符流;
3、按功能不同,可分为节点流(原始流)、处理流(包裹流);
(三)四大基本抽象流
注:1、顾名思义InputStream和OutputStream读写数据的单位是一个字节,Reader和Writer读写数据的单位是一个字符(Java中一个字符占两个字节);
2、InputStream、OutputStream、Reader、Writer这四个类都是抽象类;
3、输入是从设备中读取写入程序中,输出是从程序中读取写入设备中;
(四)常用方法
InputStream流常用方法
1、public int read(); throws IOException
读取一个字节并以整型的形式返回,如果读取到输入流末尾则返回-1;
import java.io.*;//导入流的包
public class TestRead_1 {
public static void main(String[] args) throws Exception {
int data;
FileInputStream bis=new FileInputStream("D:\\JAVA\\lianxi\\src\\lianxi\\TestRead_1.java");
//FileInputStream是InputStream的子类为文本流,其中该地址是本人该程序中的路径,需要更改成自己电脑的程序路径
data=bis.read();
while(-1 != data) {
System.out.printf("%c",data);
data=bis.read();
}
bis.close();//使用流必须调用的方法,关闭此输入流并释放与该流相关的所有系统资源
}
}
2、public int read(byte[] b) throws IOException
从输入流中读取一定数量的字节,并将其储存在缓冲区数组b中,以整数形式返回实际读取的字节数;
如果b中长度为0,便不读取任何字节,并返回0;
如果因为流位于文件末尾而没有可用字节,则返回-1;、
public class TestRead_2{
public static void main(String[] args)throws Exception {
FileInputStream fr=new FileInputStream("D:/JAVA/lianxi/src/lianxi/liu1.java");//FileInputStream是InputStream的子类为文本流,其中该地址是本人该程序中的路径,需要更改成自己电脑的程序路径
int ch;
byte[] b=new byte[1024];//创建一个byte类型数组
ch=fr.read(b);//返回实际读取的字节数
System.out.println(ch);
while(ch!=-1) {
ch=fr.read();//返回-1
System.out.println(ch);
}
fr.close();
}
}
//输出:该程序实际字节数
// -1
3、public int read(byte[] b,int off,int len); throws IOException
从下标off的位置开始读取最多len个字节数据储存于b数组缓冲区;
返回实际读取的字节数以整数形式返回,读取到输入流末尾则返回-1;
public class TestRead_3{
public static void main(String[] args)throws Exception {
FileInputStream fr=new FileInputStream("D:/JAVA/lianxi/src/lianxi/liu1.java");//FileInputStream是InputStream的子类为文本流,其中该地址是本人该程序中的路径,需要更改成自己电脑的程序路径
int ch;
byte[] b=new byte[1024];//创建一个byte类型数组
ch=fr.read(b,0,1000);//返回实际读取的字节数
System.out.println(ch);
while(ch!=-1) {
ch=fr.read();//返回-1
System.out.println(ch);
}
fr.close();
}
}
//输出:该程序实际字节数
// -1
4、void close(); throws IOException
关闭此输入流并释放与此输入流相关的所有系统资源;
OutputStream流常用方法
1、void write(int b); throws IOException
向输出流中写入一个字节数据;
import java.io.*;
public class TestWrite_1{
public static void main(String[] args)throws Exception {
FileInputStream fr=new FileInputStream("D:/JAVA/lianxi/src/lianxi/liu1.java");//FileInputStream是InputStream的子类为文本流,其中该地址是本人该程序中的路径,需要更改成自己电脑的程序路径
FileOutputStream fo=new FileOutputStream("C:\\Users\\86152\\Desktop\\suibian.doc");//路径是写入的设备的地址,其中这是本人桌面的建立的一个doc文档
int i;
i=fr.read();
while(-1!=i) {
fo.write(i);
i=fr.read();
}
fo.flush();//将输出流的缓冲流数据全部写出到达目的地
fo.close();
fr.close();
}
}
输出结果:
2、void write(byte[] b); throws IOException
将一个字节类型的数组中的数据写入输出流;
public class TestWrite_2 {
public static void main(String[] args)throws Exception {
FileInputStream fr=new FileInputStream("D:/JAVA/lianxi/src/lianxi/liu1.java");
FileOutputStream fo=new FileOutputStream("C:\\Users\\86152\\Desktop\\suibian.doc");
int i;
byte[] b=new byte[1024];
i=fr.read(b,0,1024);
while(-1!=i) {
fo.write(b);
i=fr.read(b,0,1024);
}
fo.flush();
fo.close();
fr.close();
}
}
3、void write(byte[] b,int off,int len); throws IOException
将一个字节类型的数组从指定位置off(下标)开始写入len个字节到输入流;
public class TestWrite_3 {
public static void main(String[] args)throws Exception {
FileInputStream fr=new FileInputStream("D:/JAVA/lianxi/src/lianxi/liu1.java");
FileOutputStream fo=new FileOutputStream("C:\\Users\\86152\\Desktop\\suibian.doc");
int i;
byte[] b=new byte[1024];
i=fr.read(b,0,1024);
while(-1!=i) {
fo.write(b,0,1024);
i=fr.read(b,0,1024);
}
fo.flush();
fo.close();
fr.close();
}
}
4、void close(); throws IOException
关闭此输入流并释放与此输入流相关的所有系统资源;
5、void flush(); throws IOException
将输出流的缓冲流数据全部写出到达目的地
Reader流常用方法
1、pubic int read(); throws IOException
读到一个字符并以整数的形式返回(0~255),如果到输入流末尾则返回-1;
import java.io.*;
public class TestRead_4 {
public static void main(String[] args) throws Exception {
int data;
File bis=new FileRead("D:\\JAVA\\lianxi\\src\\lianxi\\TestRead_4.java");
data=bis.read();
while(-1 != data) {
System.out.printf("%c",data);
data=bis.read();
}
bis.close();
}
}
2、public int read(char[] buf); throws IOException
读取一系列字符并储存到buf数组,返回实际读取的字符数,如果读取前已经到输入流的末尾则返回-1;
public class TestRead_5{
public static void main(String[] args)throws Exception {
FileInputStream fr=new FileInputStream("D:/JAVA/lianxi/src/lianxi/TestRead_5.java");
int ch;
char[] buf=new char[1024];
ch=fr.read(buf);
System.out.println(ch);
while(ch!=-1) {
ch=fr.read();
System.out.println(ch);
}
fr.close();
}
}
3、public int read(char[] buf,int off,int len); throws IOException
从off下标开始读取最多len个字符储存到buf字符数组中;
返回实际读取的字符数,如果读取前已经到输入流末尾,则返回-1;
public class TestRead_6{
public static void main(String[] args)throws Exception {
FileReader fr=new FileReader("D:/JAVA/lianxi/src/lianxi/liu1.java");
int ch;
char[] buf=new char[1024];
ch=fr.read(buf,0,1000);
System.out.println(ch);
while(ch!=-1) {
ch=fr.read();
System.out.println(ch);
}
fr.close();
}
}
4、public void close(); throw IOException
关闭流释放内存;
Writer流常用方法
1、public void write(int i); throws IOException
向输入流写入一个字符数据;
import java.io.*;
public class TestWrite_4{
public static void main(String[] args)throws Exception {
FileReader fr=new FileReader("D:/JAVA/lianxi/src/lianxi/liu1.java");
FileWriter fo=new FileWriter("C:\\Users\\86152\\Desktop\\suibian.doc");
int i;
i=fr.read();
while(-1!=i) {
fo.write(i);
i=fr.read();
}
fo.flush();
fo.close();
fr.close();
}
}
2、public int write(char[] buf); throws IOException
将一个字符类型的数组中的数据写入输出流;
public class TestWrite_5 {
public static void main(String[] args)throws Exception {
FileReader fr=new FileReader("D:/JAVA/lianxi/src/lianxi/liu1.java");
FileWriter fo=new FileWriter("C:\\Users\\86152\\Desktop\\suibian.doc");
int i;
char[] buf=new char[1024];
i=fr.read(buf,0,1024);
while(-1!=i) {
fo.write(buf);
i=fr.read(buf,0,1024);
}
fo.flush();
fo.close();
fr.close();
}
}
3、public int write(char[] buf,int off,int len); throws IOException
将一个字符串从下标off开始最多len个字符写入输出流中;
public class TestWrite_6 {
public static void main(String[] args)throws Exception {
FileReader fr=new FileReader("D:/JAVA/lianxi/src/lianxi/liu1.java");
FileWriter fo=new FileWriter("C:\\Users\\86152\\Desktop\\suibian.doc");
int i;
char[] b=new char[1024];
i=fr.read(b,0,1024);
while(-1!=i) {
fo.write(b,0,1024);
i=fr.read(b,0,1024);
}
fo.flush();
fo.close();
fr.close();
}
}
4、void close(); throws IOException
关闭此输入流并释放与此输入流相关的所有系统资源;
5、void flush(); throws IOException
将输出流的缓冲流数据全部写出到达目的地