前言
I/O流在Java中是一个不可或缺的框架,当我们需要使用文件的时候就需要使用到这个,所以在总结玩常用类之后就可以马上来学习I/O流了
一、什么是流?
在电脑中会有内存和硬盘这两种说法,而我们的程序就是存储在内存中,而其它文件则存储在硬盘中,当我们需要把硬盘中的文件写入内存或者把内存中的程序保存到硬盘中的时候,就需要使用到流。总而言之,流就是文件在内存和硬盘之间的通道
二、流的分类
1.根据流向来分
1)输入流
2)输出流
2.根据单位来分
1)字节流
2)字符流
3.根据功能来分
1)节点流
2)过滤流
三.字节流的使用
1.FileInputStream
0)cmd.txt文件
1)read()方法
在FileInputStream如果不给参数,那么就是按一个字节一个字节的来读文件
示例public class Test2 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\cmd.txt");
int read =0;
while((read=fileInputStream.read()) != -1){
System.out.print((char)read);
}
fileInputStream.close();
}
}
输出:
python has an easy syntax and user-friendly interaction.
但是这种方法是一个字节一个字节的来读文件,所以在效率上不是很理想
2)read(byte[] byte)方法
如果给定了一个字节数组参数,那么就会把读取到的字节存放在这个数组里面
示例代码public class Test2 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\cmd.txt");
//创建一个大小为1kb的数组用来就收数据
byte[] bytes = new byte[1024];
//通过循环读取文件并存放到数组中
//当文件读取完成后会返回一个-1所以,当返回值为-1时退出循环
while(fileInputStream.read(bytes) != -1){
//读取数组中的元素
for (byte aByte : bytes) {
System.out.print((char)aByte);
}
}
fileInputStream.close();
}
}
输出:
python has an easy syntax and user-friendly interaction.
3)read(byte[] byte,int off,int lenth)方法
这个方法和前一个方法一样,只不过是限制了读取的其实位置和长度
示例代码public class Test2 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\cmd.txt");
//创建一个大小为1kb的数组用来就收数据
byte[] bytes = new byte[1024];
//通过循环读取文件并存放到数组中
//当文件读取完成后会返回一个-1所以,当返回值为-1时退出循环
//这里选择其实位置为第一个字节,然后往后的12个字节
while(fileInputStream.read(bytes,0,12) != -1){
//读取数组中的元素
for (byte aByte : bytes) {
System.out.print((char)aByte);
}
}
fileInputStream.close();
}
}
输出:
python has a
这个的输出结果我们就可以看到是我们预期的长度了
2.FileOutputStream
0)构造方法
FileOutputStream这个类的构造方法分为两种
源代码//第一种
public FileOutputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null, false);
}
//第二种
public FileOutputStream(String name, boolean append) throws FileNotFoundException {
this(name != null ? new File(name) : null, append);
}
第一种不带一个布尔类型的append 的时候,会自动判断所给文件是否存在,如果不存在则新建一个文件,但是若存在则会默认覆盖原本文件里面的所有内容,这点我们可以看三元判断可知
第二种则是会让我们去输入一个append的值,如果值为true那么在三元判断的时候就会执行append的值,从而在原文件后面**追加内容**,相反如果给定的值为false则会和第一种方法一样去覆盖掉原文件的全部内容
1)write(byte[] byte)方法
通过传入一个字节数组,再通过FileOutputStream流把数组中的内容写入文件
示例代码public class Test2 {
public static void main(String[] args) throws IOException {
//是否在文件内容后追加内容:是
FileOutputStream fileOutputStream = new FileOutputStream("D:\\cmd.txt",true);
//创建一个字节大小为10的数组
byte[] bytes = new byte[10];
//使用Arrays类自动装填
Arrays.fill(bytes, (byte) '9');
for (byte aByte : bytes) {
System.out.println((char)aByte);
}
//向文件写入数组内容
fileOutputStream.write(bytes);
//关闭流
fileOutputStream.close();
}
}
但是看到这个方法的实现是否觉得有些臃肿,所以我们可以借助String的getByte()方法来简化实现
源代码
public byte[] getBytes() {
return StringCoding.encode(this.coder(), this.value);
}
我们可以看到这个方法直接返回的是一个byte[]类型的值,所以我们可以直接用在write方法上
示例代码public class Test2 {
public static void main(String[] args) throws IOException {
//是否在文件内容后追加内容:是
FileOutputStream fileOutputStream = new FileOutputStream("D:\\cmd.txt",true);
String s = "ciallo~";
//使用String的getBytes方法向文件写入数组内容
fileOutputStream.write(s.getBytes());
//关闭流
fileOutputStream.close();
}
}
输出结果
2)write(byte[] byte,int off,int lenth)方法
这个方法和单个byte的使用方法近乎相同,只不过是多了一个其实位置和长度
示例代码public class Test2 {
public static void main(String[] args) throws IOException {
//是否在文件内容后追加内容:是
FileOutputStream fileOutputStream = new FileOutputStream("D:\\cmd.txt",true);
String s = "ciallo~";
//使用String的getBytes方法向文件写入数组内容,并从滴1个位置开始写入3个长度
fileOutputStream.write(s.getBytes(),1,3);
//关闭流
fileOutputStream.close();
}
}
3.字节流复制文件
原理:只需要使用字节流的输入流方法来把文件存储到内存中,然后再使用输出流,把内存中存储到的文件再复制到硬盘中
读完再写的示例代码public class Test2 {
public static void main(String[] args) throws IOException {
//创建输入流,并指定目标文件
FileInputStream fileInputStream =
new FileInputStream("D:\\picture\\pixiv\\校园.png");
//创建字节数组
byte[] bytes = new byte[3051209];
//使用流读取文件并存入数组中
int read = fileInputStream.read(bytes);
//创建输出流,并创建新文件
FileOutputStream fileOutputStream =
new FileOutputStream("D:\\picture\\pixiv\\test.png", false);
//使用流将读取的文件复制到新建的文件中去
fileOutputStream.write(bytes);
//反馈成功信息
System.out.println("复制成功!");
//关闭流
fileInputStream.close();
fileOutputStream.close();
}
}
边读边示例代码
public class Test2 {
public static void main(String[] args) throws IOException {
//创建输入流,并指定目标文件
FileInputStream fileInputStream =
new FileInputStream("D:\\picture\\pixiv\\校园.png");
//创建字节数组
byte[] bytes = new byte[3051209];
//使用流读取文件并存入数组中
int read = 0;
//创建输出流,并创建新文件
FileOutputStream fileOutputStream =
new FileOutputStream("D:\\picture\\pixiv\\test.png", false);
//边读边写
while((read=fileInputStream.read(bytes)) !=-1){
//使用流将读取的文件复制到新建的文件中去
fileOutputStream.write(bytes,0,read);
}
//反馈成功信息
System.out.println("复制成功!");
//关闭流
fileInputStream.close();
fileOutputStream.close();
}
}
4.BufferInputStream
本质是FileInputStream的增强类,其继承了FilterInputStream,并在此基础上创建了缓存区,所以当我们使用这个类后当访问重复的文件时,就不需要一遍又一遍的去硬盘中访问了,而是可以在缓存中直接访问
示例代码public class Test2 {
public static void main(String[] args) throws IOException {
//读取文件
FileInputStream fileInputStream = new FileInputStream("D:\\cmd.txt");
//创建字节缓冲流
BufferedInputStream bufferedInputStream =
new BufferedInputStream(fileInputStream,3051209);
int read = 0;
//当读取完毕后,输出读取到的数据
while((read = bufferedInputStream.read()) != -1){
System.out.print((char)read);
}
System.out.println();
System.out.println("读取成功");
//关闭流
bufferedInputStream.close();
}
}
输出:
9999999999ciallo~
读取成功
其实除了读的时候时调用的Buffer的,其他与File字节流的使用方法并没有太大方法
5.BufferOutputStream
本质是FileOutputStream的增强类,其继承了FilterInputStream,并在此基础上创建了缓存区,当我们需要将内容写入文件的时候就可以先写入缓存区然后再写入文件
示例代码public class Test2 {
public static void main(String[] args) throws IOException {
//创建基本file流,并指定文件
FileOutputStream fileOutputStream = new FileOutputStream("D:\\cmd.txt",true);
//创建buffer流,并指定缓存大小
BufferedOutputStream bufferedOutputStream =
new BufferedOutputStream(fileOutputStream,3051209);
//创建需要写入的内容
String s = "YuKikaZeSaMaNaNoDa!";
//写入
bufferedOutputStream.write(s.getBytes());
System.out.println("写入成功");
//关闭流
bufferedOutputStream.close();
}
}
四.对象流的使用
0.实验对象,文件及注意事项
1)示例代码
import java.io.Serializable;
public class Cat implements Serializable {
private String catName;
private Integer catAge;
public Cat(String catName, Integer catAge) {
this.catName = catName;
this.catAge = catAge;
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
public Integer getCatAge() {
return catAge;
}
public void setCatAge(Integer catAge) {
this.catAge = catAge;
}
@Override
public String toString(){
return "猫名字是:"+this.catName+"\t"
+"猫年龄是:"+this.catAge;
}
@Override
public boolean equals(Object obj){
//1、判断两个类是否相同
if(this.getClass() == obj.getClass()){
return true;
}
//2、判断obj是否为空
if (obj == null){
return false;
}
//3、判断引用指向的实际类型是否相同
if(obj instanceof Cat){
//4、类型强制转换
Cat cat = (Cat)obj;
//5、逐个比较属性值是否相同
if(this.getCatName().equals(cat.getCatName()) || this.getCatAge().equals(cat.getCatAge())){
return true;
}
}
return false;
}
}
2)示例文件
3)注意事项
(1)在我们进行序列化的时候,编译器可能会报NotSerializableException这个异常,而这个异常我们需要在我们的实体类中去实现Serializable这个接口就可以正常的序列化了
(2)在序列化和反序列化的时候需要抛出ClassNotFound这个异常
(3)如果对象的属性有对象的话,那个对象也要实现Serializable这个接口
(4)若对象中某个属性修饰符为transient,那么这个属性就无法序列化
(5)静态属性无法序列化
(6)若要实现多个对象的序列化,请借助集合
1.序列化及其反序列化
我们知道流可以传输各种各样类型的文件或者数据,而流也可以传输对象,对象写入硬盘的过程叫做序列化,反之从硬盘读取到内存的过程叫做反序列化
示例代码public class Test2 {
public static void main(String[] args) throws IOException,ClassNotFoundException{
//创建基本file输出流,并指定文件
FileOutputStream fileOutputStream = new FileOutputStream("D:\\new.txt",false);
//创建对象输出流
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//创建猫对象
Cat cat = new Cat("dear",12);
//进行序列化
objectOutputStream.writeObject(cat);
System.out.println("序列化完成");
System.out.println("开始反序列化");
//创建基本file输入流,并指定文件
FileInputStream fileInputStream = new FileInputStream("D:\\new.txt");
//创建对象输出
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//将反序列化得到的结果强制转换成我们需要的猫类
Cat ob = (Cat)objectInputStream.readObject();
System.out.println("反序列化完成");
//输出这个对象
System.out.println(ob.toString());
//关闭流
objectInputStream.close();
objectOutputStream.close();
}
}
输出:
序列化完成
开始反序列化
猫名字是:dear 猫年龄是:12
反序列化完成
五.字符流的使用
1.字符流与字节流的区别
当我们使用字节流读取文件的时候时一个字节一个字节的读取文件的,但是我们可以在文件里面添加中文,这时候我们会发现,读取后会输出一串乱码
示例这里输出的一串乱码正是因为字节流的原因,正是因其一个字节一个字节的读,才导致这样的乱码。我们的文件保存的格式是UTF-8
所以我们的中文汉字一个字就占用了三个字节,所以当一个一个的读后就把原来三个字节组成的文字拆分了,这样就输出了乱码。而为了解决这个问题我们就需要采用字符流来读取和存入
2.FileRead类
1)read()方法
这种不带参数的read方法就是按照文件对应的字符来读取
代码示例public class Test2 {
public static void main(String[] args) throws IOException,ClassNotFoundException{
FileReader fileReader= new FileReader("D:\\new.txt");
int read =0;
while((read=fileReader.read()) != -1){
System.out.print((char)read);
}
fileReader.close();
}
}
2)read(char[] char)方法
若带这个字符数组的参数的话,就相当于自己创建了一个缓存区用来存放读取到的数据,最后再一并写入硬盘
代码示例public class Test2 {
public static void main(String[] args) throws IOException,ClassNotFoundException{
FileReader fileReader = new FileReader("D:\\new.txt");
char[] chars = new char[1024];
int read = 0;
while((read = fileReader.read(chars))!=-1){
System.out.println(new String(chars));
}
fileReader.close();
}
}
输出:
我们知道流可以传输各种各样类型的文件或者数据,而流也可以传输对象,对象写入硬盘的过程叫做序列化,反之从硬盘读取到内存的过程叫做反序列化
3)read(char[] char,int off,int lenth)方法
和只带char数组的使用方法相同,但是可以在给定的缓存区较小且不足以一次性读完文件的时候可以避免在读最后几个字符时因小于给定缓存区而造成的自动填充
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
FileReader fileReader = new FileReader("D:\\new.txt");
char[] chars = new char[24];
int read = 0;
while ((read = fileReader.read(chars)) != -1) {
System.out.println(new String(chars, 0, read));
}
fileReader.close();
}
}
输出:
我们知道流可以传输各种各样类型的文件或者数据,而
流也可以传输对象,对象写入硬盘的过程叫做序列化,
反之从硬盘读取到内存的过程叫做反序列化
3.FileWrite类
1)write(String s)方法
这个方法就是传入一个String类型的字符串,然后再写入到文件
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字符输出流并指定输出文件
FileWriter fileWriter = new FileWriter("D:\\new.txt",true);
String s = "ciallo~";
//写入
fileWriter.write(s);
fileWriter.close();
}
}
输出结果:
2)write(char[] char)方法
这个传入了一个char数组的方法,而这个方法一般是配合着字符输入流的用法,但是也可以单独使用
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字符输出流并指定输出文件
FileWriter fileWriter = new FileWriter("D:\\new.txt",true);
//创建一个大小为1024的字符数组
char[] chars = new char[1024];
//自动填充数组
Arrays.fill(chars,'你');
//写入文件
fileWriter.write(chars);
fileWriter.close();
}
}
4.字符流的复制文件
0)注意事项和文件内容
无法复制图片和二进制文件
文件内容
1)开始复制
代码示例
public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字符输入流并指定目标文件
FileReader fileReader = new FileReader("D:\\cmd.txt");
//创建字符输出流并指定输出文件
FileWriter fileWriter = new FileWriter("D:\\new.txt");
//创建缓存区
char[] chars = new char[1024];
int read = 0;
//在读文件的同时写入文件
while((read = fileReader.read(chars))!=-1){
//从开始位置一直写入到读取结束位置
fileWriter.write(chars,0,read);
//清空缓存区
fileWriter.flush();
}
System.out.println("复制完成");
//关闭流
fileReader.close();
fileWriter.close();
}
}
2)结果
5.BufferedRead
1)BufferedRead的read方法
这个方法的使用本质和字节流的BufferInputStream的使用方法并没有太大区别,同样是使用read来读取文件并输出,且亦可自行创建char数组来实现缓存区
自行创建缓存区代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字符输出流并指定输出文件
FileReader fileReader = new FileReader("D:\\new.txt");
//创建字符BufferedRead流,并传入字符输入流
BufferedReader bufferedReader = new BufferedReader(fileReader);
//设定一个字符数组
char[] chars = new char[1024];
int read = 0;
//读取文件
while((read=bufferedReader.read(chars))!=-1){
//输出
System.out.println(new String(chars,0,read));
}
//关闭流
bufferedReader.close();
}
}
不创建代码示例
public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字符输出流并指定输出文件
FileReader fileReader = new FileReader("D:\\new.txt");
//创建字符BufferedRead流,并传入字符输入流
BufferedReader bufferedReader = new BufferedReader(fileReader);
int read = 0;
//读取文件
while((read=bufferedReader.read())!=-1){
System.out.print((char)read);
}
//关闭流
bufferedReader.close();
}
}
2)BufferedRead的readLine方法
这个方法是用来读取一行的字符,所以当我们的文本有多行时就需要使用循环,且本方法返回值是一个String类型,所以当读取完毕后会返回Null
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字符输出流并指定输出文件
FileReader fileReader = new FileReader("D:\\new.txt");
//创建字符BufferedRead流,并传入字符输入流
BufferedReader bufferedReader = new BufferedReader(fileReader);
String read = null;
//读取文件,并当读到空时跳出循环
while((read=bufferedReader.readLine())!=null){
System.out.println(read);
}
//关闭流
bufferedReader.close();
}
}
6.BufferedWrite
1)write方法
此方法与字节流的无二异,任然是通过这个方法向文件里写入
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字符输出流,并决定在源文件后面追加写入文件
FileWriter fileWriter = new FileWriter("d:\\new.txt",true);
//创建BufferedWrite流并传入字符流
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//向文件写入
for (int i = 0; i <5 ; i++) {
bufferedWriter.write("hello");
//换行,且这个换行会根据你的操作系统来自行判断
bufferedWriter.newLine();
//清空缓存区
bufferedWriter.flush();
}
//关闭流
bufferedWriter.close();
System.out.println("写入完成");
}
}
2)newLine方法
在之前的字节流的时候,我们向文件里面写入文件后我们还需要自己在写入的地方添加\n,\r等,但是使用了这个方法后系统就会自动换行
7.prinWrite类
支持了原样输出,和更多种类的输出,实现了print和println的封装类
代码示例
public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字符输出流,并决定在源文件后面追加写入文件
PrintWriter printWriter = new PrintWriter("d:\\print.txt");
printWriter.println(97);
printWriter.println('a');
printWriter.println(true);
printWriter.close();
System.out.println("写入完成");
}
}
六.转换流的使用
1.InputStreamReader
把字节转换成字符,从硬盘读入内存
代码示例
public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字节流
FileInputStream fileInputStream = new FileInputStream("d:\\new.txt");
//创建转换流
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
int read= 0;
//读文件
while((read=inputStreamReader.read())!=-1){
System.out.print((char)read);
}
//关闭流
inputStreamReader.close();
}
}
2.OutputStreamWriter
把字节转换成字符,从内存写入硬盘
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建字节流
FileOutputStream fileOutputStream = new FileOutputStream("d:\\new.txt");
//创建转换流,并指定编码格式
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"utf-8");
//写入到目标文件
outputStreamWriter.write("yahoo");
//关闭流
outputStreamWriter.close();
System.out.println("写入完毕");
}
}
七.File类
1.文件操作
1)文件创建
通过createNewFile()方法的使用来创建一个新文件
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件
//指定文件创建路径
File file = new File("d:\\file.txt");
boolean result=false;
//判断文件是否存在
if(!file.exists()){
file.createNewFile();
//把结果改为true
result = true;
//输出创建成功的结果
System.out.println(result);
}
//创建失败
System.out.println(result);
}
}
2)文件删除
通过使用delete()方法和deleteOnExit()方法来删除文件
前者则是在执行后立刻删除文件,后者则是在jvm结束后帮你删除文件
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件
//指定文件创建路径
File file = new File("d:\\file.txt");
//删除文件
//file.delete();
file.deleteOnExit();
Thread.sleep(5000);
//查看文件信息
System.out.println(file.getAbsolutePath());
}
}
3)文件信息
通过getAbsolutePath,getName,getPath,length这几个方法来获取文件的绝对路径,文件名,给定路径,文件长度
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件
//指定文件创建路径
File file = new File("d:\\file.txt");
file.createNewFile();
//获取文件的绝对路径,文件名,给定路径,文件长度
System.out.println(file.getAbsolutePath()+file.getName()+file.getPath()+file.length());
}
}
4)文件判断
主要是通过canRead,isHidden,lastModified这三个方法来获取文件是否可读,是否为隐藏文件,并获取文件的创建时间
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件
//指定文件创建路径
File file = new File("d:\\file.txt");
file.createNewFile();
//判断文件
//获取文件的创建时间
long l = file.lastModified();
//判断文件是否可读
boolean canRead = file.canRead();
//判断文件是否为隐藏文件
boolean isHidden = file.isHidden();
System.out.println(canRead+" "+isHidden);
}
}
2.文件夹操作
1)文件夹创建
与文件的创建的过程类似,只是把creatNewFile改成了mkdir或者mkdirs,这两个方法的区别仅仅只是前者创建一级目录,后者创建多级目录
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件夹
//指定文件夹创建路径
File file = new File("d:\\aaa\\bbb\\ccc");
boolean result=false;
//判断文件夹是否存在
if(!file.exists()){
file.mkdirs();
//把结果改为true
result = true;
//输出创建成功的结果
System.out.println(result);
}else{
//创建失败
System.out.println(result);
}
}
}
2)文件夹删除
与文件删除操作相同,且使用方法相同
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件夹
//指定文件夹创建路径
File file = new File("d:\\aaa\\bbb\\ccc");
boolean result=false;
//判断文件夹是否存在
if(!file.exists()){
file.mkdirs();
//把结果改为true
result = true;
//输出创建成功的结果
System.out.println(result);
}else{
//创建失败
System.out.println(result);
}
//删除文件夹
//file.delete();
file.deleteOnExit();
Thread.sleep(5000);
}
}
3)文件夹信息
获取文件夹的绝对路径,文件名,给定路径,父路径,创建时间
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件夹
//指定文件夹创建路径
File file = new File("d:\\aaa\\bbb\\ccc");
boolean result=false;
//判断文件夹是否存在
if(!file.exists()){
file.mkdirs();
//把结果改为true
result = true;
//输出创建成功的结果
System.out.println(result);
}else{
//创建失败
System.out.println(result);
}
//获取文件夹的绝对路径,文件名,给定路径,父路径,创建时间
System.out.println(file.getAbsolutePath()+file.getName()+file.getPath()+file.getParent());
System.out.println(new Date(file.lastModified()).toLocaleString());
}
}
4)文件夹判断
判断文件是否
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件夹
//指定文件夹创建路径
File file = new File("d:\\aaa\\bbb\\ccc");
boolean result=false;
//判断文件夹是否存在
if(!file.exists()){
file.mkdirs();
//把结果改为true
result = true;
//输出创建成功的结果
System.out.println(result);
}else{
//创建失败
System.out.println(result);
}
//判断文件夹是否可读
boolean canRead = file.canRead();
//判断文件夹是否为隐藏文件夹
boolean isHidden = file.isHidden();
System.out.println(canRead+" "+isHidden);
}
}
5)文件夹遍历
通过list()方法来查看文件夹下的文件
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件夹
//指定文件夹创建路径
File file = new File("D:\\picture\\pixiv");
boolean result=false;
//判断文件夹是否存在
if(!file.exists()){
file.mkdirs();
//把结果改为true
result = true;
//输出创建成功的结果
System.out.println(result);
}else{
//创建失败
System.out.println(result);
}
//
String[] list = file.list();
for (String s : list) {
System.out.println(s);
}
}
}
3.FileFilter接口
这个接口是通过传入一个File类型的参数进行过滤的,当你使用listFile这个方法的时候就可以在方法里面实现FileFilter接口并重写方法,然后就可以过滤你不需要的文件
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//创建文件夹
//指定文件夹创建路径
File file = new File("D:\\2019-2020学生工作处(校学生党员督察队)文件文档\\19-20");
//遍历未过滤的文件
String[] list = file.list();
for (String s : list) {
System.out.println(s);
}
System.out.println("============================");
System.out.println("使用过滤器后");
//使用listFiles方法,并实现FileFilter接口
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File filed) {
//判断当后缀名为.jpg的时候返回该文件
if(filed.getName().endsWith(".jpg")) {
return true;
}
return false;
}
});
//打印过滤得到的文件
for (File file1 : files) {
System.out.println(file1.getName());
}
}
}
4.递归遍历文件和递归删除文件
1)一次运行访问目标文件夹下所有子文件夹及子文件
我们使用list()方法是不能一次就把目标文件下的子文件夹里面的文件或者文件夹访问完毕的,所以我们需要自己创建一个方法来自己调用实现,当查找到文件下还有文件夹时就递归调用,知道结束
代码示例public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, NullPointerException {
//调用
Look(new File("D:\\aaa"));
}
//遍历查看文件夹
public static void Look(File dir) {
//把遍历得到的文件&文件夹放入File数组
File[] dirs = dir.listFiles();
for (File file : dirs) {
//如果时文件夹则递归调用
if (file.isDirectory()) {
Look(file);
} else {
//输出文件名
System.out.println(file.getName());
}
}
}
}
2)一次运行删除目标文件夹下所有子文件夹及文件
我们要删除一个文件夹的时候,如果里面任然还有文件或者文件夹,那么我们就得先把文件夹里面的文件或者文件夹删除才能删除这个文件夹,所以我们在使用delete方法的时候效率会非常低,所以此时我们需要递归调用来访问文件夹判断里面是否还有文件或者文件夹,进而删除
代码示例 //遍历删除文件夹及其文件
public static void Delete(File dir){
//把遍历得到的文件&文件夹放入File数组
File[] dirs1 = dir.listFiles();
//如果文件不为空且文件长度大于0则遍历数组
if(dirs1 != null || dirs1.length>0){
for (File file : dirs1) {
//如果遍历得到的还是个文件夹则递归调用
if(file.isDirectory()){
Delete(file);
}else{
System.out.println(file.getAbsolutePath()+"删除:"+file.delete());
}
}
}
System.out.println(dir.getAbsolutePath()+"删除:"+dir.delete());
}
八.Properties
1.Properties
本质是一个集合,但是这个集合和流有关,可以使用这个来存储一些键值对,然后通过字节或字符流写入硬盘或者从硬盘读入
2.Properties使用
这个集合主要的使用地方是在存储键值对,然后对其遍历在输出。以及保存为文件,并读取所保存的文件
代码示例public class Test2 {
public static void main(String[] args) throws IOException,NullPointerException {
//创建properties集合
Properties properties = new Properties();
//存入键值对
properties.setProperty("UserName","LiHua");
properties.setProperty("Age","12");
//=================================遍历==============================
//遍历方法一 keySet
Set<Object> objects = properties.keySet();
for (Object object : objects) {
System.out.println(object+properties.getProperty((String)object));
}
//遍历方法二 stringPropertyNames
Set<String> strings = properties.stringPropertyNames();
for (String string : strings) {
System.out.println(string+properties.getProperty(string));
}
//===============================存储================================
//创建字符输出流
PrintWriter printWriter = new PrintWriter("d:\\pro.properties");
//使用Properties方法存储键值对
properties.store(printWriter,"Test");
//关闭流
printWriter.close();
//===============================加载================================
//创建一个新的properties集合
Properties properties1 = new Properties();
//创建字节输入流,并读取之前存储的properties文件
FileInputStream fileInputStream = new FileInputStream("d:\\pro.properties");
properties1.load(fileInputStream);
//关闭流
fileInputStream.close();
//打印读取到的数据
System.out.println(properties1.toString());
}
}
总结
流的学习到这里就结束了,至少对我来说,把以前学习不牢固的地方又重新的学习了一遍,加深了印象,最后想着好的工作奋斗吧!