本文章为基础知识,供入门者参考。
一.字节流之介绍
字节流:读取字节byte。
分类:输入流和输出流,这是相对于程序而言,程序获取数据(从硬盘中读取数据)称为输入流,程序将数据输出(如保存到硬盘上)称为输出流。
超类:InputStream 所有字节输入流的超类;OutputStream 所有字节输出流的超类。
我们接下来先讨论FileInputStream和FileOutputStream。
二、FileInputStream
从文件系统中的某个文件中获取输入字节,常用于读取二进制数据。
hello.txt中存储的内容是:我是一个程序员
@Test
public void testInputStream() throws IOException {
File file = new File("D:\\hello.txt");
if (!file.exists()) {
file.createNewFile();
}
//read()每次只读取一个字节,返回0~255之间的数据,读到末尾返回-1
System.out.println("--------1--------");
//构造方法 FileInputStream(File file)
FileInputStream in = new FileInputStream(file);
//构造方法 FileInputStream(String name)
//FileInputStream in = new FileInputStream("D:\\hello.txt");
while (true) {
// 每次读取一个字节
int len = in.read();
if (len == -1) {
break;
}
System.out.println(len);
}
in.close();
//read(byte[] b)每次最多可以读取b.length个字节,返回实际读取字节数。读到末尾返回-1
System.out.println("-------2---------");
FileInputStream in1 = new FileInputStream(file);
while (true) {
byte[] b = new byte[5];
int len = in1.read(b);
if (len == -1) {
break;
}
System.out.println(Arrays.toString(b));
System.out.println(len);
}
in1.close();
//read(byte[] b, int offset, int len)每次最多读取len个字节放入byte[]中
//0 <= offset <= b.length; 0 <= len <= b.length - offset
System.out.println("-------3---------");
FileInputStream in2 = new FileInputStream(file);
while (true) {
byte[] b = new byte[5];
int len = in2.read(b, 2, 3);
if (len == -1) {
break;
}
System.out.println(Arrays.toString(b));
System.out.println(len);
}
//skip(long n)跳过n个字节的数据
System.out.println("-------4---------");
FileInputStream in3 = new FileInputStream(file);
while (true) {
byte[] b = new byte[6];
int len = in3.read(b);
if (len == -1) {
break;
}
in3.skip(6);
System.out.println(Arrays.toString(b));
System.out.println(len);
}
}
//运行结果
/*
--------1--------
206
210
202
199
210
187
184
246
179
204
208
242
212
177
-------2---------
[-50, -46, -54, -57, -46]
5
[-69, -72, -10, -77, -52]
5
[-48, -14, -44, -79, 0]
-------3---------
[0, 0, -50, -46, -54]
3
[0, 0, -57, -46, -69]
3
[0, 0, -72, -10, -77]
3
[0, 0, -52, -48, -14]
3
[0, 0, -44, -79, 0]
2
-------4---------
[-50, -46, -54, -57, -46, -69]
6
[-44, -79, 0, 0, 0, 0]
2
*/
三、FileOutputStream
1.构造方法
FileOutputStream(String name)
FileOutputStream(String name, boolean append) //写入到原文件末尾
FileOutputStream(File file)
FileOutputStream(File file, boolean append) //写入到原文件末尾
//如果指定的文件是一个目录而不是一个文件,或该文件不存在而又无法创建它。会抛出FileNotFoundException.
2.常用方法
@Test
public void testOutputStream() throws IOException {
File file = new File("D:\\hello.txt");
if(file.exists()) {
file.createNewFile();
}
//FileOutputStream out = new FileOutputStream(file);
//FileOutputStream out = new FileOutputStream(file,true);
//FileOutputStream out = new FileOutputStream("D:\\hello.txt");
FileOutputStream out = new FileOutputStream("D:\\hello.txt",true);
int b = 97;
out.write(b);//一次写一个字节
//a
String s = "我是一个程序员";
byte[] bytes = s.getBytes("gbk");
out.write(bytes);//一次写出一个字节数组
//我是一个程序员
out.write(bytes,2,bytes.length-2);//从第3个字节开始写出bytes.length-2个字节
//是一个程序员
out.close();
}
四、缓冲流
BufferedInputStream , BufferedOutputStream 。
1.BufferedInputStream 缓冲输入流
1.1 构造方法
BufferedInputStream(InputStream in)
BufferedInputStream(InputStream in, int size) //创建一个缓冲区大小为size的缓冲流,size>0
1.2 常用方法
常用方法read(),同FileInputStream的read()
2.BufferedOutputStream 缓冲输出流
2.1 构造方法
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out, int size)
2.2 常用方法
write(int b)
write(byte[] bytes, int off, int len)
flush() 刷出缓存中的数据。