当前位置:首页 > 技术文章 > 正文内容

「Java」常用的文件操作

arlanguage1个月前 (03-27)技术文章24

目录

1.创建文件对象相关构造器和方法

1.相关方法

1.new File(String pathname)//根据文件路径构建一个File对象

2.new File(File parent,String child)//根据父目录文件+子路径构建一个File对象

3.new File(String parent,String child)//根据父目录+子路径构建一个File对象

package com;

import java.io.File;
import java.io.IOException;

/**
 * @version 1.0
 * @auther Demo龙
 * 演示创建文件
 */
public class FileCreat {
 
    public static void main(String[] args) {
 
        Test test = new Test();
        test.creat01();
        test.creat02();
        test.creat03();
    }
}
class Test {
 
    //方式1:> 1.new File(String pathname)//根据文件路径构建一个File对象
        public void creat01() {
 
            String filePath = "e:\\news1.txt";
            File file = new File(filePath);
            try {
 
                file.createNewFile();
                System.out.println("file文件创建成功");
            } catch (IOException e) {
 
                throw new RuntimeException(e);
            }
        }
        //> 2.new File(File parent,String child)//根据父目录文件+子路径构建一个File对象
        public void creat02() {
 
            File parentfile=new File("e:\\");
            String fileName = "news2.txt";
            File file = new File(parentfile,fileName);
            try {
 
                file.createNewFile();
                System.out.println("file文件创建成功");
            } catch (IOException e) {
 
                throw new RuntimeException(e);
            }
        }
    //> 3.new File(String parent,String child)//根据父目录+子路径构建一个File对象
    public void creat03() {
 
        String parentfile="e:\\";
        String fileName = "news3.txt";
        File file = new File(parentfile,fileName);
        try {
 
            file.createNewFile();
            System.out.println("file文件创建成功");
        } catch (IOException e) {
 
            throw new RuntimeException(e);
        }
    }
}

演示结果

2.获取文件信息

//1.getName()获取文件名

System.out.println(“文件名=”+file.getName());

//2.文件绝对路径file.getAbsolutePath()

System.out.println(“文件绝对路径=”+file.getAbsolutePath());

//3.文件父级目录file.getParent()
    System.out.println("文件父级目录=" + file.getParent());3.
//4.文件大小(字节)file.length()
    System.out.println("文件大小(字节)=" + file.length());
//5.文件是否存在file.exists()
    System.out.println("文件是否存在=" + file.exists());//T
//6.是不是一个文件file.isFile()
    System.out.println("是不是一个文件=" + file.isFile());//T
//7.是不是一个目录file.isDirectory()
    System.out.println("是不是一个目录=" + file.isDirectory());//F
import java.io.File;

/**
 * @version 1.0
 * @auther Demo龙
 * 获取文件信息
 */
public class fileInformation {
 
    public static void main(String[] args) {
 
        Test02 test02 = new Test02();
        test02.info();
    }
}
class Test02{
 
    public void info() {
 
        //获取文件信息
        File file = new File("e:\\news1.txt");
        //调用相应方法,得到对应信息
        //1.getName()获取文件名
        System.out.println("文件名="+file.getName());
        //2.文件绝对路径file.getAbsolutePath()
        System.out.println("文件绝对路径="+file.getAbsolutePath());
        //3.文件父级目录file.getParent()
        System.out.println("文件父级目录=" + file.getParent());
        //4.文件大小(字节)file.length()
        System.out.println("文件大小(字节)=" + file.length());
        //5.文件是否存在file.exists()
        System.out.println("文件是否存在=" + file.exists());//T
        //6.是不是一个文件file.isFile()
        System.out.println("是不是一个文件=" + file.isFile());//T
        //7.是不是一个目录file.isDirectory()
        System.out.println("是不是一个目录=" + file.isDirectory());//F
    }
}

演示结果

3.目录操作和文件删除

mkdir创建一级目录,mkdirs创建多级目录,delete删除空目录或文件

1.//判断 d:\news1.txt 是否存在,如果存在就删除

2. //判断 D:\demo02 是否存在,存在就删除,否则提示不存在

3. //判断 D:\demo\a\b\c 目录是否存在,如果存在就提示已经存在,否则就创建

package com;

import java.io.File;

