Java的IO流详解
流的概念和作用
流是一组有顺序的,有始有终的字节集合,是对数据传输的总称或抽象。即数据的传输称为流。
IO流的分类
根据类型分为:字节流和字符流。
根据流向分为:输入流和输出流。
输入流和输出流的区别:
输入流:即把数据从持久化设配转移到内存中。
输出流:把内存中的数据转移到持久化设备中。
字节流和字符流的区别
字节流:可以操作任何数据,如MP4文件等。
字符流:只能操作纯字符数据
IO程序的书写流程
- 使用前,导入IO包中的类
- 使用中,进行异常处理。
- 使用后,关闭流释放资源
字节输出流与字节输入流的用法
字节输出流OutputStream
OutputStream是一个抽象类,是说有输出字节流的父类。
ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。
例:
public static void main(String[] args) throws IOException {
//步骤 1创建流 子类对象 绑定数据目的
FileOutputStream fos= new FileOutputStream("d:\\aaa.txt");
// 2 调用write() 方法 写一个字节
fos.write(97);
//2.1 写字节数组
byte[] b={65,66,67,68};
fos.write(b);
// 2.2 写字节数组的一部分
fos.write(b, 1, 2);
//2.3写字符串 getBytes() 字符串转字节
fos.write("hello world".getBytes());
// 3 close 关闭资源
fos.close();
}
字节输入流InputStream
InputStream 是所有的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。
例:
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("d:\\aaa.txt");
int len =0;
//定义read方法并while循环遍历出内容,每次读1字节
while((len=fis.read())!=-1){
System.out.print((char)len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
还可以以字节数组的形式读取,例:
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("c:\\aaa.txt");
// 创建字节数组
byte[] b = new byte[1024];
int len=0;
while((len=fis.read(b))!=-1){
//字节数组转字符串
System.out.println(new String(b,0,len));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
字节输入流与字节输出流的结合使用达到文件的复制效果
例:
public static void main(String[] args) {
//建立两个流对象 绑定数据源和目的地
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis= new FileInputStream("c:\\liu.mp4");
fos= new FileOutputStream("d:\\ma.mp4");
//字节输入流 读取一个字节 写一个字节
int len =0;
while((len=fis.read())!=-1){
fos.write(len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e);
throw new RuntimeException("文件复制失败");
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException("释放资源失败");
}
}
}
}
}
}
为了提升效率,可以转变为字节数组来进行。例:
public static void main(String[] args) {
long s = System.currentTimeMillis();
//
FileInputStream fis =null;
FileOutputStream fos=null;
try {
fis = new FileInputStream("c:\\liu.mp4");
fos = new FileOutputStream("d:\\liu.mp4");
// 定义字节数组
byte[] b = new byte[1024*10];
// 读取操作
int len = 0;
while((len=fis.read(b))!=-1){
fos.write(b,0,len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(fos!=null){
fos.close();
}
} catch (Exception e2) {
throw new RuntimeException("释放资源失败");
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("释放资源失败");
}
}
}
}
//获取文件复制所需的时间
long e = System.currentTimeMillis();
System.out.println(e-s);
}
字符流的用法
只有字符流才用flush()方法刷新。
字符输出流Writer
例:
public static void main(String[] args) throws IOException {
FileWriter fWriter = new FileWriter("d:\\1.txt");
fWriter.write(100);
fWriter.write(101);
char[] c = {'a','b','c','d'};
fWriter.write(c);
fWriter.write(c, 1, 2);
fWriter.write("我爱Java");
//刷新流
fWriter.flush();
//关闭流
fWriter.close();
}
字符输入流Reader
例:
public static void main(String[] args) throws Exception {
FileReader fileReader = new FileReader("c:\\1.txt");
// int len=0;
// while((len=fileReader.read())!=-1){
// System.out.print((char)len);
// }
char[] ch =new char[1024];// 一次读1024个字符
int len= 0;
while((len=fileReader.read(ch))!=-1){
System.out.println(new String(ch,0,len));
}
fileReader.close();
}
字符流实现复制
public static void main(String[] args) {
FileReader fileReader=null;
FileWriter file=null;
try {
fileReader = new FileReader("C:\\1.txt");
file = new FileWriter("d:\\1.txt");
char[] c = new char[1024];
int len = 0;
while((len=fileReader.read(c))!=-1){
file.write(c, 0, len);
file.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(file!=null){
try {
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fileReader!=null){
try {
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException("关闭失败");
}
}
}
}
}
}
字节流与字符流之间的转换
转换流OutputStreatWriter是字符流通向字节流的桥梁。例:
public static void main(String[] args) {
try {
//字节
FileOutputStream fos = new FileOutputStream("c:\\utf.txt");
OutputStreamWriter sow = new OutputStreamWriter(fos,"utf-8");
sow.write("您好");
sow.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}