您的位置:首页 > 其它

I/O流技术

2015-05-29 14:29 260 查看
IO流
|--字节流

|--字节输入流

InputStream

int read():一次读取一个字节

int read(byte[] bys):一次读取一个字节数组

|--FileInputStream

|--BufferedInputStream

|--字节输出流

OutputStream

void write(int by):一次写一个字节

void write(byte[] bys,int index,int len):一次写一个字节数组的一部分

|--FileOutputStream

|--BufferedOutputStream

|--字符流

|--字符输入流

Reader

int read():一次读取一个字符

int read(char[] chs):一次读取一个字符数组

|--InputStreamReader

|--FileReader

|--BufferedReader

String readLine():一次读取一个字符串

|--字符输出流

Writer

void write(int ch):一次写一个字符

void write(char[] chs,int index,int len):一次写一个字符数组的一部分

|--OutputStreamWriter

|--FileWriter

|--BufferedWriter

void newLine():写一个换行符

void write(String line):一次写一个字符串

IO流的分类:

流向:

输入流 读取数据

输出流 写出数据

数据类型:

字节流

字节输入流 读取数据 InputStream

字节输出流 写出数据 OutputStream

字符流

字符输入流 读取数据 Reader

字符输出流 写出数据 Writer

注意:一般我们在探讨IO流的时候,如果没有明确说明按哪种分类来说,默认情况下是按照数据类型来分的。

注意:每种基类的子类都是以父类名作为后缀名。

XxxOutputStream

XxxInputStream

XxxReader

XxxWriter

FileOutputStream的构造方法:

FileOutputStream(File file)

FileOutputStream(String name)

字节输出流操作步骤:

A:创建字节输出流对象

创建字节输出流对象了做了几件事情:

调用系统功能去创建文件

创建fos对象

把fos对象指向这个文件

B:写数据

C:释放资源

关闭此文件输出流并释放与此流有关的所有系统资源。

为什么一定要close()

A:让流对象变成垃圾,这样就可以被垃圾回收器回收了

B:通知系统去释放跟该文件相关的资源

public void write(int b):写一个字节

public void write(byte[] b):写一个字节数组

public void write(byte[] b,int off,int len):写一个字节数组的一部分

如何实现数据的换行

为什么现在没有换行呢?因为你值写了字节数据,并没有写入换行符号。

刚才我们看到了有写文本文件打开是可以的,通过windows自带的那个不行,为什么呢?

因为不同的系统针对不同的换行符号识别是不一样的?

windows:\r\n

linux:\n

Mac:\r

而一些常见的个高级记事本,是可以识别任意换行符号的。

如何实现数据的追加写入?

用构造方法带第二个参数是true的情况即可

字节输入流操作步骤:

A:创建字节输入流对象

B:调用read()方法读取数据,并把数据显示在控制台

C:释放资源

读取数据的方式:

A:int read():一次读取一个字节

B:int read(byte[] b):一次读取一个字节数组:返回值其实是实际读取的字节个数。

我们每次获取到一个字节数据,就把该字节数据转换为了字符数据,然后输出到控制台。

而这一次通过IO流读取数据,写到文本文件,你读取一个字节,我就写入一个字节,没有做任何的转换,它会自己做转换。

计算机是如何识别什么时候该把两个字节转换为一个中文

在计算机中中文的存储分两个字节:

第一个字节肯定是负数。

第二个字节常见的是负数,可能有正数。

注意:虽然我们有两种方式可以读取,但是,请注意,这两种方式针对同一个对象在一个代码中只能使用一个。

通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非常好的。

既然是这样的话,那么,java开始在设计的时候,它也考虑到了这个问题,就专门提供了带缓冲区的字节类。

这种类被称为:缓冲区类(高效类)

写数据:BufferedOutputStream

读数据:BufferedInputStream

构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。

为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?

原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。

String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组

byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组

编码:把看得懂的变成看不懂的

String -- byte[]

解码:把看不懂的变成看得懂的

byte[] -- String

编码问题简单,只要编码解码的格式是一致的。

InputStreamReader(InputStream is):用默认的编码读取数据

InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据

InputStreamReader的方法:

int read():一次读取一个字符

int read(char[] chs):一次读取一个字符数组

OutputStreamWriter(OutputStream out):根据默认编码把字节流的数据转换为字符流

OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流

OutputStreamWriter的方法:

public void write(int c):写一个字符

public void write(char[] cbuf):写一个字符数组

public void write(char[] cbuf,int off,int len):写一个字符数组的一部分

public void write(String str):写一个字符串

public void write(String str,int off,int len):写一个字符串的一部分

把字节流转换为字符流。

字符流 = 字节流 +编码表。

close()和flush()的区别?

A:close()关闭流对象,但是先刷新一次缓冲区。关闭之后,流对象不可以继续再使用了。

B:flush()仅仅刷新缓冲区,刷新之后,流对象还可以继续使用。

字符缓冲流的特殊方法:

BufferedWriter:字符缓冲输出流

public void newLine():根据系统来决定换行符

BufferedReader:字符缓冲输入流

public String readLine():一次读取一行数据

包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null

案例一:复制文件a.txt内容复制到另一个目录下的b.txt中(一次一个字节)

public class CopyFileDemoOne {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("a.txt");
// 封装目的地
FileOutputStream fos = new FileOutputStream("b.txt");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 释放资源(先关谁都行)
fos.close();
fis.close();
}
}


案例二:复制文件a.txt内容复制到另一个目录下的b.txt中(一次读取一个数组长度)

public class CopyFileDemoTwo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("a.txt");
// 封装目的地
FileOutputStream fos = new FileOutputStream("b.txt");
byte [] bys = new byte[1024];
int len = 0
while ((len = fis.read(bys)) != -1) {
fos.write(by,0,len);
}
// 释放资源(先关谁都行)
fos.close();
fis.close();
}
}


案例三:把e:\\海贼王700.mp4复制到当前项目目录下的copy.mp4中

字节流四种方式复制文件:
基本字节流一次读写一个字节: 共耗时:117235毫秒
基本字节流一次读写一个字节数组: 共耗时:156毫秒
高效字节流一次读写一个字节: 共耗时:1141毫秒
高效字节流一次读写一个字节数组: 共耗时:47毫秒
public class CopyMp4Demo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
method1("e:\\海贼王700.mp4", "copy1.mp4");
method2("e:\\海贼王700.mp4", "copy2.mp4");
method3("e:\\海贼王700.mp4", "copy3.mp4");
method4("e:\\海贼王700.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();
}
}


案例四:复制文本文件

分析:
复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流。
通过该原理,我们知道我们应该采用字符流更方便一些。
而字符流有5种方式,所以做这个题目我们有5种方式。推荐掌握第5种。
数据源:
c:\\a.txt -- FileReader -- BufferdReader
目的地:
d:\\b.txt -- FileWriter -- BufferedWriter
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
String srcString = "c:\\a.txt";
String destString = "d:\\b.txt";
method1(srcString, destString);
method2(srcString, destString);
method3(srcString, destString);
method4(srcString, destString);
method5(srcString, destString);
}
// 字符缓冲流一次读写一个字符串
private static void method5(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
// 字符缓冲流一次读写一个字符数组
private static void method4(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
}
bw.close();
br.close();
}
// 字符缓冲流一次读写一个字符
private static void method3(String srcString, String destString)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
}
bw.close();
br.close();
}
// 基本字符流一次读写一个字符数组
private static void method2(String srcString, String destString)
throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
}
fw.close();
fr.close();
}
// 基本字符流一次读写一个字符
private static void method1(String srcString, String destString)
throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
fw.close();
fr.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: