一、File类及其常用方法
File 类的使用:
- File类的一个对象,代表一个文件或一个文件目录
- File类声明在java.io包下
- File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、 文件大小等方法,并未涉及到写入或读取文件内容的操作。如果需要读取或写入文件内容,必须使用I0流来完成。
- 后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的”终点".
/*
1.如何创建File类的实例File(String filePath)
File(String filePath)
File(String parentPath,String childPath)
File(File ParentFile,String childPath)
2.相对路径:相较于某个路径下,指明的路径。
绝对路径:包含盘符在内的文件或文件目录的路径
3.路径分隔符
windows:\
unix:/
也可以使用File.separator
*/
@Test
public void test1(){
//构造器1
File file1 = new File( "hello. txt");//相对于当前module
File file2 = new File("D:\\Java\\hello.txt");
System. out . println(file1);
System. out. println(file2);
//构造器2:
File file3 = new File( "D:\\java","idea_worksapce");
System.out.println(file3);
//构造器3
File file4 = new File(file3,"hi.txt");
System.out.println(file4);
}
常用方法:
public String getAbsolutePath(): 获取绝对路径
public String getPath() :获取路径
public String getName() :获取名称
public String getParent(): 获取上层文件目录路径。若无,返回null
public Long Length() :获取文件长度(即:字节数)。不能获取目录的长度。
public Long LastModified() :获取最后-次的修改时间,毫秒值
如下两个方法适用于文件目录
public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
public File[] ListFiles() :获取指定目录下的所有文件或者文件目录的File数组
public boolean renameTo(File dest): 把文件重命名为指定的文件路径,功能相当于 移动+重命名
public boolean isDirectory(): 判断是否是文件目录
public boolean isFile() :判断是否是文件
public boolean exists() :判断是否存在
public boolean canRead() :判断是否可读
public boolean canWrite() :判断是否可写
public boolean isHidden() :判断是否隐藏
创建硬盘中的文件或文件目录
public boolean createNewFile(): 创建文件。若文件存在,则不创建,返回false
public boolean mkdir() :创建文件日录。如果此文件日录存在,就不创建了,如果此文件日录的上层日录不存在,也不创建。
public boolean mkdirs(): 如果父文件夹不存在并且最后一级子文件夹不存在,它就自动新建所有路经里写的文件夹;如果父文件夹存在,它就直接在已经存在的父文件夹下新建子文件夹
public boolean delete():删除文件或文件夹,直接删除,不走回收站
二、IO流体系
蓝色部分比较重要。
一、流的分类:
1.操作数据单位:字节流、字符流
2.数据的流向:输入流、输出流
3.流的角色:节点流、处理流
二、流的体系结构
抽象基类 节点流(或文件流) 缓冲流(处理流的一种)
InputStream FileInputStream (read(byte[] buffer)) BufferedInputStream (read(byte[] buffer))
OutputStream FileOutputStream (write(byte[] buffer,0,len)) BufferedOutputStream (write(byte[] buffer,0,len)) / flash()
Reader FileReader (read(char[] cbuf)) BufferedReader (read(char[] cbuf) / readLine())
Writer FileWriter (write(char[] cbuf,0,len)) BufferedWriter (write(char[] cbuf,0,len)) / flash()
两种重要的字符流
先说明一个问题,在idea中,图片等文件的路径在单元测试@Test中是相较于Module的;在main()方法中是相较于整个Project的。
FileReader: 将hello.txt文件内容读入程序中,并输出到控制台。
- read()方法的理解:返回读入的一个字符串。如果达到文件末尾,返回-1
- 异常处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理。
@Test
public void test1() {
FileReader fileReader = null;
try {
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt");//相较于当前Module
//2.提供具体的流
fileReader = new FileReader(file);
//3.数据流的读入
//read():返回读的一个字符,如果达到文件末尾,返回-1
//方式1
// int data = fileReader.read();
// while(data != -1){
// System.out.print((char)data);
// data = fileReader.read();
// }
//方式2
int data;
while ((data = fileReader.read()) != -1) {
System.out.println((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
try {
if (fileReader != null)
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//read()操作升级,使用read的重载方法
@Test
public void test2() {
FileReader fr = null;
try {
//1.
File file = new File("hello.txt");
//2.
fr = new FileReader(file);
//3.
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
//不要将循环条件写为cbuf.length,否则有可能输出出错(最后一次没装满)
// 因为读一次就依次覆盖数组对应位置,最后的位置没有覆盖,但也输出了
for (int i = 0; i < len; i++) {
System.out.print(cbuf[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter: 从内存中写出数据到硬盘的文件
- 输出操作,对应的File文件可以不存在。
- 如果不存在,在输出的过程中,会自动创建此文件。
如果存在:
1). 使用的流的构造器是:FileWriter(file,false) / FileWriter(file)是对原有文件的覆盖
2). 使用的流的构造器是:FileWriter(file,true),不会对原有文件覆盖,而是在原有的基础上追加内容。
@Test
public void test3() {
FileWriter fw = null;
try {
//1.提供File类的对象,指明写出到的文件
File file = new File("hello1.txt");
//2.提供FileWriter的对象,用于数据的写出
fw = new FileWriter(file);
//3.写出操作
fw.write("hah,da sha bi");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流资源的关闭
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
组合使用:
@Test
public void testReadWriter() {
FileReader fr = null;
FileWriter fw = null;
try {
//1.创建File类的对象,指明读入和写出的文件
File srcFile = new File("hello1.txt");
File destFile = new File("hello2.txt");
//2.创建输入流和输出流的对象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
//3.数据的读入和写出操作
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流资源
try {
if(fw!=null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fr!=null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结一句话就是:四步走套路