一、IO流
1.概述:
IO用于在设备间进行数据传输的操作
2.分类:
-
流向
- 输入流 读取数据
- 输出流 写出数据 数据类型
-
字节流
– 字节输入流InputStream
– 字节输出流OutputStream -
字符流
– 字符输入流 Reader
– 字符输出流 Writer
注意
- a:如果我们没有明确说明按照什么分,默认按照数据类型分。
- b:除非文件用windows自带的记事本打开我们能够读懂,才采用字符流,
二、FileOutputStream
1、构造方法
//A:这种方式从头开始写,不是追加的写
public FileOutputStream(File file)
public FileOutputStream(String name)
//B:以追加的方式进行写入
//如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处
public FileOutputStream(File file,boolean append)
public FileOutputStream(String name,boolean append)
通过上面的方法进行构造举例:
/*
* 创建字节输出流对象做了几件事情
* A:调用系统功能去创建文件
* B:创建fos对象
* C:把fos对象指向这个文件
*/
//方式1:
File file = new File("1.txt");
FileOutputStream fos = new FileOutputStream(file);
//方式2:
FileOutputStream fos1 = new FileOutputStream("1.txt");
//方式3:
FileOutputStream fos2 = new FileOutputStream("1.txt",true);
fos2.write("I love you".getBytes());
fos2.close();
2、常用常见的成员方法
public void write(int b):写一个字节
public void write(byte[] b):把整个字节数组,都写入文件
public void write(byte[] b,int off,int len):把数组中的一部分写入文件
public void close();释放资源,一旦关闭之后,就不能调用此对象
举例子:
FileOutputStream fos1 = new FileOutputStream("1.txt",true);
fos1.write("I love you".getBytes());
fos1.close();
标准写法举例子
FileOutputStream fos = null;
try {
fos = new FileOutputStream("1.txt");
fos.write("hello".getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos == null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三、FileInputStream
1、构造方法
public FileInputStream(File file)
public FileInputStream(String name)
2、成员方法:
public int read()一个字节一个字节的读取,返回值就是读取的读取字符的ASCII码,-1,代表读到末尾
public int read(byte[] b)将数据读到byte[]数组中,返回值代表实际读取的个数
方式1:
int by = 0;
//读取,赋值,判断
while((by = fis.read()) != -1){
System.out.print((char)by);
}
方式2:
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}
3、问题:
A:为什么字节流不能处理中文?
中文是由两个字符组成的,第一个是负数,第一个配合第一个使用,字节流只能一次处理一个字节,所以字节流不能处理中文
String s1 = "我爱你中国";
//[-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]
//在计算机中中文的存储分为两个字节:
// 第一个字节:肯定是负数
// 第二个字节常见的是负数,可能有正数,但是没影响
//控制台上,一个负数一处理,所以出现乱码
//而计算机自己处理的时候,遇见负数,首先考虑的是汉字,两个字节的拼接,所以会转换成功
byte[] by = s1.getBytes();
System.out.println(Arrays.toString(by));
四、BufferedOutputStream
为了读取和写入的速度快,所以引用字节缓冲流
1、概述:
可以想象成为一个水杯,仅仅是高效的作用,真正有效的还是FileOutputStream,FileOutputStream相当于一个水龙头
2、构造函数:
public BufferedOutputStream(OutputStream out)
public BufferedOutputStream(OutputStream out,int size)size定义的是缓冲区大小,但是不定义就够用
例子:
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
bos.write("hello".getBytes());
bos.close();
3、部分成员方法
public void flush()throws IOException刷新
public void write(byte[] b,int off,int len)throws IOException,将byte[]数组写入到文件中
五、BufferedInputStream:缓冲区,快的读取文件
1、构造函数
public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in,int size)size定义的是缓冲区大小,但是不定义就够用
2、部分成员方法:
public int read()throws IOException 一个字节一个字节的读取,返回值就是读取的读取字符的ASCII码,-1,代表读到末尾
public int read(byte[] b,int off,int len)throws IOException 将数据读到byte[]数组中,返回值代表实际读取的个数
六、练习:
需求:用这四种方式:把F:\xihong.mp4复制到当前项目目录下的copy.mp4中
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:把F:\\xihong.mp4复制到当前项目目录下的copy.mp4中
*
* 字节流四种方式复制文件:
* 基本字节流一次读写一个字节: 共耗时:62281毫秒
* 基本字节流一次读写一个字节数组: 共耗时:98毫秒
* 高效字节流一次读写一个字节: 共耗时:333毫秒
* 高效字节流一次读写一个字节数组: 共耗时:33毫秒
*/
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// method1("F:\\xihong.mp4", "copy1.mp4");
// method2("F:\\xihong.mp4", "copy2.mp4");
// method3("F:\\xihong.mp4", "copy3.mp4");
method4("F:\\xihong.mp4", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
// 高效字节流一次读写一个字节数组:
public static void method4(String srcString, String destString) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节:
public static void method3(String srcString, String destString) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString) throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节
public static void method1(String srcString, String destString) throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}