一、定义
输入流:从文件或者网络中读取内容到内存中
输出流:从内存中写入内容到文件或者网络中
二、分类
流可以分为字节流、字符流
字节流:以字节从流中读取数据或者往流中写入数据,通常用来处理二进制文件,比如图像和声音。比较常用,也推荐使用。
基类为InputStream和OutputStream。
字符流:以字符从流中读取数据或者往流中写入数据,通常用来处理字符或者字符串数据。
基类为Reader和Writer。
字节流转换为字符流:java中提供了两个适配器,分别是InputStreamReader和OutputStreamWriter。
三、InputStream和OutputStream详解
InputStream:一个抽象类,不能实例化
InputStream is = new InputStream();//写法错误,因为抽象类无法实例化
正确写法如下:
File file = new File("D:\input.txt");
InputStream is = new FileInputStream(file);
主要功能:将数据一个字节一个字节的读到内存中
主要方法:
- int read(byte[] b):从当前位置从输入流中读取b.length个字节的数据存放到b中,实际读取到的数据要看输入流中的字节数,最多读取的数量就是b.length,返回值为实际读取的字节数。
- int read(byte[] b,int off,int len):从当前位置从输入流中读取len个字节的数据存放到b中,第一个字节存放的位置为b[off],第二个为b[off+1],依次类推,实际中读取的数据可能并没有len个字节,具体看输入流中还存在多少数据,返回值为实际读取的字节数。
- int read():从输入流中读取数据的下一个字节到java程序内部中,返回值为0到255的int类型的值,返回值为字符的ACSII值(如a就返回97,n就返回110),如果没有可用的字节,因为已经到达流的末尾,-1返回的值,运行一次只读一个字节,所以,它就经常与while((len =inputstream.read()) != -1)一起使用。
- int close():关闭此输入流并释放此输入流中占用的所有系统资源。
OutputStream:一个抽象类,不能实例化
OutputStream os = new OutputStream();//写法错误,因为抽象类无法实例化
正确写法如下:
File file = new File("D:\output.txt");
OutputStream os = new FileOutputStream(file);
主要功能:将数据一个字节,一个字节的写入到文件或者网络中
主要方法:
- void write(byte[] b):将字节数组b中的数据全部写入到输出流中,注意,仅仅是写入到流中,要写入到文件还需要调用flush()函数
- void write(byte[] b,int off,int len):将字节数组b中的数据,从off位置开始算起,写入len个字节到输出流,即将b[off], b[off+1]...b[off+len-1]这些字节写入到输出流中。
- void flush():刷新此输出流,并将该流中缓冲的字节数据全部写入到文件或者网络中
- void close():关闭输出流并释放该输出流占用的所有系统资源。
四、具体实例如下:
从指定文件中读取内容,并显示在控制台上
public static void read(){
String pathName = "D:/input.txt";
File file = new File(pathName);
InputStream is = null;
try{
is = new FileInputStream(file);
byte[] b = new byte[1024];
is.read(b);//测试输入输出流(后面还有很多空格)
System.out.println("读取的内容为:"+new String(b));//由于b字节数组长度为1024,如果文件较小,则会有大量填充空格
}catch(Exception e){
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
从指定文件中读取内容,并显示指定长度的内容到控制台,不包括多余的空格
public static void readLength(){
String pathName = "D:/input.txt";
File file = new File(pathName);
InputStream is = null;
try{
is = new FileInputStream(file);
byte[] b = new byte[1024];
int len = is.read(b);
System.out.println(new String(b,0,len));//测试输入输出流(不包含空格)
}catch(Exception e){
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
从指定文件中逐字节的读取文件内容,显示在控制台
public static void readByByte(){
InputStream is = null;
try{
File file = new File("D:/input.txt");
byte[] b = new byte[(int)file.length()];
is = new FileInputStream(file);
for (int i = 0; i < b.length; i++) {
b[i]=(byte)is.read();
}
System.out.println(new String(b));//测试输出输入流
}catch(Exception e){
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
向指定的文件中写入指定的字符串
public static void write(){
String pathName = "D:/output.txt";
File file = new File(pathName);
FileOutputStream fos = null;
try{
fos = new FileOutputStream(file);
String str = "Hello World!";
byte[] b = str.getBytes();
for(int i=0;i<b.length;i++){
fos.write(b[i]);
}
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
向指定的文件中写入某个文件中的内容
public static void writeFromFile(){
byte[] buffer = new byte[512];
int numberRead = 0;
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("D:/input.txt");
fos = new FileOutputStream("D:/output.txt");
while((numberRead = fis.read(buffer))!=-1){
fos.write(buffer, 0, numberRead);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
参考文章: