一、File类
1、是什么:
File是一个类,它在java.io包中,可以理解为File是在内存中的一个对象用来映射硬盘上的文件或文件夹;File与硬盘中的文件或文件夹是不同的,File用来抽象地表示文件或目录路径名。
2、File类中的常用的方法
String getPath() //返回此对象表示的文件的相对路径名
String getAbsolutePath() //返回此对象表示的文件的绝对路径名
String getName() //获取文件或目录的名称
String pname = getParent() //获取当前file的父亲file名字
File file = getParentFile() //获取当前file的父亲file对象
String[] names = list() //获取当前file的所有儿子名字
File[] files = listFiles() //获取当前file的所有儿子对象
boolean isFile() //判断是否是文件
boolean isDirectory() //判断是否是目录
boolean delete() //删除文件或空的文件夹,不能删除带元素的文件夹
boolean createNewFile() //创建新的文件,不创建文件夹
boolean mkdir() //创建新的文件夹,外层没有,不能创建
boolean mkdirs() //创建新的文件夹,外层没有,可以自动创建
long length() //返回由此抽象路径名表示的文件的长度
更多方法可参照Java8手册:https://www.matools.com/api/java8
3、递归思想学习
文件夹的遍历和删除都需要利用递归思想来实现,下面让我们来学习一下递归思想吧。
1)递归思想:可以理解为,你让我做事情,我先要求让别人把之前的事情做完,我再做,步骤:1.让别人先做事, 2.我自己做事。可能感觉有点绕,那看下面一串代码
public class Recursive {
public void testOne(){
this.testTwo();
System.out.println("testOne方法");
}
public void testTwo(){
this.testThree();
System.out.println("testTwo方法");
}
public void testThree(){
System.out.println("testThree方法");
}
public static void main(String[] args) {
Recursive re=new Recursive();
re.testOne();
}
}
输出结果为:
testThree方法
testTwo方法
testOne方法
这段代码的执行顺序可以让我我们理解递归的一个调用过程;首先应该清楚方法执行内存加载的顺序。在创建对象之前会在方法区中加载类,new一个对象在堆内存中开辟空间,然后re地址引用指向了堆内存中的空间,调用re执行方法会在栈内存中开辟临空间,执行结束由GC回收。
注意区分递归和循环:递归调用时,产生很多一样的临时执行空间,消耗内存,性能慢,但是递归能解决的问题循环做不了; 递归会有一个1)递归一定明确条件,否则会形成死循环并栈溢出。2)注意一下递归的次数,次数过多也会发生栈溢出。
循环在方法中是正常执行的只有一个方法执行空间。
2)递归练习:使用递归计算出1-100的累加求和
public class Recursive {
public static int sum(int n) {
if (n == 1) {
return 1;
} else {
return n + sum(n - 1);
}
}
public static void main(String[] args) {
System.out.println(sum(100));
}
}
输出结果:
3)递归思想在File中的使用
设计一个方法 用来展示(遍历)文件夹,利用递归
public class DemoFile {
public void showFile(File file){
//Demo文件夹里的所有文件
File [] files=file.listFiles();
//判断file是一个文件夹,文件夹内有元素,遍历
if (files!=null&&files.length!=0){
for (File f :files){
this.showFile(f);
}
}
System.out.println(file.getAbsolutePath());
}
public static void main(String[] args) {
DemoFile demo =new DemoFile();
File file =new File("C://Demo");
demo.showFile(file);
}
}
执行流程:
file对象不能操作文件中的内容,因此需要通过流I/O的方式来完成
二、IO流
1、分类
按流向来分:输入流(lnputStream和Reader作为基类);输出流output(outputStream和Writer作为基类)
按处理数据不同来分:字节流(输入InputStream,输出OutputStream)和字符流(输入Reade,输出Writer)
字节流:二进制,可以处理一切文本,包括:纯文本、doc、音频、视频等。
字符流:文本文件,只能处理纯文本,注意:字符流可以直接读取中文汉字,而字节流处理时会发生中文乱码。
2、字节流
1)字节型文件输入流FileInputStream;在Java.io包中,继承自InputStream类;
当编写io流的程序的时候一定要关闭流
步骤;
1、选择合适的io流对象
2、创建对象
3、传输数据
4、关闭流对象(占用系统资源)
常用方法:
int code = read(); 每次从流管道中读取一个字节,返回code码
int count = read(byte[] ) 每次从流管道中读取若干个字节 存入数组内 返回有效元素个数
int count = available(); 返回流管道中还有多少缓存的字节数
skip(long n) 跳过几个字节读取
close() 将流管道关闭(必做)
示例,读取文件内容,打印到控制台:
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("C://Demo//Test.txt");
int read = 0;
//添加缓冲区的方式进行读取,每次将数据添加到缓冲区中,当缓冲区满了之后,一次读取,而不是每一个字节进行读取
byte[] buffer = new byte[1024];
//循环输出所有
while ((read = fis.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, read));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
2)字节型文件输出流FileOutputStream,将数据写入文件中,在 java.io包中,继承OutputStream,所有字节型输出流的父类。
.常用方法
write(int code); 将给定code对应的字符写入文件
write(byte[]) 将数组中的全部字节写入文件 ,getByte()
flush(); 将管道内的字节推入(刷新)文件
close();关闭流
示例,将数据写入到demo.txt中:
public static void main(String[] args) {
FileOutputStream fos=null;
File file =new File("C://Demo//demo.txt");
try {
fos=new FileOutputStream(file);
//给定code对应的字符写入文件
fos.write(97);
fos.write("Hello,世界".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
3)缓冲流(BufferedInputStream/BufferedOutputStream;BufferedReader/BufferedWriter),在流管道内增加缓存的数据;让我们使用流读取的文字更加的流畅。
2、字符流
字符流,输入流Reader,输出流Writer,只能操作纯文本文件。
3、按类型分类
文件流
字节型 FileInputStream; FileOutputStream
字符型 FileReader ; FileWriter
缓冲
字节型 BufferedInputStream BufferedOutputStream
字符型 BufferedReader BufferedWriter
数组流
byte数组 ByteArrayInputStream ;ByteArrayOutputStream
char数组 CharArrayReader; CharArrayWriter
数据流 DataInputStream ; DataOutputStream
字符串流 StringReader; StringWriter
对象流 ObjectInputStream ObjectOutputStream
4、序列化和反序列化问题
对象的序列化:将一个完整的对象,拆分成字节碎片记录在文件中
对象的反序列化:将文件中记录的字节序列,组合成一个完整的对象
详细请参考:序列化学习