File - 文件类
File,是文件和目录路径名的抽象表示。
File只关注文件本身的信息,而不能操作文件里面的内容。
File类 – 表示文件或文件夹,不能对文件里的数据进行操作。
对文件里的数据进行操作的是:IO流。
需求1:通过程序,获取已知文件的以下信息:
public static void main(String[] args){File file = new File("C:\\Users\\何小宝\\Desktop\\hhy.txt");System.out.println("获取文件名:" + file.getName());System.out.println("获取路径:" + file.getAbsolutePath());System.out.println("获取是否可读:" + file.canRead());System.out.println("获取是否可写:" + file.canWrite());System.out.println("获取是否隐藏:" + file.isHidden());System.out.println("获取文件大小(字节):" + file.length());SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");String datetime = sdf.format(file.lastModified());System.out.println("获取文件最后修改时间:" + datetime);
}
相对路径 和 绝对路径
绝对路径:指定盘符的路径
相对路径:相对于项目下的路径
public static void main(String[] args) {File file = new File("hhy.txt");//hhy.txtSystem.out.println("获取相对路径:" + file.getPath());//D:\2402workspace\Day23\hhy.txtSystem.out.println("获取绝对路径:" + file.getAbsolutePath());
}
File类
需求2:通过程序,判断指定路径的文件是否存在,如果不存在,则创建该文件。
下面按各种情况来解决该问题
1)目录已存在的情况
public static void main(String[] args) throws IOException {File file = new File("file01//hhy.txt");if(!file.exists()){//判断文件是否存在//创建文件file.createNewFile();}
}
2)有一个层级的目录不存在的情况
public static void main(String[] args) throws IOException {File file = new File("file01//hhy.txt");//获取父路径的file对象 -- file01File parentFile = file.getParentFile();if(!parentFile.exists()){//判断目录路径是否存在//创建一层目录parentFile.mkdir();}if(!file.exists()){//判断文件是否存在//创建文件file.createNewFile();}
}
3)有多个层级的目录不存在的情况
public static void main(String[] args) throws IOException {File file = new File("file01//file02//file03//hhy.txt");//获取父路径的file对象 -- file01//file02//file03File parentFile = file.getParentFile();if(!parentFile.exists()){//判断目录路径是否存在//创建多层目录parentFile.mkdirs();}if(!file.exists()){//判断文件是否存在//创建文件file.createNewFile();}
}
需求3:输出指定目录下的所有文件信息(只考虑当前目录,不考虑子目录)
public static void main(String[] args) {File file = new File("D:\\2023");//获取当前路径下所有的文件名
// String[] list = file.list();
// for (String str : list) {
// System.out.println(str);
// }//获取当前路径下所有的file对象File[] listFiles = file.listFiles();for (File f : listFiles) {System.out.println(f.getName() + " -- " + f.canRead() + " -- " + f.canWrite());}
}
1)要求只输出文件后缀名为txt的文件
public static void main(String[] args) {File file = new File("D:\\2023");//获取当前路径下所有的文件名
// String[] list = file.list();
// for (String str : list) {
// if(str.endsWith(".txt")){
// System.out.println(str);
// }
// }//获取当前路径下所有的file对象File[] listFiles = file.listFiles();for (File f : listFiles) {String name = f.getName();if(name.endsWith(".txt") && f.isFile()){System.out.println(name);}}
}
2)根据API的过滤器来完成该功能
public static void main(String[] args) {File file = new File("D:\\2023");File[] listFiles = file.listFiles(new FilenameFilter() {//底层逻辑:遍历当前目录所有的file对象,返回true就添加到listFiles数组中,返回false就不管@Overridepublic boolean accept(File dir, String name) {//D:\\2023 + \\ + 文件名File f = new File(dir, name);String n = f.getName();if(n.endsWith(".txt") && f.isFile()){return true;}return false;}});for (File f : listFiles) {System.out.println(f.getName());}
}
3)需求继续跟进,列出当前目录及子目录中符合该条件的文件信息(递归)
public static void main(String[] args) {File file = new File("D:\\2023");method(file, ".txt");
}public static void method(File file,String suffix){File[] listFiles = file.listFiles();for (File f : listFiles) {if(f.isDirectory()){//文件夹method(f, suffix);}else if(f.isFile()){//文件String name = f.getName();if(name.endsWith(suffix)){System.out.println(name + " -- " + f.getAbsolutePath());}}}
}
利用 文件字节输出流 向文件写入数据
1)不处理异常的方式
2)文件存在的情况
public static void main(String[] args) throws IOException {//1.创建流对象FileOutputStream fos = new FileOutputStream("hhy.txt");//2.写入数据//fos.write(97);//写入Unicode码//fos.write("123abc".getBytes());//写入字节数组fos.write("123abc".getBytes(), 1, 3);//写入字节数组,偏移量,写入长度//3.关闭资源fos.close();
}
3)在末尾追加
经验:考虑基础流的构造方法
4)文件不存在的情况
经验:所有的输出流,当文件不存在时都会先创建该文件,再写入数据
5)处理异常的方式
public static void main(String[] args) {FileOutputStream fos = null;try {//1.创建流对象fos = new FileOutputStream("hhy.txt",true);//2.写入数据fos.write("123abc".getBytes());//写入字节数组} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//3.关闭资源if(fos != null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}
}
利用 文件字节输入流 读取文件里的数据
1)不处理异常的方式
2)文件存在的情况
public static void main(String[] args) throws IOException {//1.创建流对象FileInputStream fis = new FileInputStream("hhy.txt");//2.读取数据//read():读取一个字节,读取到文件末尾返回-1int read = fis.read();System.out.println((char)read);read = fis.read();System.out.println((char)read);read = fis.read();System.out.println((char)read);read = fis.read();System.out.println((char)read);read = fis.read();System.out.println((char)read);read = fis.read();System.out.println((char)read);read = fis.read();System.out.println(read);//3.关闭资源fis.close();
}
public static void main(String[] args) throws IOException {//1.创建流对象FileInputStream fis = new FileInputStream("hhy.txt");//2.读取数据//read():读取一个字节,读取到文件末尾返回-1int read;while((read = fis.read()) != -1){System.out.println((char)read);}//3.关闭资源fis.close();
}
public static void main(String[] args) throws IOException {//1.创建流对象FileInputStream fis = new FileInputStream("hhy.txt");//2.读取数据//read(bs):读取bs数组长度的数据并把数据存入数组中,返回读取到的有效字节数。如果读取到文件末尾,则返回-1byte[] bs = new byte[1024];//[0,0,0,0,0,0,....]int len;len = fis.read(bs);System.out.println(new String(bs, 0, len));//3.关闭资源fis.close();
}
3)文件不存在的情况
经验:
所有的输出流,当文件不存在时都会先创建该文件,再写入数据
所有的输入流,当文件不存在时都会报错 - FileNotFoundException
4)处理异常的方式
public static void main(String[] args) {FileInputStream fis = null;try {//1.创建流对象fis = new FileInputStream("hhy.txt");//2.读取数据//read(bs):读取bs数组长度的数据并把数据存入数组中,返回读取到的有效字节数。如果读取到文件末尾,则返回-1byte[] bs = new byte[1024];//[0,0,0,0,0,0,....]int len;while((len = fis.read(bs)) != -1){System.out.println(new String(bs, 0,len));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally{//3.关闭资源if(fis != null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}
}
拷贝文件
思路:读取源文件,写入目标文件
public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("源文件.mp4");FileOutputStream fos = new FileOutputStream("copy.mp4");int read;while((read = fis.read()) != -1){fos.write(read);}fis.close();fos.close();
}
public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("源文件.mp4");FileOutputStream fos = new FileOutputStream("copy.mp4");byte[] bs = new byte[1024];int len;while((len = fis.read(bs)) != -1){fos.write(bs, 0, len);}fis.close();fos.close();
}
public static void main(String[] args) {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("源文件111.mp4");fos = new FileOutputStream("copy.mp4");byte[] bs = new byte[1024];int len;while((len = fis.read(bs)) != -1){fos.write(bs, 0, len);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fis != null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}if(fos != null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}
}
public static void main(String[] args) {try(FileInputStream fis = new FileInputStream("源文件.mp4");FileOutputStream fos = new FileOutputStream("copy.mp4");) {byte[] bs = new byte[1024];int len;while((len = fis.read(bs)) != -1){fos.write(bs, 0, len);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
}
利用 带缓冲区的字节输出流 向文件写入数据
1)不处理异常的方式
2)文件存在的情况
3)在末尾追加
经验:考虑基础流的构造方法
4)文件不存在的情况
经验:所有的输出流,当文件不存在时都会先创建该文件,再写入数据
5)处理异常的方式
public static void main(String[] args) throws IOException{//1.创建流对象//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("hhy.txt"));//1.创建流对象 + 末尾追加//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("hhy.txt",true));//1.创建流对象 + 自定义长度BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("hhy.txt"),2048);//2.写入数据bos.write("123abc".getBytes());bos.write("123abc".getBytes());//3.关闭资源bos.close();
}
利用 带缓冲区的字节输入流 读取文件里的数据
1)不处理异常的方式
2)文件存在的情况
3)文件不存在的情况
经验:
所有的输出流,当文件不存在时都会先创建该文件,再写入数据
所有的输入流,当文件不存在时都会报错 - FileNotFoundException
4)处理异常的方式
public static void main(String[] args) throws IOException {//1.创建流对象//BufferedInputStream fis = new BufferedInputStream(new FileInputStream("hhy.txt"));//1.创建流对象 + 自定义缓冲区大小BufferedInputStream fis = new BufferedInputStream(new FileInputStream("hhy.txt"),2048);//2.读取数据byte[] bs = new byte[1024];int len;while((len = fis.read(bs)) != -1){System.out.println(new String(bs, 0, len));}//3.关闭资源fis.close();
}