IO流-字节流2
![]()
package IO.ByteStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamException {
public static void main(String[] args) {
FileOutputStream fos = null; //把变量初始化,否则空指针异常
try {
fos = new FileOutputStream("fos.txt");
fos.write("hello,world".getBytes());
}catch (IOException i ){
i.printStackTrace();
}finally{ if (fos!=null) { //加入finally来释放资源, if语句增强健壮性,包括整个finally语句块
try { //Alt+Enter自动处理异常
fos.close(); //释放资源的方法
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//Cannot resolve symbol 'fos' : 无法解析的符号, 就是没有这个变量
//Variable 'fos' might not have been initialized : 变量可能没被初始化
}
}
}
字节流读写数据 一次读一个写一个字节
/* 复制txt文件
使用的方法
1. 读取:
int read(): 从该输入流读取一个字节的数据。
int read(byte[] b): 从该输入流读取最多 b.length个字节的数据为字节数组。
int read(byte[] b, int off, int len): 从该输入流读取最多 len字节的数据为字节数组。
2. 写入:
void write(int b) : 将指定的字节写入此文件输出流。
void write(byte[] b) : 将 b.length个字节从指定的字节数组写入此文件输出流。
void write(byte[] b, int off, int len) : 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
*/
package IO.ByteStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyTxtDemo {
public static void main(String[] args) throws IOException {
//创建输入流对象
FileInputStream fis = new FileInputStream("d:\\2\\fileOutputStream.txt");
//创建输出流对象
FileOutputStream fos = new FileOutputStream("fos.txt");
//读取数据, 复制文本文件(一次读一个字节, 一次写一个字节)
int bytes;
while ((bytes=fis.read())!=-1){ //读取一个数据
fos.write(bytes); //写一个数据
}
//释放资源
fis.close();
fos.close();
}
}
//--------------------------------
//复制jpeg图片文件
package IO.ByteStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyJpeg {
public static void main(String[] args) throws IOException {
//创建输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\BigBaby\\Desktop\\下载_截图\\学习金字塔.jpeg");
//创建输出流对象
FileOutputStream fos = new FileOutputStream("study.jpeg");
//读取数据,复制文件, 读一个写一个字节
int bytes ;
while ((bytes = fis.read())!= -1){
fos.write(bytes);
}
//释放资源
fis.close();
fos.close();
}
}
![]()
package IO.ByteStream;
import java.io.FileInputStream;
import java.io.IOException;
/* 需求: 把文件中的内容读取出来在控制台输出
使用字节输入流读数据的步骤:
1. 创建字节输入流对象
2. 调用字节输入流对象的读数据方法
3. 释放资源
需要用到的方法:
方法1. int read(byte[] b): 从该输入流读取最多b.length个字节的数据到一个字节数组,
就是把读取的数据存储到这个数组中,其返回值就是实际读取数据的个数, 读取到没有数据的时候返回值为-1.
方法2. String (byte [] bytes, int offset, int len): 把一个字节数组转成字符串(String类,非静态方法需要new)
参数为需要转换的字节数组, 和指定长度.
*/
public class FileInputStream_1 {
public static void main(String[] args) throws IOException {
//1. 创建字节输入流对象
FileInputStream fis = new FileInputStream("fos.txt");
//2. 调用字节输入流对象的读数据方法
/*
byte[] b =new byte[5];
int len = fis.read(b); // < len 表示实际读取数据的个数 >
String s = new String(b); //不是静态方法
System.out.println(len);
//String (byte [] bytes, int offset , int length): 一次读取完成
System.out.println(new String(b,0,len));
System.out.println("----------");
//第二次读取
len = fis.read(b);
System.out.println(len);
System.out.println(new String(b,0,len));
System.out.println("----------");
*/
/* hello\r\n
world\r\n
第一次: hello
第二次: \r\nwor
第三次: ld\r\nr 数组前4个元素被替换,第五个元素保留上面的r,
所以要用String (byte [] bytes, int offset , int length)构造方法:一次读取完成
实际读取长度是-1时,就表示已经读取到末尾了.
*/
//字节流读取数据的通用格式
byte [] b = new byte[1024]; // 数组长度为读取的次数: 1024及其整数倍
int len; //声明变量: 实际读取个数
while((len=fis.read(b))!=-1){ //把数据读取到字节数组,再把实际读取个数赋值, 读取到末尾也就是-1时,结束循环
System.out.print(new String(b,0,len)); //把byte数组转成字符串
}
//3. 释放资源
fis.close();
}
}
复制图片 一次读取一个字节数组的数据
package IO.ByteStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyJpegWithArray {
public static void main(String[] args) throws IOException {
//创建输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\BigBaby\\Desktop\\下载_截图\\学习金字塔.jpeg");
//创建输出流对象
FileOutputStream fos = new FileOutputStream("study.jpeg");
//复制文件
byte [] bytes = new byte[1024]; //创建容器数组
int len ; //实际读取数
while((len = fis.read(bytes)) != -1){ //往容器数组里读取数据,返回读取数,
fos.write(bytes, 0, len); //把数组中的数据写入文件中, 读取到无数据时返回值为-1, 循环结束.
}
}
}
![]()
字节缓冲流:
BufferedOutputStream BufferedInputStream
构造方法:
字节缓冲输出流: BufferedOutputStream (OutputStream out)
字节缓冲输入流: BufferedInputStream (InputStream in)
*/
package IO.ByteStream;
import java.io.*;
public class ByteBufferedStream {
public static void main(String[] args) throws IOException {
//字节缓冲输出流: BufferedOutputStream (OutputStream out) 参数是文件输出流对象
// BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("fos.txt"));
// //写入数据到文件
// bos.write("hello\r\n".getBytes());
// bos.write("world".getBytes());
// bos.close();
//字节缓冲输入流: BufferedInputStream (InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("fos.txt"));
//读取文件 一次一个字节
int bytes;
while((bytes=bis.read())!=-1){
System.out.print((char) bytes); //强转为char类型就能显示为字符串
}
//一次读取一个字节数组
byte [] b = new byte[100];
int len;
while ((len=bis.read(b))!=-1){
System.out.print(new String(b,0,len));//String方法把byte数组转字符串
}
bis.close();
}
}
复制视频(大文件)
/* 复习一下复制文件的四种方法 记住格式就行
建议使用第二或第四种方法(执行时间最短)
需求:
把 X:\\XXX\\XXX.mp4 复制到模块目录下的 XXX.mp4
思路:
1:根据数据源创建字节输入流对象
2:根据目的地创建字节输出流对象
3:读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组)
4:释放资源
四种方式实现复制视频,并记录每种方式复制视频的时间
1:基本字节流一次读写一个字节 用时:64秒.
2:基本字节流一次读写一个字节数组 用时:0秒.
3:字节缓冲流一次读写一个字节 用时:0秒.
4:字节缓冲流一次读写一个字节数组 用时:0秒.
*/
package IO.ByteStream;
import java.io.*;
public class CopyAvi {
public static void main(String[] args) throws IOException {
long startTime = System.currentTimeMillis();
//数据源 d:\\2\\gaoshou.mp4 目的地 本目录下 社会高手.mp4
// CopyFile();
// CopyFile1();
// CopyFile2();
CopyFile3();
long endTime = System.currentTimeMillis();
System.out.println("用时:"+(endTime-startTime)/1000+"秒.");
}
//1:基本字节流一次读写一个字节
public static void CopyFile() throws IOException {
FileInputStream fis = new FileInputStream("d:\\2\\gaoshou.mp4");
FileOutputStream fos = new FileOutputStream("社会高手.mp4");
int bys;
while ((bys=fis.read()) != -1){
fos.write(bys);
}
fis.close();
fos.close();
}
//2:基本字节流一次读写一个字节数组
public static void CopyFile1() throws IOException{
FileInputStream fis = new FileInputStream("d:\\2\\gaoshou.mp4");
FileOutputStream fos = new FileOutputStream("社会高手.mp4");
byte [] bys = new byte[1024];
int len=0;
while((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}
fis.close();
fos.close();
}
//3:字节缓冲流一次读写一个字节
public static void CopyFile2() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\2\\gaoshou.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("社会高手.mp4"));
int bys;
while ((bys =bis.read())!=-1){
bos.write(bys);
}
bis.close();
bos.close();
}
//4:字节缓冲流一次读写一个字节数组
public static void CopyFile3() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\2\\gaoshou.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("社会高手.mp4"));
byte [] bys = new byte[1024];
int len;
while((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bis.close();
bos.close();
}
}