流的概念:
- 流(Stream )的概念代表的是程序中数据的流通
- 数据流是一串连续不断的数据的集合
- 在Java程序中,对于数据的输入/输出操作是以流(Stream)的方式 进行的
输入流 — 流入程序的数据
输出流 — 流出程序的数据
在java程序中,从输入流读取数据(读到内存中),而从输出 流输出数据(从内存存储到文件或显示到屏幕上)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190905231732258.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzQ0MTc5MA==,size_14,color_FFFFFF,t_70,pic_center%3E)
流的分类
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190905232807782.png)
字节流输入流,InputStream常用的方法
方法 | 含义 |
---|
int read( ) | 一次读取一个byte的数据,并以int类型把数据返 回来,如果没有数据可以读了,会返回”-1” |
int read(byte[] buffer) | 把所读取到的数据放在这个byte数组中,返回一 个int型的数据,这个int型数据存储了返回的真正读取到的数据byte数 |
int read(byte[] buffer,int offset,int length) | 读取length个字节,并存储到一个字节数组buffer 中,并从offset位置开始返回实际读取的字节数 void close( ) 关闭此输入流并释放与该流关联的所有系统资源 |
void close( ) | 关闭此Reader并释放与其关联的所有系统资源 |
字节流输出流,OutputStream常用的方法
方法 | 含义 |
---|
void write(byte[] buffer) | =将要输出的数组先放在一个byte数组中,然后用 这个方法一次把一组数据输出出去 |
void write(byte[] buffer,int off,int len) | 将指定字节数组中从偏移量 off 开始的 len 个字节 写入此输出流 |
abstract void write(int b) | 将指定的字节写入此输出流 |
void close( ) | |
void flush( ) | 刷新此输出流并强制写出所有缓冲的输出字节 |
字符输入流,Reader常用的方法
方法 | 含义 |
---|
int read( ) | 一次读取一个char的数据,并以int类型把数据返 回来,如果没有数据可以读了,会返回”-1” |
int read(char[] cbuffer) | |
int read(char[] cbuffer,int offset,int length) | 读取length个字符,并存储到一个字节数组 cbuffer中,并从offset位置开始返回实际读取的 字符数 |
void close( ) | 关闭此Reader并释放与其关联的所有系统资源 |
字符输出流,Writer常用的方法
方法 | 含义 |
---|
void write(char[] cbuffer) | 将要输出的数组先放在一个char数组中,然后用这 个方法一次把一组数据输出出去 |
void write(char[] cbuffer,int off,int len) | 将指定字符数组中从偏移量 off 开始的 len 个字符 写入此输出流 |
int write(int b) | 将指定的字符写入此输出流 |
void write(String str) | 写入字符串 |
void write(String str, int off,int len) | 将指定字符串中从偏移量 off 开始的 len 个字符写 入此输出流 |
void close( ) | 关闭此输出流并释放与此流有关的所有系统资源 void flush( ) 刷新此输出流并强制写出所有缓冲的输出字符 |
字节输入流Demo
package com.neuedu.chapter03._文件管理Java7特性;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class FileOutputStream_FileInputSteream_Demo {
public static void main(String[] args) {
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream("file/file1.txt");
output = new FileOutputStream("file/file_back.txt");
byte[] b = new byte[6];
int length = -1;
while((length=input.read(b)) != -1) {
System.out.println(length+"------"+Arrays.toString(b));
output.write(b,0,length);
output.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
}
}
}
字符输入流Dem
package com.neuedu.chapter03._文件管理Java7特性;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
public class FileReader_FileWriter_Demo {
public static void main(String[] args) {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader("file/file1.txt");
writer = new FileWriter("file/file_back.txt");
int cotent = reader.read();
System.out.println(cotent);
cotent = reader.read();
System.out.println(cotent);
cotent = reader.read();
System.out.println(cotent);
cotent = reader.read();
System.out.println(cotent);
char[] c = new char[5];
int content;
while((content = reader.read(c))!=-1) {
System.out.println(content+"--------"+Arrays.toString(c));
writer.write(c,0,content);
}
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}