/**
 * @version 1.0
 * @auther Demo龙
 */
public class directory {
 
    public static void main(String[] args) {
 
        Test03 test03 = new Test03();
        test03.func01();
        test03.func02();
        test03.func03();
    }
}
class Test03{
 
    //判断 d:\\news1.txt 是否存在,如果存在就删除
    public void func01(){
 
        String filePath = "e:\\news1.txt";
        File file = new File(filePath);
        if (file.exists()) {
 
            if (file.delete()) {
 
                System.out.println(filePath + "删除成功");
            } else {
 
                System.out.println(filePath + "删除失败");
            }
        } else {
 
            System.out.println("该文件不存在...");
        }
    }
    //判断 D:\\demo02 是否存在,存在就删除,否则提示不存在
    //这里我们需要体会到,在java编程中,目录也被当做文件
    public void func02(){
 
        String filePath = "D:\\demo02";
        File file = new File(filePath);
        if (file.exists()) {
 
            if (file.delete()) {
 
                System.out.println(filePath + "删除成功");
            } else {
 
                System.out.println(filePath + "删除失败");
            }
        } else {
 
            System.out.println("该目录不存在...");
        }
    }
    //判断 D:\\demo\\a\\b\\c 目录是否存在,如果存在就提示已经存在,否则就创建
    public void func03(){
 
        String directoryPath = "D:\\demo\\a\\b\\c";
        File file = new File(directoryPath);
        if (file.exists()) {
 
            System.out.println(directoryPath + "存在..");
        } else {
 
            if (file.mkdirs()) {
  //创建一级目录使用mkdir() ,创建多级目录使用mkdirs()
                System.out.println(directoryPath + "创建成功..");
            } else {
 
                System.out.println(directoryPath + "创建失败...");
            }
        }


    }

}

演示结果

扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/3625.html

标签: java文档
分享给朋友:

“「Java」常用的文件操作” 的相关文章

Nginx详细介绍

本期我们来说一说nginxNginx介绍nginx是俄罗斯人lgor Sysoev(伊戈尔.塞索耶夫)开发的一款高性能HTTP和反向代理服务器。Nginx以高效的epoll、kqueue、eventport作为网络IO模型,在高并发场景下、Nginx能够轻松支持5W并发连接数的响应,并且消耗的服务器...

NGINX配置跨域CORS支持 nginx配置跨域请求

NGINX配置跨域CORS支持这两天在搞酷瓜云网课的 app,采用 uni-app 做全端支持,现学现卖,目前算是入门了。在做 H5 的时候难免会跨域请求后端 API,虽然用 HBuilder 内置的浏览器不会有跨域问题(这个应该是做了内部处理),但是那个内置浏览器真尼妈坑爹,过一会就会卡死,导致...

nginx配置集群 -websocket

nginx配置集群 -websocket前几天做一个nginx的反向代理,来代理websocket。因为上线时间的问题,所以是单节点运行。现在准备做集群优化,然后上容器环境。这样就需要配置nginx的负载均衡。不废话了,下面是配置文件。当然配置后需要验证,验证的时候会出现很多奇怪的问题。这里就不进行...

nginx支持跨域的方法 nginx配置支持跨域

在nginx的配置文件中添加允许跨域的响应头。参考nginx官方文档,添加响应头的方法如下:在conf文件的server作用域中添加如下响应头:server { listen 80; # 其他配置... # 允许跨域 add_header Access-Control-Allow-...

C# 实现高并发 Web 应用的性能优化秘籍

在现代的互联网应用中,尤其是大型 Web 应用,性能和可扩展性成为了核心竞争力。随着用户访问量和数据量的增大,高并发处理成为了系统稳定性和响应速度的关键因素。无论是电商平台、社交网站还是 SaaS 应用,如何应对海量用户的同时访问,确保系统高效运转,已经成为了技术人员面临的重要挑战。C# 和 ASP...

宝塔面板——永久免费的服务器运维面板

宝塔面板是什么?宝塔面板,简单好用的服务器运维面板。官称永久免费。Linux版请确保纯净系统安装(支持CentOS、Ubuntu、Debian、Fedora、deepin),Web端管理,一键创建网站、FTP、数据库、SSL;安全管理,计划任务,文件管理,PHP多版本共存及切换;自带LNMP与LAM...