Java基础-IO(三)

目录:

  1. Java IO主要相关类体系结构
  2. IO读写主要方法
  3. 读写Demo
  4. 总结

1. Java IO主要相关类体系结构

根据操作类型划分如下:

按照字符字节类型归类:

2.IO读写主要方法

根据以上的体系结构来划分,主要分配字节流与字符类,也就是关键的4个抽象类:InputStream、OutputStream、Reader、Writer。这四个抽象的最基本的两个方法read()、write()读写。

1
2
3
4
5
6
7
8
9
10
11
/**
*字符流
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new FileWriter("d:\\lishijia.txt"));

/**
*字节流
*/
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("D:\\bin32_64\\setup.ini"));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("d:\\lishijia.txt"));

InputStream类:

方法名称 描述
public abstract int read() 读取数据
public int read(byte b[]) 将读取到的数据放在 byte 数组中,该方法实际上是根据下面的方法实现的,off 为 0,len 为数组的长度
public int read(byte b[], int off, int len) 从第 off 位置读取 len 长度字节的数据放到 byte 数组中,流是以 -1 来判断是否读取结束的。返回值为读取字节数组的长度
public long skip(long n) 跳过指定个数的字节不读取,想想看电影跳过片头片尾
public int available() 返回可读的字节数量
public void close() 读取完,关闭流,释放资源
public synchronized void mark(int readlimit) 标记读取位置,下次还可以从这里开始读取,使用前要看当前流是否支持,可以使用 markSupport() 方法判断
public synchronized void reset() 重置读取位置为上次 mark 标记的位置
public boolean markSupported() 判断当前流是否支持标记流,和上面两个方法配套使用

OutputStream类:

方法名称 描述
public abstract void write(int b) 写入一个字节,可以看到这里的参数是一个 int 类型,对应上面的读方法,int 类型的 32 位,只有低 8 位才写入,高 24 位将舍弃。
public void write(byte b[]) 将数组中的所有字节写入,和上面对应的 read() 方法类似,实际调用的也是下面的方法。
public void write(byte b[], int off, int len) 将 byte 数组从 off 位置开始,len 长度的字节写入
public void flush() 强制刷新,将缓冲中的数据写入
public void close() 关闭输出流,流被关闭后就不能再输出数据了

Reader类:

方法名称 描述
public int read(java.nio.CharBuffer target) 读取字节到字符缓存中
public int read() 读取单个字符
public int read(char cbuf[]) 读取字符到指定的 char 数组中
abstract public int read(char cbuf[], int off, int len) 从 off 位置读取 len 长度的字符到 char 数组中
public long skip(long n) 跳过指定长度的字符数量
public boolean ready() 和上面的 available() 方法类似
public boolean markSupported() 判断当前流是否支持标记流
public void mark(int readAheadLimit) 标记读取位置,下次还可以从这里开始读取,使用前要看当前流是否支持,可以使用 markSupport() 方法判断
public void reset() 重置读取位置为上次 mark 标记的位置
abstract public void close() 关闭流释放相关资源

Writer类:

方法名称 描述
public void write(int c) 写入一个字符
public void write(char cbuf[]) 写入一个字符数组
abstract public void write(char cbuf[], int off, int len) 从字符数组的 off 位置写入 len 数量的字符
public void write(String str) 写入一个字符串
public void write(String str, int off, int len) 从字符串的 off 位置写入 len 数量的字符
public Writer append(CharSequence csq) 追加写入一个字符序列
public Writer append(CharSequence csq, int start, int end) 追加写入一个字符序列的一部分,从 start 位置开始,end 位置结束
public Writer append(char c) 追加写入一个 16 位的字符
abstract public void flush() 强制刷新,将缓冲中的数据写入
abstract public void close() 关闭输出流,流被关闭后就不能再输出数据

3.读写Demo

通过控制台输入操作字符流读写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Test
public void test1()throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("please input char");
char c;
c = (char)reader.read();
System.out.println("you put the char is " + c);
}

@Test
public void test2() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("please input char");
String c = "";
do{
c = reader.readLine();
System.out.println("you put the char is " + c);
}while(!c.equals("quit"));
System.out.println("finish input");
}

@Test
public void test3() throws IOException{
BufferedWriter writer = new BufferedWriter(new FileWriter("d:\\lishijia.txt"));
writer.write("通过字符流写入字符");
writer.write("abcdef", 1, 3); //从索引1位置开始写入,写两个2字符
writer.flush();
writer.close();
System.out.println("finish the test3 method");
}

通过字节流操作读写,下面的这个inputStream.read(bytes)之后,一定要注意最后一次的读写,即如果使用bytes,那么一定要根据len这个所读取的长度来使用,要不然在最后一次使用的时候会转换上一次的字节数据内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@Test
public void test4() throws IOException{
FileInputStream inputStream = new FileInputStream("D:\\bin32_64\\setup.ini");
byte[] bytes = new byte[512];
int len = 0;
StringBuffer buffer = new StringBuffer();
while( (len = inputStream.read(bytes)) != -1){
String s = new String(bytes,0, len);
buffer.append(s);
}
System.out.println(buffer.toString());
}

@Test
public void test5() throws IOException{
FileInputStream inputStream = new FileInputStream("D:\\bin32_64\\setup.ini");
FileOutputStream outputStream = new FileOutputStream("d:\\lishijia.txt");
byte[] bytes = new byte[512];
int len = 0;
while ( (len = inputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, len);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}

@Test
public void test6() throws IOException{
/**
* 使用缓存冲流
*/
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("D:\\bin32_64\\setup.ini"));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("d:\\lishijia.txt"));

byte[] bytes = new byte[512];
int len = 0;
while( (len = inputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, len);
}

inputStream.close();
outputStream.flush();
outputStream.close();
}

4.总结

Java IO类确实有点多,但是根据以上这种梳理一下整体体系结构,然后自己在详细的一个一个使用下就会发现其实就只有4个大类InputStream、OutputStream、Reader、Writer。

分享到 评论