javaAPI_IO流基础_字节流基础知识


IO流

1.IO流概述
所谓的IO流就是用来进行设备之间的数据传输的。


2.IO流分类
(1).按照数据流向
输入流 读取数据
输出流 写出数据
(2).按照数据类型
字节流
字节输入流 读取数据 InputStream
字节输出流 写出数据 OutputStream
字符流
字符输入流 读取数据 Reader
字符输出流 写出数据 Writer
什么情况下使用哪种流呢?
如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。
如果你什么都不知道,就用字节流[由此可见,在IO流技术中,常用的也就是这四个操作]


3.OutputStream类
(1).由于该类是一个抽象的基类,所以不能够实例化,所以我们需要学习其子类。

(2).FileOutputStream类

A:构造方法
FileOutputStream(File file)
FileOutputStream(String path)
FileOutputStream(String path,boolean true):指定是否需要追加,true表示可以追加,false表示不可以追加

注意事项1:
//创建对象
FileOutputStream fos = new FileOutputStream("test.txt");做了哪一些事情?

a:调用系统功能,创建文件test.txt
b: 创建fos对象
c: 把fos对象指向这一个文件

B:相关方法
public void write(int b);把一个字节写入指定对象文件中
public void write(byte[] b);把一个字节数组写入指定对象中
public void write(byte[] b,int off,int len);把字节数组的部分写入指定对象中



C:方法测试:要求在项目的根目录下面的文件test.txt中写入字符串"helloworld"

public static void main(String[] args) throws IOException {
//创建对象
FileOutputStream fos = new FileOutputStream("test.txt");
fos.write("helloworld".getBytes());
//释放资源(因为在传递这一个字节的通道一直在开着)
fos.close();
}

//注意:为什么要关闭流对象?
a:为了让流对象变成垃圾,方便资源回收。
b: 告诉系统去释放和该文件相关的资源。


D:FileOutputStream实现数据换行以及数据追加
a:如何实现数据的换行?
在程序中,实现数据的换行可以加入表示符号:
windows:\r\n;
linux:\n
mac:\r
b:如何实现数据的追加写入?
使用构造方法带第二个参数是true的情况即可

c:代码实现
public static void main(String[] args) throws IOException {
//创建对象
FileOutputStream fos = new FileOutputStream("test.txt",true);
for(int i = 0;i<10;i++){
fos.write(("helloworld"+i).getBytes());
fos.write("\r\n".getBytes());
}
//释放资源(因为在传递这一个字节的通道一直在开着)
fos.close();
}


E:异常处理在FileOutputStream类中的使用
public static void main(String[] args) {
FileOutputStream fos = null;

try {
fos = new FileOutputStream("test.txt",true);
fos.write("helloworld".getBytes());
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{

if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


4.FileInputStream类

(1).构造方法
FileInputStream(String path)

(2).相关方法
public int read():一次读取一个字节,如果已经到达文件的末尾,则返回-1
public int read(byte[] b):一次读取一个字节数组,如果已经到达文件的末尾,则返回-1


(3).使用FileInputStream读取数据基本测试代码[一次读取一个字节]
public static void main(String[] args) throws IOException {
//创建对象
FileInputStream fis = new FileInputStream("test.txt");
//读取数据
int by = 0;
//读取,赋值,判断
while ((by = fis.read()) != -1) {
System.out.print((char) by);
}
//释放资源
fis.close();

}

(4).使用FileInputStream读取数据基本测试代码[一次读取一个字节数组*****]
public static void main(String[] args) throws IOException {
//创建对象
FileInputStream fis = new FileInputStream("test.txt");
//读取数据
//读取数组的长度一般是1024或者是1024的整数倍,因为1k=1024个字节
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}
//释放资源
fis.close();

}

5.字节缓冲输出流(BufferedOutPutStream)/字节缓冲输入流(BufferedInputStream)

(1).概述:
字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果,java本身在设计的时候,
也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流


(2).BufferedOutPutStream类
A:构造方法
BufferedOutPutStream(outPutStream out):创建一个缓冲区输出流,使用默认缓冲区大小
BufferedOutPutStream(outPutStream out,int size):指定缓冲区大小,一般不用指定,使用默认就已经足够了。

注意:
为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。


B:基本使用测试:
public static void main(String[] args) throws IOException {
// 简单写法
// BufferedOutputStream(OutputStream out)
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("bos.txt"));

// 写数据
bos.write("hello".getBytes());

// 释放资源
bos.close();
}


(3).BufferedInputStream类

A:构造方法
BufferedInputStream(InputStream out):创建一个缓冲区输入流,使用默认缓冲区大小
BufferedInputStream(InputStream out,int size):指定缓冲区大小,一般不用指定,使用默认就已经足够了。

B:基本使用测试

public static void main(String[] args) throws IOException {
// BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}

// 释放资源
bis.close();
}





6.字节流常见的相关操作

(1).字节流复制粘贴文件[即上传下载]
/*
* 需求:把c:\\a.txt内容复制到d:\\b.txt中
*
* 数据源:
* c:\\a.txt -- 读取数据 -- FileInputStream
* 目的地:
* d:\\b.txt -- 写出数据 -- FileOutputStream
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("c:\\a.txt");
FileOutputStream fos = new FileOutputStream("d:\\b.txt");

// 复制数据
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}

// 释放资源
fos.close();
fis.close();
}
}


(2).使用4种方式复制MP4文件到指定文件中[*****]

/*
* 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
*
* 字节流四种方式复制文件:
* 基本字节流一次读写一个字节: 共耗时:117235毫秒
* 基本字节流一次读写一个字节数组: 共耗时:156毫秒
* 高效字节流一次读写一个字节: 共耗时:1141毫秒
* 高效字节流一次读写一个字节数组: 共耗时:47毫秒
*/
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// method1("e:\\哥有老婆.mp4", "copy1.mp4");
// method2("e:\\哥有老婆.mp4", "copy2.mp4");
// method3("e:\\哥有老婆.mp4", "copy3.mp4");
method4("e:\\哥有老婆.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();
}
}

猜你喜欢

转载自www.cnblogs.com/nwxayyf/p/10258264.html