IO流
概念
流是一种抽象概念,代表了数据的无序化结构;
从流中取得数据的操作叫提取操作;
向流中添加数据的操作叫做插入操作;
用来输入和输出的流叫做IO流;
换句话来说,IO流就是以流的方式进行输入和输出
流的分类
按照方向分为:输入流、输出流
按照操作单位分为:字节流(针对二进制文件)、字符流(针对文本文件)
字节输入流
字节输入流:InputStream
FileInputStream -----操作文件的字节输入流
BufferedInputStream ----- 高效字节输入流
字节输入流的测试:
进行测试之前在D盘创建ready文件夹,在该文件夹中新建文本文档1.txt
在文本文件1.txt中输入几个字母,并进行保存,然后再开始测试
package cn.tedu.review;
import java.io.*;
/*本类用来练习字节输入流InputStream*/
public class TestIn1 {
public static void main(String[] args) {
method1();//普通的字节输入流
method2();//高效的字节输入流
}
private static void method2() {
//创建一个在本方法中都生效的局部变量,注意手动初始化,引用类型默认值null
BufferedInputStream in = null;
try {
//1.创建流对象
in = new BufferedInputStream(
new FileInputStream("D:\\ready\\1.txt"));
//2.使用流对象
//定义变量,保存结果
int b;
//循环读取数据,只要读到的数据不等于-1,说明还有数据,符合循环条件,继续循环
while ((b = in.read()) != -1){
System.out.println(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//3.关闭流对象
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void method1() {
//创建一个在本方法中都生效的局部变量,注意手动初始化,引用类型默认值null
InputStream in = null;
try {
//1.创建流对象
in = new FileInputStream(new File("D:\\ready\\1.txt"));
//2.使用流对象
//定义变量,保存结果
int b;
//循环读取数据,只要读到的数据不等于-1,说明还有数据,符合循环条件,继续循环
while ((b = in.read()) != -1){
System.out.println(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//3.关闭流对象
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
上面详细介绍了字节输入流,在下面的介绍中就不过多的写注释了,套路都一样
字节输出流
字节输出流:OutputStream
FileOutputStream ----- 操作文件的字节输出流
BufferedOutputStream ----- 高效字节输出流
字节输出流的测试:
package cn.tedu.review;
import java.io.*;
/*本类用来测试字节输出流OutputStream*/
public class TestOut1 {
public static void main(String[] args) {
method1();//普通的字节输出流
method2();//高效字节输出流
}
private static void method2() {
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(
new FileOutputStream("D:\\ready\\1.txt"));
out.write(108);
out.write(105);
out.write(117);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void method1() {
OutputStream out = null;
try {
out = new FileOutputStream("D:\\ready\\1.txt");
out.write(108);
out.write(106);
out.write(120);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符输入流
字符输入流: Reader
FileReader ----- 操作文件的字符输入流
BufferedReader ----- 高效字符输入流
字符输入流的测试
package cn.tedu.review;
import java.io.*;
/*本类用来练习字符输入流Reader*/
public class TestIn2 {
public static void main(String[] args) {
//method1();//普通的字符输入流
method2();//高效的字符输入流
}
private static void method2() {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("D:\\ready\\3.txt"));
int c;
while ((c = in.read()) != -1){
System.out.println(c);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void method1() {
Reader in = null;
try {
in = new FileReader("D:\\ready\\2.txt");
int b;
while ((b = in.read()) != -1){
System.out.println(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符输出流
字符输出流:Writer
FileWriter ----- 操作文件的字符输出流
BufferedWriter ----- 高效字符输出流
字符输出流的测试
package cn.tedu.review;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/*本类用来测试字符输出流Writer*/
public class TestOut2 {
public static void main(String[] args) {
//method1();//普通字符输出流
method2();//高效字符输出流
}
private static void method2() {
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter("D:\\ready\\3.txt"));
out.write(108);
out.write(106);
out.write(120);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void method1() {
Writer out = null;
try {
out = new FileWriter("D:\\ready\\2.txt");
out.write(119);
out.write(108);
out.write(106);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}