一、文件
1.1、什么是文件1.2、文件流 二、常用的文件操作
2.1、创建文件对象的相关构造器和方法2.1、获取文件的相关信息2.3、目录的操作和文件删除 三、IO的原理及流的分类
3.1、java IO 流原理3.2、流的分类 四、IO流体系图-常用的类
4.1、FileInputStream的使用4.2、FileOutputStream的使用4.3、FileReader和FileWriter的使用4.4、节点流和处理流
基本介绍节点流和处理流的区别和联系处理流的优点处理流-BufferedReader和 BufferedWriter的联合使用 五、Properties类
5.1、基本介绍5.2Properties类的使用
一、文件 1.1、什么是文件文件,对我们并不陌生,文件时保存数据的地方,比如大家常见的txt文本,word文档,excel文件…都是文件.他既可以保存一张图片,也可以保存视频,声音…
1.2、文件流流:数据在数据源(文件)和程序(内存)之间经历的路径输入流:数据从数据源(文件)到程序(内存)的路径输出流:数据从程序(内存)到数据源(文件)的路径 二、常用的文件操作 2.1、创建文件对象的相关构造器和方法
new File(String pathname)//根据路径构建一个File对象new File(File parent,String child)//根据父目录文件+子路径构建new File(String parent,String child)//根据父目录+子路径构建createNewFile方法创建新文件
注意 : ‘‘在java中表示转义的意识,所以在写路径时’‘必须以’‘代替可以看做’‘转义了’’,当然还有第二张写法反写’/’
@Test
public void create01(){
//new File(String pathname)根据路径构建一个File对象
File file=new File("e:\f.txt");
try {
//获取返回值,返回是否创建成功
boolean newFile = file.createNewFile();
System.out.println(newFile);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void create02(){
//new File(File parent,String child)//根据父目录文件+子路径构建
File parentFile=new File("E:\");
String child="f2.txt";
File file=new File(parentFile,child);
try {
//获取返回值,返回是否创建成功
boolean newFile = file.createNewFile();
System.out.println(newFile);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void create03(){
//new File(String parent,String child)//根据父目录+子路径构建
String parentPath="E:\";
String child="f3.txt";
File file=new File(parentPath,child);
try {
//获取返回值,返回是否创建成功
boolean newFile = file.createNewFile();
System.out.println(newFile);
} catch (IOException e) {
e.printStackTrace();
}
}
2.1、获取文件的相关信息
| 方法名 | 作用 |
|---|---|
| getName | 获取文件名 |
| getAbsolutePath | 获取文件绝对路径 |
| getParent | 获取文件父级目录 |
| length | 获取文件大小(字节) |
| exists | 文件是否存在 |
| isFile | 是不是一个文件 |
| isDirectory | 是不是一个目录 |
@Test
public void getFileInfo(){
//先创建文件对象
File file = new File("e:\f.txt");
//调用相应的方法,得到对应信息
//getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
System.out.println("文件名字=" + file.getName());
System.out.println("文件绝对路径=" + file.getAbsolutePath());
System.out.println("文件父级目录=" + file.getParent());
System.out.println("文件大小(字节)=" + file.length());
System.out.println("文件是否存在=" + file.exists());
System.out.println("是不是一个文件=" + file.isFile());
System.out.println("是不是一个目录=" + file.isDirectory());
}
2.3、目录的操作和文件删除
注:在java中目录也被当成一个文件
| 方法名 | 作用 |
|---|---|
| mkdir | 创建一级目录 |
| mkdirs | 创建多级目录 |
| delete | 删除空目录或文件 |
什么是流? 可以理解为一组流动的数据
- I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理数据传输。如读/写文件,网络通讯等。Java程序中,对于数据的输入/输出操作以”流(stream)”的方式进行。java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中
按操作数据单位不同分为:
字节流(8 bit) 二进制文件字符流(按字符) 文本文件 按数据流的流向不同分为:
输入流输出流 按流的角色的不同分为:
节点流处理流/包装流
| (抽象基类) | 字节流 | 字符流 |
|---|---|---|
| 输入流 | InputStream | Reader |
| 输出流 | OutputStream | Writer |
- Java的IO流共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的。由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。
- IO体系图
- 文件 VS 流
@Test
public void fileInputStream(){
FileInputStream fileInputStream=null;
try {
//创建FileInputStream对象用于读取文件
fileInputStream=new FileInputStream("e:\f.txt");
//从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止。
//如果返回-1,表示读取完毕
int readData=0;
while ((readData=fileInputStream.read())!=-1){
System.out.print((char)readData );//转换成char
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
//关闭文件流,释放资源
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void fileInputStream02(){
FileInputStream fileInputStream=null;
try {
//创建FileInputStream对象用于读取文件
fileInputStream=new FileInputStream("e:\f.txt");
//从该输入流读取最多 readData.length 字节的数据到字节数组。 此方法将阻塞,直到某些输入可用。
byte[] readData=new byte[8];
int readLen=0;
while ((readLen=fileInputStream.read(readData))!=-1){
System.out.print(new String(readData,0,readLen) );//转换成char
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.2、FileOutputStream的使用
@Test
public void fileOutputStream02() throws IOException {
FileOutputStream fileOutputStream=null;
String outputContext="context";
try {
//创建fileOutputStream对象用于写入文件
fileOutputStream=new FileOutputStream("e:\target.txt");
//将输出内容转换成字节数组写入
fileOutputStream.write(outputContext.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
fileOutputStream.close();
}
}
4.3、FileReader和FileWriter的使用
4.4、节点流和处理流 基本介绍与以上使用相同,只不过new的类变了
- 节点流可以从一个特定的数据源读写数据,如FileReader、FileWriter处理流(也叫包装流)是“连接”在已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,也更加灵活,如BufferedReader、BufferedWriter
- 节点流是底层流/低级流,直接跟数据源相接。处理流(包装流)包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出。处理流(也叫包装流)对节点流进行包装,使用了修饰器设计模式,不会直接与数据相连
- 性能的提高:主要以增加缓冲的方式来提高输入输出的效率。操作的便捷:处理流可能是供了一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活方便
@Test
public void buffered() throws IOException {
BufferedReader bufferedReader=null;
BufferedWriter bufferedWriter=null;
try {
//实例化读写处理流,并连接上节点流
bufferedReader=new BufferedReader(new FileReader("e:\f.txt"));
bufferedWriter=new BufferedWriter(new FileWriter("e:\ff.txt"));
String context=null;
while ((context=bufferedReader.readLine())!=null){
bufferedWriter.write(context);//
bufferedWriter.newline();//插入一个换行
}
bufferedWriter.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
bufferedReader.close();
bufferedWriter.close();
}
}
五、Properties类
5.1、基本介绍
专门用于读写配置文件的集合类
配置文件的格式:
键=值
注意:兼职对不需要有空格,值不需要用引号引起来.默认类型是String
Properties的常见方法
load: 加载配置文件的键值对到Properties对象list:将数据显示到指定设备getProperty(key):根据键获取值setProperty(key,value):设置键值对到Properties对象store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码
@Test
public void readProperties() throws IOException {
//使用 Properties 类来读取 mysql.properties 文件
//1. 创建 Properties 对象
Properties properties = new Properties();
//2. 加载指定配置文件
properties.load(new FileReader("src\mysql.properties"));
//3. 把 k-v 显示控制台
properties.list(System.out);
//4. 根据 key 获取对应的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名=" + user);
System.out.println("密码是=" + pwd);
}
@Test
public void createProperties() throws IOException {
//使用 Properties 类来创建 mysql02.properties 文件
Properties properties=new Properties();
//setProperty方法有就修改,没有就创建
properties.setProperty("user","admin");
properties.setProperty("pwd","123");
properties.store(new FileWriter("src\mysql02.properties"),null);
}



