IO流 I input output 的概念 程序停止,内存中的数据变量/数组/集合消失 I input output
而IO流可以把数据写出到硬盘文件中,实现数据的持久化保存
按照流的方向:
输入流: 字节输入流: InputStream 字符输入流: Reader
输出流: 字节输出流: OutputStream 字符输出流: Writer
计算机中的一切都是字节流,字符流: 只能用来处理纯文本文件
.java,.html,.xml,.css,.js,.dtd,.xsd 是纯文本文件
.doc,.xls 不是纯文本文件
OutputStream类和InputStream类:抽象类,不能直接new对象
close(): 关闭流, write(int b): 写出一个字节 write(byte[] bs): 写出一个字节数组
write(byte[] bs,int startIndex,int len): 写出一个字节数组的一部分
参数: byte[] bs: 字节数组 int startIndex: 起始索引 int len: 写出字节的个数
OutputStream类不能直接new对象,
其子类FileOutputStream (File/String) 以文件字节输出流 File/String类型的文件路径
String变成字节数组? byte[] getBytes(): 获取调用方法字符串对应的字节数组
字节输出流都在一行追加续写改变格式
FileOutputStream(File/String path,boolean append):
参数: boolean append true: 追加写入,继续写入 false: 覆盖/重写写入
默认不写第二个参数,就是false,代表覆盖/重写写入
换行也是字符串: windows: \r\n linux/unix: \n
字节输出流写数据的方法
1.创建文件字节输出流FileOutputStream类的对象fos,绑定目标文件
2.文件字节输出流FileOutputStream类的对象fos调用write方法,写出字节到文件
3.字节输出流FileOutputStream类的对象fos调用close方法,关闭流释放资源
public class Demo03OputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("day11_xw\\fos1.txt");
fos.write(97);
fos.write('A');
byte[] bs = {66,67,68,69,70,71};
fos.write(bs);
fos.write(bs,2,3);
byte[] bs2 = "好想有个女朋友".getBytes();
fos.write(bs2);
fos.write("有个女同桌也行".getBytes());
fos.close();
}
}
public class Demo04Append {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("day11_xw\\fos2.txt",true);
fos.write("好好学习".getBytes());
fos.write("\r\n".getBytes());
fos.write("天天向上".getBytes());
fos.write("\r\n".getBytes());
fos.close();
}
}
InputStream类
close() read(): 读取一个字节,返回int数据
int read(byte[] bs): 读取一个字节数组,返回int数据(表示读取到的字节的数量)
参数: byte[] bs: 字节数组,作用是存储读取到的多个字节的内容
返回值: int: 表示读取到的字节的数量
如果read方法返回-1,说明文件读取结束了
一次读取一个字节数组
String(byte[] bs): 把构造方法参数字节数组,转换成字符串
String(byte[] bs,int startIndex,int len): 把构造方法参数字节数组从指定索引开始后面的len个字节,转换成字符串
InputStream子类FileInputStream类 public FileInputStream(File/String类型的文件路径path)
一次读取一个字节
public class Demo03InputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("day11_xw\\fis1.txt");
int b = 0;
while ((b = fis.read()) != -1) {
System.out.println(b+"<------>"+(char)b);
}
fis.close();
}
}
一次读取一个字节数组
public class Demo04InputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("day11_xw\\fis2.txt");
byte[] bs = new byte[2];
int len = 0;
while ((len = fis.read(bs)) != -1) {
System.out.println(new String(bs,0,len));
}
fis.close();
}
文件复制
使用字节流可以进行任何文件的复制,因为字节流操作的是组成文件的最小单元-字节
字节流复制文件
实现步骤:
1.创建文件字节输入流FileInputStream类的对象fis,绑定源文件
2.创建文件字节输出流FileOutputStream类的对象fos,绑定目标文件
3.循环读(源文件)写(目标文件)
4.关闭流,释放资源
测试效率: 单字节循环复制文件: 30443 毫秒 字节数组循环复制: 55 毫秒
public class Demo04CopyFile {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
copy02();
long end = System.currentTimeMillis();
System.out.println("总耗时: "+(end - start)+" 毫秒");
}
private static void copy02() throws IOException {
FileInputStream fis = new FileInputStream("day11_xw\\io\\from\\jzc.flv");
FileOutputStream fos = new FileOutputStream("day11_xw\\io\\to\\jzc.flv");
byte[] bs = new byte[1024*8];
int len = 0;
while ((len = fis.read(bs)) != -1) {
fos.write(bs,0,len);
}
fis.close();
fos.close();
}
private static void copy01() throws IOException {
FileInputStream fis = new FileInputStream("day11_xw\\io\\from\\jzc.flv");
FileOutputStream fos = new FileOutputStream("day11_xw\\io\\to\\jzc.flv");
int b = 0;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}
}
IO流中的异常处理
1.throws: 声明抛出异常
2.try-catch: 捕获处理异常
try{
有可能产生异常的代码
} catch(异常类 对象名){
异常处理的代码
} finally{
释放资源的代码
}
public class Demo05IOException {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("day11_xw\\io\\from\\jzc.flv");
fos = new FileOutputStream("day11_xw\\io\\to\\jzc.flv");
byte[] bs = new byte[1024*8];
int len = 0;
while ((len = fis.read(bs)) != -1) {
fos.write(bs,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
缓冲流
字节输出缓冲流
BufferedOutputStream(OutputStream os)类: 字节缓冲输出流
BufferedInputStream类 字节输入缓冲流
public void write(byte[] bs,int index,int len): 写出一个字节数组的一部分
参数: byte[] bs: 字节数组 int index: 起始索引 int len: 字节个数
步骤
1.创建OutputStream类的子类FileOutputStream的对象fos,绑定目标文件
2.创建字节缓冲输出流BufferedOutputStream的对象bos,构造方法传递fos
3.字节缓冲输出流BufferedOutputStream的对象bos调用write方法,写出字节
4.字节缓冲输出流BufferedOutputStream的对象bos调用close方法,关闭流,释放资源
注意:
1.字节缓冲输出流使用缓冲流,千万不要调用flush方法了
2.字节缓冲输出流不用自己关闭缓冲流所关联的其它流
### 字节输入缓冲流
public BufferedInputStream(InputStream is):
参数: InputStream is 字节输入流抽象类 传递子类FileInputStream对象
使用步骤:
1.创建InputStream类的子类FileInputStream对象fis,绑定源文件
2.创建高效字节输入流BufferedInputStream对象bis,传递InputStream类的子类FileInputStream对象
3.高效字节输入流BufferedInputStream对象bis调用read方法,读取字节
4.关闭流
字节输入缓冲流 只需要关闭缓冲流,自动关闭缓冲流所关联的其它流对象 ,千万不要调用flush方法了
字节缓冲流复制文件代码实现
public class Demo04BufferedCopyFile {
public static void main(String[] args) throws IOException {
File srcFile = new File("day12_sw\\copyfrom\\cxyjzc.flv");
File destFile = new File("day12_sw\\copyto\\cxyjzc.flv");
long start = System.currentTimeMillis();
copy02(srcFile,destFile);
long end = System.currentTimeMillis();
System.out.println("总耗时: "+(end-start)+" 毫秒");
}
public static void copy02(File srcFile,File destFile) throws IOException {
BufferedInputStream bis =
new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(destFile));
int len = 0;
byte[] bs = new byte[1024];
while ((len = bis.read(bs)) != -1) {
bos.write(bs,0,len);
}
bis.close();
bos.close();
}
public static void copy01(File srcFile,File destFile) throws IOException {
BufferedInputStream bis =
new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(destFile));
int b = 0;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
}
文件夹复制
package com.it.d2_copy_file;
import java.io.*;
public class Demo04CopyDir {
public static void main(String[] args) throws IOException {
File srcDir = new File("E:\\黑马JavaEE基础班156期-武汉");
File destDir = new File("C:\\Users\\smhjx\\Desktop\\aaaaa");
copyDir2Dir(srcDir,destDir);
}
public static void copyDir2Dir(File srcDir, File destDir) throws IOException {
File newDestDir = new File(destDir, srcDir.getName());
if (!newDestDir.exists()) {
newDestDir.mkdirs();
}
File[] files = srcDir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isFile()) {
copyFile2Dir(file,newDestDir);
} else {
copyDir2Dir(file,newDestDir);
}
}
}
}
private static void copyFile2Dir(File srcFile, File destDir) throws IOException {
File destFile = new File(destDir, srcFile.getName());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bs = new byte[1024];
int len;
while ((len = bis.read(bs)) != -1) {
bos.write(bs,0,len);
}
bis.close();
bos.close();
}
}