• JAVA的File对象


    文件

    1.File对象

    java封装的一个操作文件及文件夹(目录)的对象。可以操作磁盘上的任何一个文件和文件夹。

    2.创建文件

    方式一:根据路径构建一个File对象new File(path)

    //方式一
     @Test public void create01(){ try {
                String path = URLDecoder.decode("D:\\博客园\\wjj1.txt","UTF-8");//解决中文乱码,转UTF-8
                File file = new File(path);
                file.createNewFile();
                System.out.println("创建成功01");
            } catch (UnsupportedEncodingException e) {//decode方法需要抛异常或捕获异常
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    方式二:根据父目录文件和子目录路径构建一个File对象new File(File,Spath)

    //方式二
        @Test public void create02(){
            String path = null; try {
                path = URLDecoder.decode("D:\\博客园","UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            File parentFile = new File(path);//父目录文件
            String fileName = "wjj2.txt";//子路径
            File file = new File(parentFile, fileName); try {
                file.createNewFile();
                System.out.println("创建成功02");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    方式三:根据父目录路径和子目录路径构建一个File对象new File(Fpath,Spath)

    
    
    //方式三
     @Test public void create03() throws Exception{//抛异常
            String path = URLDecoder.decode("D:\\博客园","UTF-8");
            String filePath = "wjj3.txt";
            File file = new File(path, filePath);
            file.createNewFile();
            System.out.println("创建成功03");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    运行结果:

    3.文件的相关操作

    文件的路径相关和判断功能的构造方法

     @Test public void info() throws Exception{ //创建文件对象
            String path = URLDecoder.decode("D:\\博客园\\wjj1.txt","UTF-8");
            File file = new File(path);
            System.out.println("文件名:"+file.getName());
            System.out.println("文件绝对路径:"+file.getAbsolutePath());
            System.out.println("文件父目录:"+file.getParent());
            System.out.println("文件大小(字节):"+file.length());
            System.out.println("文件是否存在:"+file.exists());
            System.out.println("是否是文件:"+file.isFile());
            System.out.println("是否是目录:"+file.isDirectory());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    UTF-8一个英文一个字节,一个汉字三个字节

    运行结果:

    文件删除操作的构造方法

     @Test public void fileDelete() throws Exception{
            String path = URLDecoder.decode("D:\\博客园\\wjj1.txt","UTF-8");
            File file = new File(path); if (file.exists()){ if (file.delete()){
                    System.out.println(path+"删除成功");
                }else {
                    System.out.println(path+"删除失败");
                }
            }else {
                System.out.println("文件不存在");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    文件创建目录操作的构造方法

    
    
     @Test public void isMkdir() throws Exception{
            String path = URLDecoder.decode("D:\\博客园\\wjj1","UTF-8");
            File file = new File(path); if (file.exists()){
                System.out.println(path+"该目录已存在");
            }else { if (file.mkdirs()){
                    System.out.println("创建成功");
                }else {
                    System.out.println("创建失败");
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    运行结果:

  • 相关阅读:
    C语言—统计从键盘输入的一行英文句子的字符个数
    C#通过MGet方法快速获取Redis数据库的记录
    async-validator 源码学习笔记(六):validate 方法
    26、Qt调用.py文件中的函数
    从0开始学人工智能测试节选:Spark -- 结构化数据领域中测试人员的万金油技术(四)
    Springboot门诊电子处方管理系统3kqta计算机毕业设计-课程设计-期末作业-毕设程序代做
    在IDEA中如何使用tomcat部署项目
    java计算机毕业设计支部党建工作源程序+mysql+系统+lw文档+远程调试
    IPv6进阶:IPv6 过渡技术之IPv6 over IPv4 手动隧道
    C++:什么时候使用move
  • 原文地址:https://blog.csdn.net/Huangjiazhen711/article/details/127728544