IO流、FileOutputStream、FileInputStream、字节缓冲流
IO流
(1)概述:
输入流:硬盘 ——> 内存
输出流:内存 ——> 硬盘
(2)从数据划分
字节流Byte 操作的是底层的0和1数据
字符流char 操作字符数据
(3)体系图
FileOutputStream类
(1)常用方法
方法 | 备注 |
---|---|
void write(int b) | 将指定的字节写入此文件输出流 |
void write(byte[] b) | 将 b.length字节从指定的字节数组写入此文件输出流 |
void write(byte[] b, int off, int len) | 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流 |
void close() | 关闭此文件输出流并释放与此流相关联的任何系统资源 |
(2)注意事项
1、在创建对象的时候,如果文件不存在,则创建。如果文件存在,则清空。 【注意】 FileOutputStream 创建对象,可能清空文件!!!!!!
2、写数据的时候,如果写入的是 整数数据,对应的是 码表上面的字母
3、 每次使用完毕IO流之后,记得释放资源,断开与操作系统之间的联系。
(3)入门示例:
//FileOutputStream的快速入门
public class Test01 {
public static void main(String[] args) throws IOException {
// (1). 创建对象
FileOutputStream fos = new FileOutputStream("JavaSEDay12\\dir\\aaa.txt");
// (2). 读写数据
fos.write(65);
// (3). 释放资源
fos.close();
}
}
(4)字节流写数据的两个问题
a) 换行
A. windows 操作系统: “\r\n”
B. Linux 操作系统: “\n”
C. Mac 操作系统: “\r”
b) 续写
在创建对象的时候,传入第二个参数,设置为 true 就是续写。
例如: FileOutputStream fos = new FileOutputStream(“路径”,true); //给出第二个参数 true
FileInputStream类
(1)构造方法
方法名 | 备注 |
---|---|
FileInputStream(File file) | 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名 |
FileInputStream(String name) | 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名 |
(2)常用方法
方法名 | 备注 |
---|---|
int read() | 从该输入流读取一个字节的数据 |
int read(byte[] b) | 从该输入流读取最多 b.length个字节的数据到一个字节数组 |
(4)入门案例:
//FileInputStream 的快速入门
public class Test01 {
public static void main(String[] args) throws IOException {
// (1). 创建对象
FileInputStream fis = new FileInputStream("JavaSEDay12\\dir\\bb.txt");
// (2). 读写数据
int b;
//第一步: fis.read() 一次读取一个字节数据
//第二步: b = fis.read() 将读取到的数据,赋值给b
//第三步: (b = fis.read()) != -1 读取到的数据,是否到达了末尾,末尾就是 -1
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
// (3). 释放资源
fis.close();
}
}
(5)注意事项
a) 文件的路径, 当前的文件必须存在,否则会出现 文件找不到的异常。 FileNotFoundException
b) 下面的循环,必须采用 while 循环,不要搞 for 循环。 因为这个是固定格式。
c) 上述的 while 循环当中的小括号必须这样写, 如果是一次读取一个字节的时候,不能改写。
(6)文件复制案例
//采用字节流 复制文件。 一次读写一个字节
public class CopyFile01 {
public static void main(String[] args) throws IOException {
String srcPath = "JavaSEDay11\\dir\\原始文件.txt";
String destPath = "JavaSEDay11\\dir\\目标文件1.txt";
//-----------------
// (1). 创建对象
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
// (2). 读写数据
int temp;
while ((temp = fis.read()) != -1) {
fos.write(temp);
}
// (3). 释放资源
fis.close();
fos.close();
}
}
//采用字节流一次读写一个字节数组,复制图片案例
public class Test03 {
public static void main(String[] args) throws IOException {
String srcPath = "JavaSEDay11\\dir\\baoshijie.png";
String destPath = "JavaSEDay11\\dir\\目标图片1.png";
// --------
// (1). 创建对象
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
// (2). 读写数据
byte[] data = new byte[1024];
int len;
while ((len = fis.read(data)) != -1) {
fos.write(data, 0, len);
}
// (3). 释放资源
fis.close();
fos.close();
}
}
字节缓冲流
(BufferedInputStream、BufferedOutputStream)使用方法和FileInputStream、FileOutputStream一模一样
案例:
//缓冲流一次读写一个字节 复制图片
public class Test03 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
String srcPath = "JavaSEDay11\\dir\\mm.png";
String destPath = "JavaSEDay11\\dir\\目标图片1.png";
//----------------------------------
// (1). 创建对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcPath));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
// (2). 读写数据
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
// (3). 释放资源
bis.close();
bos.close();
long end = System.currentTimeMillis();
System.out.println("共计花费时间:" + (end - start));
}
}
//缓冲流一次读写一个字节数组 复制图片
public class Test04 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
String srcPath = "JavaSEDay11\\dir\\mm.png";
String destPath = "JavaSEDay11\\dir\\目标图片2.png";
//----------------------------------
// (1). 创建对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcPath));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
// (2). 读写数据
byte[] data = new byte[1024];
int len;
while((len = bis.read(data))!=-1){
bos.write(data,0,len);
}
// (3). 释放资源
bis.close();
bos.close();
long end = System.currentTimeMillis();
System.out.println("小数组共计花费时间:" + (end - start));
}
}