一、BIO介绍
BIO实际为最原始的io,在java.io包下面,可以理解为Block-io(阻塞io),也可以理解为Base-io(基础io);在jdk1.4后添加NIO以及在jdk1.7后添加了AIO。I/O代表输入流和输出流(输入流:将数据从磁盘读取到应用程序;输出流:将数据从应用程序写入到磁盘),同时I/O在任何计算机程序中都是必须的
二、BIO分类
(1)流的方向:输出流,输入流
(2)流的数据格式:字节流,字符流
(3)流的功能:节点流,处理流
三、BIO常用类
四、BIO使用举例
注:示例分别使用字节流,字符流,缓冲流实现文件的复制
(1)字节流实现文件复制
/**
* 字节流文件复制
* @param f 源文件
* @param f1 目的文件
* @throws IOException
*/
public static void copyByByte(File f, File f1) throws IOException{
//输入字节流
InputStream input = new FileInputStream(f);
//输出字节流
OutputStream output = new FileOutputStream(f1,true);
//一个字节数组
byte[] bt = new byte[1024];
int count = input.read(bt);
//文件读取完标志
while(count != -1){
String str = new String(bt, 0, count);
System.out.println(str);
output.write(bt);
count = input.read(bt);
}
//先关闭输出流,再关闭输入流
output.flush();
output.close();
input.close();
}
(2)字符流实现文件复制
/**
* 字符流文件复制
* @param f 源文件
* @param f1 目的文件
* @throws IOException
*/
public static void copyByChar(File f, File f1) throws IOException{
//输入字符流
Reader reader = new FileReader(f);
//输出字符流
Writer writer = new FileWriter(f1);
//一个字符数组
char ch[] = new char[1024];
int count = reader.read(ch);
while(count != -1){
String str = new String(ch, 0, count);
System.out.println(str);
writer.write(ch);
count = reader.read(ch);
}
//先关闭输出流,再关闭输入流
writer.flush();
writer.close();
reader.close();
}
(3)缓冲流实现文件复制
/**
* 缓冲流文件复制
* @param f 源文件
* @param f1 目的文件
* @throws IOException
*/
public static void copyByBuffer(File f, File f1) throws IOException{
Reader reader = new FileReader(f);
Writer writer = new FileWriter(f1,true);
//字符流包装成缓冲流
BufferedReader br = new BufferedReader(reader);
PrintWriter pw = new PrintWriter(writer,true);
//读取一行
String str = br.readLine();
while(str != null){
System.out.println(str+"\r\n");
pw.write(str.trim());
str = br.readLine();
}
//先关闭输出流,再关闭输入流
pw.flush();
pw.close();
br.close();
}
五、测试示例
(1)测试方法
public static void main(String[] args) throws IOException {
//创建文件夹
File f = new File("D:\\demo");
createFile(f);
//创建文件a
File a = new File("D:\\demo\\a.txt");
FileUtil(a,"create");
//创建文件b
File b = new File("D:\\demo\\b.txt");
FileUtil(b,"create");
//字节流文件复制
//copyByByte(a,b);
//字符流文件复制
//copyByChar(a,b);
//缓冲流文件复制
copyByBuffer(a,b);
}
(2)文件相关操作
/**
* 创建文件夹
* @param f 文件对象
*/
public static void createFile(File f){
//判断文件夹是否存在
if(f.exists()){
System.out.println("文件夹已存在");
}else{
//创建文件
if(f.mkdirs()){
System.out.println("文件夹创建成功!");
}
}
}
/**
* 文件操作
* @param f 文件对象
* @param param 操作标志
* @throws IOException
*/
public static void FileUtil(File f, String param) throws IOException{
if(param.equals("create")){
if(f.exists()){
System.out.println("文件已存在!");
}else{
if(f.createNewFile()){
System.out.println("文件创建成功!");
}
}
}else if(param.equals("delete")){
if(f.exists()){
//删除文件/文件夹
if(f.delete()){
System.out.println("文件/文件夹删除成功!");
}else{
System.out.println("文件/文件夹删除失败!");
}
}else{
System.out.println("文件/文件夹不存在");
}
}else if(param.equals("query")){
File[] files = f.listFiles();
for (File file2 : files) {
System.out.println(file2.getName());
System.out.println(file2.getPath());
}
}
}