Io流
1、什么是IO流,IO流分类
-
方向:
- 输入流
- 输出流
-
操作单位:
- 字节流
- 字符流
2、字节流
2.1、应用场景
- 只要涉及流操作,它都可以用
2.2、字节输入流
2.2.1、InputStream
- 字节输入流超类
- 成员方法:close 、read
2.2.2、FileInputStream
-
继承InputStream,读硬盘内容到内存
-
构造方法:FileInputStream(File file); FileInputStream(String name);
-
主要方法:
- int read(); //从此输入流中读取一个数据字节
- int read(byte[] b); //从此输入流中将最多b.length个字节的数据读入一个byte数据中
- int read(byte[] b,int off,int length); //从此输入流中将最多length个字节的数据读入一个byte数组中
- void close(); //关闭此输入流并释放于此流有关的所有系统资源
-
使用步骤
- 创建对象,确定数据源
- 调用read方法
- close方法释放资源
2.3、字节输出流
2.3.1、OutputStream
- 字节输出流超类
- 成员方法:close、write、fluse
2.3.2、FileOutputStream
-
继承OutputStream,数据写入文件
-
构造方法
- FileOutputStream(File file); //默认写在开始处
- FileOutputStream(File file,boolean append); //append true 表示追加
-
主要方法:
- void write(int b); //将指定字节写入此文件输出流
- void wirte(byte[] b); //将b.length个字节从指定byte 数组写入
- void write(byte[] b,int off,int length);//off开始的length 个字节
- void close();//关闭
-
使用步骤:
- 创建对象,确定写入文件
- 调用write方法
- 调用flush方法刷新
- close
//用字节流完成文件复制
public class Test2 {
public void copyFile(File file1,File file2){
if(!file2.exists()){
file2.mkdirs();
}
File[] files = file1.listFiles();
if(files!=null){
for (File file:files) {
if(file!=null){
File file3=new File(file2,file.getName());
if(file.isFile()&&file.getName().endsWith(".mp4")){
Copy(file,file3);
}else {
copyFile(file,file3);
}
}
}
}
}
public void Copy(File file1,File file2){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream=new FileInputStream(file1);
fileOutputStream=new FileOutputStream(file2);
int length;
byte[] b=new byte[1024];
while ((length=fileInputStream.read(b))!=-1){
fileOutputStream.write(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
CloseStream.Close(fileInputStream,fileOutputStream);
}
}
}
//关流工具类
public class CloseStream {
public static void Close(InputStream inputStream, OutputStream outputStream){
try {
if(outputStream!=null){
outputStream.close();
}
if(inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void Close(Reader reader, Writer writer){
try {
if(writer!=null){
writer.close();
}
if(reader!=null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//测试
public class Test {
//将vedio中以MP4结尾的文件复制到vedio1
public static void main(String[] args) {
File file = new File("D:\\java");
File file1 = new File(file, "\\video");
File file2 = new File(file,"\\video1");
new Test2().copyFile(file1,file2);
}
}
3、字符流
- 应用场景:读取和写入中文
3.1、字符输入流
- 继承Reader //Reader成员方法 close、read
- FileReader
- 构造方法:FileReader(File file); FileReader(String name);
- 成员方法:继承父类
- 使用步骤:
- 创建对象,确定数据源
- 调用read方法:int read();int read(char[] chars);int read(char[] b,int off,int length);
- close
3.2、字符输出流
-
继承Wirter //成员方法 close、write、flush
-
FileWirter:
构造方法:
- FileWriter(File file);
- FileWriter(File file,boolean append);
- FileWirter(String name);
- FileWriter(String name,boolean append);
成员方法:继承父类
使用步骤:
-
创建对象,写入文件
-
调用wirte方法
- int wirte(int i)
- int wirte(char[] b)
- int wirte(char[] b,int off,int length)
- int wirte(String str)
- int Wirte(String str,int off,int length)
- 追加写和换行
-
close
4、缓冲流
-
BufferedReader
-
独有方法 :readLine()读取一行数据 判断的依据 返回的字符串是否为null
-
BufferedWriter
-
独有方法:newLine()换行符
-
BufferedInputStream
-
BufferedOutputStream
public class Test1 { public void copyFile(File file1,File file2){ BufferedInputStream bufferedInputStream=null; BufferedOutputStream bufferedOutputStream=null; try { File[] files = file1.listFiles(); if(files!=null){ for (File file:files) { if(file!=null&&file.isFile()&&file.getName().endsWith("mp4")){ File file3 = new File(file2, file.getName()); bufferedInputStream= new BufferedInputStream(new FileInputStream(file)); bufferedOutputStream= new BufferedOutputStream(new FileOutputStream(file3)); int length; byte[] bytes = new byte[1024*1024]; while ((length=bufferedInputStream.read(bytes))!=-1){ bufferedOutputStream.write(bytes,0,length); } }else { copyFile(file,file2); } } } } catch (IOException e) { e.printStackTrace(); } finally { CloseStream.Close(bufferedInputStream,bufferedOutputStream); } } }
5、转换流
OutputStreamWriter
InputStreamReader
6、jdk1.7以后关闭流新方式
语法:
try(
创建流
){
可能有一次的业务代码
}catch(){
}
目前用到的所有流 都是实现了AutoCloseable
实现了这个接口 就支持自动关流
redOutputStream);
}
}
}
## 5、转换流
OutputStreamWriter
InputStreamReader
## 6、jdk1.7以后关闭流新方式
```java
语法:
try(
创建流
){
可能有一次的业务代码
}catch(){
}
目前用到的所有流 都是实现了AutoCloseable
实现了这个接口 就支持自动关流
注意:有局限,一般还是用之前的close方法