IO流
一.流的分类
根据数据的流向分为:输入流和输出流
- 输入流 :把数据从其他设备上读取到程序中的流
- 输出流 :把数据从程序中写出到其他设备上的流
根据数据的类型分为:字节流和字符流
- 字节流 :以字节为单位(byte),读写数据的流
- 字符流 :以字符为单位(char),读写数据的流
几乎所有的流,都是派生自四个抽象的父类型:
- InputStream ,所有字节输入流的抽象父类型
InputStream 中最核心的三个read方法:
//每次读一个字节,返回值是本次读取的字节值
read()
//每次读多个字节,并存放到指定的字节数组中,返回值是本次一共读取了多个字节(字节数)
read(byte b[])
//每次读多个字节,并存放到指定的字节数组中,返回值是本次一共读取了多个字节(字节数)同时可以指定从数组的什么位置开始存放,以及在数组中最多存放多个字节
read(byte b[], int off, int len)
- OutputStream ,代表字节输出流类型
OutputStream 中最核心的三个write方法:
//写出去一个字节值
write(int b)
//把一个自己数组中的值全部写出去
write(byte b[])
//写出字节数组,指定开始位置,以及写出的字节数量
write(byte b[],int off,int len)
- Reader ,代表字符输入流类型
- Writer ,代表字符输出流类型
常用的字节输入输出流:
基本流:
-
FileInputStream/FileOutputStream文件(*****)
-
ByteArrayInputStream/ByteArrayOutputStream 字节数组
-
PipedInputStream /PipedOutputStream 管道输出流
包装流:
-
ObjectInputStream/ObjectOutputStream
一次性可以书写或者读取对象 又称为序列化/反序列化
前提:书写出去的对象需要实现序列化接口,提供序列化编号
java.io.Serializable 接口
public interface Serializable { }
-
DataInputStream/DataOutputStream
基本数据类型:将数据直接按照特定的形式书写出去同时也可以按照特定的方式读取进来(配合)
常用字符输入输出流:处理的时候由字节变成了字符
Reader:
基本流:
- FileReader(*):文件
- CharArrayReader(*):char类型的数组
- PipedReader
包装流:
-
BufferedReader:提高效率
readLine:读取一行数据
只要用了这个流包装,提高效率
-
InputStreamReader:把字节流转化成字符流
Writer:
基本流:
- FileWriter(*)
- CharArrayWriter(*)
- PipedWriter
包装流:
- PrintWrite && PrintStream (***)
- OutputStreamWrite
二.控制台
使用字节流,从控制台读取数据,以及向控制台中写数据。
java.lang.System
public class Test {
public static void main(String[] args) {
//1.声明流
InputStream in = null;
OutputStream out = null;
//2.创建流,这里使用System中已经创建好的流对象
in = System.in;
out = System.out;
//3.使用流
int data = -1;
try {
data = in.read();
out.write(data);
out.flush();//刷新缓冲区,强制所有数据写出去
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.关闭流
if(in!=null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
三.文件
java.io.File 类是java中对文件和目录的抽象表示,主要用于文件和目录的创建,查找和删除等操作。
File中常用的构造器有:
public class File{
//通过指定文件路径,创建File对象。
public File(String pathname){...}
//通过父路径名和子路径名,创建File实例。
public File(String parent, String child){...}
//通过父路径名和子路径名,创建File实例。
public File(File parent, String child){...}
}
public boolean exists() ,判断此文件或目录是否真的存在
public boolean isDirectory() ,判断File表示的是否是一个目录 public boolean isFile() ,判断file表示的是否是一个文件
String pathname = "Hello.java";
File file = new File(pathname);
System.out.println(file.exists());
System.out.println(file.isDirectory());
System.out.println(file.isFile());
public boolean createNewFile() ,创建一个文新文件
public boolean delete() 删除文件或目录
public boolean mkdir() 创建一个目录
public boolean mkdirs() 创建多级目录
public static void main(String[] args) {
String pathname = "D:/briup/a/b/c";
File dir = new File(pathname);
if(!dir.exists()){
System.out.println(dir.getPath()+"目录是否创建成功:"+dir.mkdirs());
}
File file = new File(dir,"Hello.java");
if(!file.exists()){
try {
System.out.println(file.getName()+"文件是否创建成功:"+file.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
}
//System.out.println(file.getAbsoluteFile()+"文件删除是否成功:"+file.delete());
}