1. IO
1.1. d什么是IO?
I表示Input输入,O表示Output输出,IO表示输入输出。
相对于程序而言,输入是把数据从硬盘、内存、键盘等设备的数据读取到程序中,输出是把程序中的数据保存到硬盘或内存上。
工具包
import java.io.*;
1.2. 什么是IO流?
形象的比喻——水流 ,文件======程序 ,文件和程序之间连接一个管道,水流就在之间形成了,自然也就出现了方向:可以流进,也可以流出.便于理解,这么定义流: 流就是一个管道里面有流水,这个管道连接了文件和程序。
一个流,必有源端和目的端,它们可以是计算机内存的某些区域,也可以是磁盘文件或Internet上的某个URL。
常用输入、输出流
文件在磁盘上通过有两种存储方式:字节(图片、声音、视频,二进制,可以一切文件等等)、字符(文章、小说、一段话,文本文件,只能处理纯文本)。
根据操作对象的类型是字符还是字节可分为两大类: 字符流和字节流。
InputStream是所有字节输入流的祖先,而OutputStream是所有字节输出流的祖先。
Reader是所有读取字符串输入流的祖先,而writer是所有输出字符串的祖先。
|
字节流 |
字符流 |
输入流 |
InputStream |
Reader |
输出流 |
OutputStream |
Writer |
四个超类都是抽象类,应该使用具体的子类。
1.3. IO接口
基于磁盘操作的I/O接口:File
基于字节操作的I/O接口:InputStream和OutputStream
基于字符操作的I/O接口:Writer和Reader
1.4. File
流的本质也是对文件的处理。处理文件的类File,支持新建文件或文件夹、删除文件或文件夹、获取文件或文件夹的属性等。
1.5. 文件操作
新建,删除,绝对路径,相对路径
/**
* java.io.*;包下
* 讲解文件的基本操作
* 绝对路径:从盘符出发 某个硬盘下的某个文件夹中
* 相对路径:相对于当前工程而言
* @author Administrator
*
*/
public class Test01 {
public static void main(String[] args)throws IOException {
//创建文件对象 绝对路径/home/soft01/others/hello.txt
File file = new File("E:/others/hello.txt");
//File file = new File("E:\\others\\hello2.txt");
//相对路径
//File file = new File("hello.txt");
//File file = new File("src\\com\\njwb\\fileoperation\\hello.txt");
//File file = new File("src/com/njwb/fileoperation/hello.txt");
//解析属性
//parseFile(file);
//删除文件
delFile(file);
}
/**
* 解析文件的基本属性
* @param file
*/
public static void parseFile(File file){
//判断文件是否存在
if(file.exists()){
//判断是文件还是文件夹
if(file.isFile()){
System.out.println("这是一个文件");
//解析文件的属性
System.out.println("文件存在");
System.out.println("文件的名称:"+file.getName());
System.out.println("文件的大小:"+file.length()+"字节");
System.out.println("文件的父路径:"+file.getParent());
System.out.println("文件的相对路径:"+file.getPath());
System.out.println("文件的绝对路径:"+file.getAbsolutePath());
}else if(file.isDirectory()){
System.out.println("这是一个文件夹(目录)");
}
}else{
System.out.println("文件不存在");
}
}
/**
* 删除文件,如果不存在,则创建文件
* @param file
* @throws IOException
*/
public static void delFile(File file)throws IOException{
if(file.exists()){ //存在
if(file.delete()){
System.out.println("文件删除成功");
}else{
System.out.println("文件删除失败");
}
}else{
//文件不存在的时候,需要创建
if(file.createNewFile()){
System.out.println("文件创建成功");
}else{
System.out.println("文件创建失败");
}
}
}
}
1.6. 文件夹操作
绝对相对路径,判断是否是文件夹,判断是否存在,创建,删除,在文件夹中创建文件。
/**
* 创建文件对象
* @author Administrator
*
*/
public class Test02 {
public static void main(String[] args)throws IOException {
File path = new File("E:/others");
//在path表示的路径下建1个a1.txt (2个参数 父文件对象,子文件名称字符串)
File file = new File(path,"a1.txt");
//创建文件
if(file.createNewFile()){
System.out.println("a1.txt文件创建成功");
}else{
System.out.println("a1.txt文件创建失败");
}
//在path路径下创建a2.txt (2个参数 父文件的路径名称字符串,子文件名称字符串)
File file2 = new File("E:/others","a2.txt");
//创建文件
if(file2.createNewFile()){
System.out.println("a2.txt文件创建成功");
}else{
System.out.println("a2.txt文件创建失败");
}
//在path路径下,创建a3.txt
File file3 = new File("E:/others/a3.txt");
//创建文件
if(file3.createNewFile()){
System.out.println("a3.txt文件创建成功");
}else{
System.out