栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

File学习,敲完代码基本就会

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

File学习,敲完代码基本就会

File

 import java.io.File;
 ​
 
 public class Demo01File {
     public static void main(String[] args) {
         String pathSeparator = File.pathSeparator;
         System.out.println(pathSeparator);//路径分隔符
 ​
         String separator = File.separator;
         System.out.println(separator);//文件名称分隔符
     }
 }
 
 public class Demo02File {
     public static void main(String[] args) {
         
         show01();
         show02("D:","学习");
         show03();
     }
 ​
     private static void show03() {
         File parent =new File("D:\");
         File file =new File(parent,"学习");
         System.out.println(file);
     }
 ​
     private static void show02(String parent,String child) {
         File file =new File(parent,child);
         System.out.println(file);
         //
 ​
     }
 ​
     private static void show01() {
         File f1 =new File("D:\学习");
         System.out.println(f1);//重写了Object类中的toString方法
 ​
     }
 }
 
import java.io.File;
 public class Demo03File {
     public static void main(String[] args) {
         show01();
         show02();
         show03();
         show04();
     }
 ​
     private static void show04() {
         File f2 =new File("D:\学习");
         //返回由此File表示的文件的长度
         //获取的是构造方法指定的文件的大小,以字节为单位的,文件夹是没有大小概率的,路径不存在返回0
         long length = f2.length();
         System.out.println(length);
     }
 ​
     private static void show03() {
         File f3 =new File("D:\学习");
         String name = f3.getName();
         System.out.println(name);//返回由此File表示的文件或目录名称
 ​
 ​
     }
 ​
     private static void show02() {
         File f2 =new File("D:\学习");
         String path =f2.getPath();
         System.out.println(path);//将File转换为路径名字符串
         //toString方法就是调用getPath方法
     }
 ​
     private static void show01() {
         File f1 =new File("D:\学习");
         String absolutePath1 =f1.getAbsolutePath();
         System.out.println(absolutePath1);//返回File的绝对路劲字符串
 ​
     }
 }

 
import java.io.File;
 ​
 public class Demo04File {
     public static void main(String[] args) {
         show01();
         show02();
         show03();
     }
 ​
     private static void show03() {
         File f3 =new File("D:\学习");
         System.out.println(f3.isFile());//此File表示的是是否为文件
 ​
     }
 ​
     private static void show02() {
         File f2 =new File("D:\学习");
         System.out.println(f2.isDirectory());//此File表示的是否为目录
     }
 ​
     private static void show01() {
         File f1 =new File("D:\学习");
         System.out.println(f1.exists());//此File表示的文件或者目录是否实际存在
     }
 }
 import java.io.File;
 import java.io.IOException;
 ​
 public class Demo05File {
     public static void main(String[] args) throws IOException {
         show01();
         show02();
         show03();
         show04();
     }
 ​
     private static void show04() {
         File f4 =new File("D:\学习\b1");
         boolean delete = f4.delete();
         System.out.println("删除"+delete);//删除File表示的文件和目录,文件和文件夹都可以删除
     }
 ​
     private static void show03() {
         File f3 =new File("D:\学习\a2");
         boolean mkdirs = f3.mkdirs();
         System.out.println(mkdirs);//既可以创建单级文件夹,也可以创建多级文件夹
     }
 ​
     private static void show02() {
         File f2 =new File("D:\学习\a1");
         boolean mkdir = f2.mkdir();
         System.out.println(mkdir);//创建单级空文件夹
     }
 ​
     private static void show01() throws IOException {
         
         File f1 =new File("D:\学习\b1");
         boolean newFile = f1.createNewFile();
         System.out.println("b1:"+newFile);
         System.out.println("a1:"+newFile);
     }
 }
   
import java.io.File;
 ​
   public class Demo06File {
       public static void main(String[] args) {
           show01();
           show02();
       }
   //返回一个String数组,表示该File目录中所有子目录或者文件
       private static void show02() {
           File file =new File("D:\学习");
           String[] list = file.list();
           for (String fileName :list){
               System.out.println(fileName);//打印名字
           }
       }
   //返回一个File数组,表示该File目录中的所有子文件或者目录
       private static void show01() {
           File file =new File("D:\学习");
           File[] files = file.listFiles();
           for (File file1 : files) {
               System.out.println(file1);//连着路径一起打印
           }
       }
   }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756073.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号