JavaSE-字节流(读写数据,复制文件,缓存流)
1.IO流概述
- IO: Input/Output
- 流:是一张抽象概念,是数据传输的总称,数据在设备之间的传输为流。
- 分类:输出流/输入流,
字节流/字符流(在记事本读的懂的是字符流,读不懂的是字节流)(不确定用字节)
2.字节流写数据
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("D:\\学习\\java\\test\\fos.txt");
/*
完成了3步
1.调用系统创建了文件
2.创建了输出流对象
3.输出流对象指向创建的文件
*/
fileOutputStream.write(97);
fileOutputStream.write(57);
fileOutputStream.write(55);
//IO相关的最后要释放资源
//void close() 关闭此文件输出流并释放与此流关联的所有系统资源。
fileOutputStream.close();
}
}
3.字节流写数据的3种方式
void write(int b) 将指定的字节写入此文件输出流。 //一次写一个字节数据
void write(byte[] b, int off, int len) 将从偏移量 off开始的指定字节数组中的 len字节写入此文件输出流。 //一次写一个字节数组数据的部分数据
void write(byte[] b) 将指定字节数组中的 b.length字节写入此文件输出流。 //一次写一个字节数组数据
fileOutputStream.write(97);
fileOutputStream.write(57);
fileOutputStream.write(55);
byte[] bytes={97,98,99,100,101};
fileOutputStream.write(bytes);
byte[] bytes="abcdef".getBytes(); //byte[] getBytes() 使用平台的 默认字符集将此 String编码为字节 序列,将结果存储到新的字节数组中。
fileOutputStream.write(bytes,1,3);//输入bcd
4.写入换行
加一个 fos.write("\n\r".getBytes()); \n\r 是为了适应多种系统
追加写入
FileOutputStream(String name, boolean append) 创建文件输出流以写入由指定的File对象表示的文件。 如果第二个参数是true ,则字节将写入文件的末尾而不是开头。
5.字节流写数据加异常处理
原因
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo01 {
public static void main(String[] args) {
FileOutputStream fileOutputStream = new FileOutputStream("D:\\学习\\java\\test\\fos.txt");
try {
fileOutputStream.write("abcef".getBytes());
fileOutputStream.close();//如果上面一行发生错误,程序直接运行 e.printStackTrace(); 而没有释放系统资源
} catch (IOException e) {
e.printStackTrace();
}
}
}
finally:在异常处理时提供finally块来执行所以清除操作。
被finally执行的语句一定会执行,除非JVM退出
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("D:\\学习\\java\\test\\fos.txt");
fileOutputStream.write("abcef".getBytes());
//如果上面一行发生错误,程序直接运行 e.printStackTrace(); 而没有释放系统资源
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileOutputStream !=null){ //防止路径错误,导致指向null
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
6.字节流读数据
- 一个读一个
import java.io.FileInputStream;
import java.io.IOException;
public class FileInPutDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("D:\\学习\\java\\test\\fos.txt");
//int read() 从此输入流中读取一个字节的数据。
// int read = fis.read();
// System.out.println(read);
// read = fis.read();
// System.out.println(read);
//先判断再执行 再判断再执行 判断和语句也会循环执行
int by =0;
while((by=fis.read())!= -1){//读到文本尾部值是-1
System.out.print((char)by);
}
fis.close();
}
}
- 一次读一个字节数组
int read(byte[] b)// 从此输入流 b.length最多 b.length字节的数据读 b.length字节数组。
- 返回的是实际读到的长度 如果byte【5】,返回4,说明第5个位置没改动
- 换行会读两个字节长度 \r\n 再次输出时 输出一个空行
import java.io.FileInputStream;
import java.io.IOException;
public class FileInPutDemo2 {
public static void main(String[] args) throws IOException {
FileInputStream fis =new FileInputStream("D:\\学习\\java\\test\\fos.txt");
byte[] bys =new byte[1024]; //一般给1024及其整数倍
int len=0;
while (-1!= (len=fis.read(bys))){ //没有读到数据len=-1
String s = new String(bys, 0, len);//取bys里的0到len
System.out.println(s);
//fos.write(bys,o,len)
}
fis.close();
}
}
7.字节流复制文本文件
while((by=fis.read())!= -1){
fos.write(by);
}
8.字节流复制图片
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyJPGDemo {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream("D:\\zha\\photo\\001.png");
FileOutputStream fileOutputStream = new FileOutputStream("D:\\学习\\java\\test\\001.png");
int len =0;
byte[] bys = new byte[1024];
while ((len=inputStream.read(bys))!=-1){
fileOutputStream.write(bys,0,len);
}
fileOutputStream.close();
inputStream.close();
}
}
9.字节缓冲流
避免不断调用底层系统
仅仅提供缓冲区,真正的读写数据还得依靠基本的字节流对象进行操作
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\学习\\java\\test\\bos.txt"));
bos.write("hello\r\n".getBytes());
bos.write("world".getBytes());
bos.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\学习\\java\\test\\bos.txt"));
int by =0;
while((by=bis.read())!= -1){
System.out.print((char)by);
}
bis.close();
复制文件运行速度比较
字节缓冲流一次读写一个数组>
基本字节流一次读写一个数组>
字节缓冲流一次读写一个字节>
基本字节流一次读写一个字节