1. The concept and classification of Io stream:
2. Byte stream
2.1 byte output stream
/*
* public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
* public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
* public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
* public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节, 从偏移量 off开始输出到此输出流。
* public abstract void write(int b) :将指定的字节输出流。
*
* java.io.FileOutputStream extends OutputStream
* FileOutputStream : 文件字节输出流
* 作用: 把内存中的数据写入到硬盘的文件中
*
* 构造方法:
* FileOutputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。
* FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
* 参数:写入数据的目的地
* String name : "一个路径"
* File file : "目的是一个文件"
* 构造方法的作用:
* 1.创建一个FileOutputStream对象
* 2.会根据构造方法中传递的文件/文件路径,创建一个空的文件
* 3.会把创建一个FileOutputStream对象指向创建好的文件
* */
The principle of writing data (from memory to hard disk):
java program--> Jvm (virtual machine)--> os (operating system)--> os calls the method of writing data--> writes the data to the file
public class Demo01 {
/*
* 字节输出流的使用步骤:
* 1.创建一个FileOutputStream对象,构造方法中传递写入数据的目的
* 2.调用FileOutputStream的writer方法,写入到文件中
* 3.释放内存(流使用会占用一定的内容,使用完毕要把内存清空,提供程序的效率)
* */
public static void main(String[] args) {
show01();
}
private static void show01() {
FileOutputStream fileOutputStream = null;
try {
// 1.创建一个FileOutputStream对象,构造方法中传递写入数据的目的
fileOutputStream = new FileOutputStream("D://aa.txt");
// 2.调用FileOutputStream的writer方法,写入到文件中
fileOutputStream.write(84); // T
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
try {
// 3.释放内存
fileOutputStream.close();
} catch (IOException ex) {
}
}
}
}
How to write multiple bytes in byte output stream
/*
public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节, 从偏移量 off开始输出到此输出流。
*/
public class Demo02 {
public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
try {
// fileOutputStream = new FileOutputStream("D://aa.txt",true);
// true 可实现数据的 数据追加续写
fileOutputStream = new FileOutputStream("D://aa.txt");
//比如 write一个数据 (一个字节byte) 就会滋滋滋兹到 磁盘文件
//一个字节一个字节缓冲到磁盘上 效率很低 所以给我们提供了一个 缓冲数组
String content="aaaa";
byte[] bytes = content.getBytes();//把字符串写入字节数组
/* 把字节数组的一部分写入到文件中:
int off:数组的开始索引
int len:写几个字节
*/
fileOutputStream.write(bytes,1,bytes.length-2);
//可以释放资源 也可以把数据刷到磁盘上
//fileOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
try {
//一次性刷完
fileOutputStream.close();
} catch (IOException ex) {
}
}
}
Continuation and line feed of byte output stream
public class Demo03 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("D:\\a.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String s ="abcdnfg".toUpperCase();
byte[] bytes = s.getBytes();
for (int i = 0; i < bytes.length; i++) {
fileOutputStream.write(bytes[i]);
//换行符/r/n
fileOutputStream.write("\r\n".getBytes());
}
//关闭资源
fileOutputStream.close();
}
}
2.2: Byte input stream
/*
* java.io.InputStream: 字符输入流
* 此抽象方法是表示字节输入流的所有方法的超类
*
* 定义了所有的子类共性的方法:
* int read();从输入流中读取数据的下一个字节。
* int read(byte[] b);从输入流中读取一些字节数,并将它们存储到字节数组 b中
* void close();关闭此输入流并释放与此流相关联的任何系统资源。
*
* java.io.FileInputStream extends InputStream
* FileInputStream :文件字节输入流
* 作用: 把硬盘中的数据读取到内存的使用
* 构造方法:
* FileInputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。
* FileInputStream(String name) 创建文件输出流以指定的名称写入文件。
* 参数:读取文件的数据源
* File file "文件路径"
* String name "文件路径"
* 构造方法的作用:
* 1.创建一个FileInputStream对象
* 2.会把FileInputStream对象指定构造方法要读取的文件
* */
The principle of reading data: (from hard disk to memory)
java program--> Jvm (virtual machine)--> os (operating system)--> os calls the method of reading data--> read file
public class InDome01 {
/*
* 1.创建FileInputStream对象, 绑定路径
* 2.使用方法read()
* 3.释放资源
* */
public static void main(String[] args) throws IOException {
FileInputStream fi=null;
fi=new FileInputStream("D:\\aa.txt");
//读取文件中的一个字节并返会 读取到末尾返回-1
int read = fi.read();
System.out.println(read);
read = fi.read();
System.out.println(read);
/*
* 麻烦呀 难受呀
* 不知道这个文件里多少字节
* 所用 while循环
*
* 布尔表达式 (b=fi.read())!=-1
* 1.fi.read() 读取一个字节
* 2.b=fi.read() 读取到的字节赋值给变量b
* 3.(b=fi.read())!=-1; 判断变量b是否不等于-1
* */
int b; // 记录读取到的字节
while ((b=fi.read())!=-1){
System.out.println(b);
}
}
}
Principle of byte input stream:
Byte input stream reads multiple bytes at once
public class InDome02 {
public static void main(String[] args) throws IOException {
FileInputStream fi=null;
fi=new FileInputStream("D:\\aa.txt");
byte[] bytes = new byte[6];
int b;//实际读取的长度
while ((b=fi.read(bytes))!=-1){
//offset起始长度
//int len:读出几个字节
System.out.println(new String(bytes, 0, b));
}
}
}
Let's take a look at the character stream