IO概述
IO流用来处理设备之间的数据传输,上传文件和下载文件
按照数据流向
输入流 读入数据 输出流 写出数据
按照数据类型
字节流 字符流
什么情况下使用哪种流呢?
如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。 如果你什么都不知道,就用字节流
字符串与字节数组之间的转换
字符串-->字节数组
String x = sc.nextLine();
out.write(x.getBytes());
字节数组-->字符串
byte[] by = new byte[1024];
System.out.println(new String(by));
close作用
a:让流对象变成垃圾,这样就可以被垃圾回收器回收
b:通知系统释放与该文件相关的资源
FileInputAndOutputStream的方法
FileInputStream:
public int read():一次读取一个字节
FileInputStream in = new FileInputStream("a.txt");
int len;
while((len=in.read())!=-1){
System.out.print((char)len);
}
public int read(byte[] b):一次读取一个字节数组
FileInputStream in = new FileInputStream("a.txt");
byte[] by = new byte[1024];
int len;
while((len=in.read(by))!=-1){
System.out.println(new String(by,0,len));
}
public int read(byte[] b, int off, int len) :一次读取一个字节数组的一部分(这个方法在读操作中不太使用)
FileInputStream in = new FileInputStream("a.txt");
byte[] b = new byte[in.available()];
int len = 0;//表示成功读取的字节数的个数
int len1;
//String(byte[] bytes, int offset, int length)
while(len<in.available()){
System.out.println(in.available());//2482
//read(byte[] b,int off, int len)表示从输入流当中最多将len个字节的数据读取到一个byte数组当中
len1 = in.read(b, len, in.available() - len);
System.out.println(len1+"---"+in.available());//2482---0
System.out.println(new String(b,len,len1));
len += len1;
}
FileOutputStream
public void write(int b):写一个字节
FileOutputStream out = new FileOutputStream("a.txt");
out.write("97".getBytes());
public void write(byte[] b):写一个字节数组
FileOutputStream out = new FileOutputStream("a.txt");
byte[] bys={97,98,99,100,101};
out.write(bys);
public void write(byte[] b,int off,int len):写一个字节数组的一部分
FileOutputStream out = new FileOutputStream("a.txt");
byte[] bys={97,98,99,100,101};
fos.write(bys,1,3);
字节缓冲流
出现原因:字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果,java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式),所以提供了字节缓冲区流
简而言之就是在字节流的基础上在加了缓冲区,比字节流的执行速度快
字节缓冲流 的使用方法和字节流一样
加入异常处理的IO流操作
FileOutputStream out = null;
try {
out = new FileOutputStream("z://fos.txt");
out.write("hello".getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out != null){//想一想如果把 out != null 去掉 然后电脑没有z盘那么out没有初始化 out = null;
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
字节流最重要的也是最基础的四大输入输出方法
package cn.itcast_06;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:把e:\\a.txt复制到当前项目目录下的copy.mp4中
*
* 字节流四种方式复制文件:
* 基本字节流一次读写一个字节: 共耗时:117235毫秒
* 基本字节流一次读写一个字节数组: 共耗时:156毫秒
* 高效字节流一次读写一个字节: 共耗时:1141毫秒
* 高效字节流一次读写一个字节数组: 共耗时:47毫秒
*/
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// method1("e:\\a.txt", "copy1.mp4");
// method2("e:\\a.txt", "copy2.mp4");
// method3("e:\\a.txt", "copy3.mp4");
method4("e:\\a.txt", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
// 高效字节流一次读写一个字节数组:
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节:
public static void method3(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节
public static void method1(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}