知识点
File类
java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。
构造方法:
public File(String pathname) :通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
public File(String parent, String child) :从父路径名字符串和子路径名字符串创建新的 File实例。
public File(File parent, String child) :从父抽象路径名和子路径名字符串创建新的 File实例。
构造举例,代码如下:
小贴士:
- 一个File对象代表硬盘中实际存在的一个文件或者目录。
- 无论该路径下是否存在文件或者目录,都不影响File对象的创建。
获取功能的方法
public String getAbsolutePath() :返回此File的绝对路径名字符串。
public String getPath() :将此File转换为路径名字符串。
public String getName() :返回由此File表示的文件或目录的名称。
public long length() :返回由此File表示的文件的长度。
方法演示,代码如下:
API中说明:length(),表示文件的长度。但是File对象表示目录,则返回值未指定。
绝对路径和相对路径
绝对路径:从盘符开始的路径,这是一个完整的路径。
相对路径:相对于项目目录的路径,这是一个便捷的路径,开发中经常使用。
FileOutputStream类
OutputStream 有很多子类,我们从最简单的一个子类开始。
java.io.FileOutputStream 类是文件输出流,用于将数据写出到文件。
构造方法
public FileOutputStream(File file) :创建文件输出流以写入由指定的 File对象表示的文件。
public FileOutputStream(String name) : 创建文件输出流以指定的名称写入文件。
当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,会创建该文件。如果有这个文 件,会清空这个文件的数据。
构造举例,代码如下:
写出字节数据
- 写出字节: write(int b) 方法,每次可以写出一个字节数据
## 课堂实践
package demo01;
import java.io.File;
import java.io.IOException;
/*
*
* file 类
*
* 创建一个文件/文件夹
* 删除一个文件、文件夹
* 获取文件、文件夹
* 判断文件或文件夹
* 多文件进行遍历
* 获取文件大小
*
*
* File 是一个与操作系统无关的类
*
* 记住三个单词:
*
* file: 文件
* directory: 目录、文件夹
* path: 路径
*
*
* 绝对路径和相对路径
*
*
*
*/
public class Demo01File {
public static void main(String[] args) throws IOException {
// 常用分割符号
// 路径分割
String pathSeparator = File.pathSeparator;
System.out.println(pathSeparator);// win 是分号 ; Linux : 冒号
String separator = File.separator;
System.out.println(separator);// 文件名称分割符 \ Linux /root/home/
// 构造方法方法
//show01();
//show02();
//show03();
// 常用方法演示
//show04();
//show05();
//show06();
show07();
show08();
show09();
//show10();
//show11();
show12();
}
private static void show03() {
File parent = new File("D:\\lx\\java\\test");
File f1 = new File("hello.java");
System.out.println(f1);
}
private static void show02(String parent,String child) {
File f1 = new File(parent,child);
System.out.println(f1);
}
private static void show01() {
File f1 = new File("D:\\lx\\java\\test");
System.out.println(f1);
// 仅仅是创建了File这样的
File f2 = new File("D:\\lx\\java\\test\\a.txt");
System.out.println(f2);
File f3 = new File("b.txt");
System.out.println(f3);
}
private static void show12() {
}
private static void show11() {
File f1 = new File("D:\\java\\318\\hello.java");
boolean b1 = f1.mkdir();
System.out.println(b1);
}
private static void show10() throws IOException {
File f1 = new File("D:\\java\\318\\hello.java");
boolean b1 = f1.createNewFile();
System.out.println(b1);
// 第二次执行的时候返回 false 因为文件 已经存在
File f2 = new File("D:\\java\\318\\hello.java");
boolean b2 = f2.createNewFile();
System.out.println(b2);
}
private static void show09() {
File f1 = new File("D:\\java\\318");
if(f1.exists()) {
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
}
//不存在的
System.out.println("====================");
File f2 = new File("D:\\java\\318");
if(f1.exists()) {// 不存在返回false 所以不会进入if语句
System.out.println(f2.isDirectory()); //f
System.out.println(f2.isFile()); //f
}
System.out.println("====================");
File f3 = new File("D:\\java\\318");
if(f3.exists()) {// 不存在返回false 所以不会进入if语句
System.out.println(f2.isDirectory()); //f
System.out.println(f2.isFile()); //
}
}
private static void show08() {
File f1 = new File("D:\\lx\\java\\20\\day20_code");
System.out.println(f1.exists());// true
File f2 = new File("D:\\lx\\java\\20\\day20_code\\.classfive");
System.out.println(f2.exists());// false
File f3 = new File("D:\\lx\\java\\20\\day20_code\\.classpath");
System.out.println(f3.exists());//
}
//注意 文件夹没有大小的概念,不能获取文件夹的大小
private static void show07() {
File f1 = new File("D:\\lx\\java\\20\\day20_code");
System.out.println(f1.length()); // 文件夹的大小是0
File f2 = new File("D:\\lx\\java\\20\\day20_code\\.classfive");
System.out.println(f2.length());// 不存在的文件夹,打印的大小为0
File f3 = new File("D:\\lx\\java\\20\\day20_code\\.classpath");
System.out.println(f3.length());//最后为文件的情况,且文件存在,打印文件的大小为
File f4 = new File("D:\\lx\\java\\20\\day20_code\\hello.java");
System.out.println(f4.length());
}
private static void show06() {
File f1 = new File("D:\\lx\\java\\20\\day20_code\\hello.java");
File f2 = new File("D:\\lx\\java\\20\\day20_code");
// 获取的是构造方法传递路径结尾部分
System.out.println(f1.getName());
System.out.println(f2.getName());
}
private static void show05() {
File f1 = new File("D:\\lx\\java\\test\\hello.java");
File f2 = new File("a.txt");
String path1 = f1.getParent();
System.out.println(path1);
System.out.println(f2.getParent());
System.out.println(f1);
System.out.println(f1.toString());
}
private static void show04() {
File f1 = new File("D:\\lx\\java\\test\\hello.java");
String absolutepath1 = f1.getAbsolutePath();
System.out.println(absolutepath1);
File f2 = new File("hello.java"); // 相对路径创建
// 创建的时候,是放在项目路径下面
String absolutepath2 = f2.getAbsolutePath();
System.out.println(absolutepath2);// 获取绝对路径
}
}
package demo02;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
*
* 字节流
*
* 构造方法的作用
*
* 1.创建了一个FileOutputStream对象
* 2.根据构造方法传递的文件/文件路径,创建一个空的文件
* 3.会把FileOutputStream对象指向创建的文件
*
*
* 写入过程:
* java --> jvm(java 虚拟机) -->os(操作系统)-->会调用对应的驱动-->把数据写入文件
*
*
*
*/
public class Demo02OutputStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("a.txt");
// 调用write 方法,将数据写入到文件中
//fos.write(97);
//fos.write(98);
//fos.write(98);
//释放资源
//fos.write(49);
//fos.write(48);
//fos.write(48);
//byte[] bytes = {65,66,67,68};
//byte[] bytes = {-65,-66,-67,-68};// 会被当成中文来解释
byte[] bytes = {65,66,67,68,69,70};
fos.write(bytes,2,3);
fos.close();
}
}
2020080605036