首页 > 编程语言 >java 常用的文件操作

java 常用的文件操作

时间:2022-11-07 11:34:21浏览次数:35  
标签:文件 常用 java File filePath System file println out

三种文件创建方操作:

import org.testng.annotations.Test;

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


public class FileTest {
    public static void main(){

    }
    //方式1
    @Test
    public void create1() throws IOException {
        String filePath = "d:\\file1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("创建文件1成功");
        }catch (IOException e){
            e.printStackTrace();
        }

    }
    //方式2
    @Test
    public void create2(){
        File parentFile = new File("D:\\");
        String fileName = "file2.txt";
        File file = new File(parentFile,fileName);

        try {
            file.createNewFile();
            System.out.println("文件2 创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //方式3
    @Test
    public void create3(){
        String parentPath = "d:\\";
        String filePath = "file3.txt";


        File file = new File(parentPath,filePath);

        try {
            file.createNewFile();
            System.out.println("文件3创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

获取文件的相关信息:

 

 常见方法:

import org.testng.annotations.Test;

import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import java.io.File;
import java.sql.SQLOutput;

public class FileFormation {
    public static void main(String[] args){

    }
    // 获取文件信息
    @Test
    public void Info(){
        //先创建文件对象
        File file = new File("D:\\file1.txt");


        //调用相应方法,得到相应信息
        System.out.println("文件名称:"+file.getName());
        System.out.println("文件绝对路径:"+file.getAbsoluteFile());
        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());
    }
}

 

目录的操作:

import org.testng.annotations.Test;

import java.io.File;

public class FileDirectory {
    public static void main(String[] args){

    }
    @Test
    //删除文件
    public void FileDelete(){
        String filePath = "D:\\file1.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(filePath+ " 文件不存在");
        }
    }

    //删除目录
    @Test
    public void FileDeleteD(){
        String filePath = "D:\\file1.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(filePath+ "目录不存在");
        }
    }
    //判断目录是否存在,不存在就创建
    @Test
    public void fileDeleteD1(){
        String dirPath = "D:\\test\\dir1.txt";
        File file = new File(dirPath);

        if(file.exists()){
            System.out.println(dirPath + "该目录已经存在");
        }else{
            if(file.mkdir()){
                System.out.println("创建成功");
            }else{
                System.out.println("创建失败");
            };
        }
    }
}

 

标签:文件,常用,java,File,filePath,System,file,println,out
From: https://www.cnblogs.com/zhangsanlisi411/p/16865396.html

相关文章

  • java问题处理---若依框架下对某个url权限放开,登陆后访问403 Forbidden
    问题情景:  在若依框架下搭建了一套前后端代码,对某个url放开权限,后端SecurityConfig进行如下配置但当登陆系统后,请求放开权限的url,403报错。解决方案:参考:https://gi......
  • /usr/bin/env: “php\r”: 没有那个文件或目录
    用./运行php脚本文件出现报错信息/usr/bin/env"php\r"没有那个文件或目录错误原因:这主要是因为\r这个字符的缘故。在linux终端下,输出\r会什么都不显示,只是把光标......
  • Ant Design Vue自定义文件上传设置progress进度条
    前言在使用AntDesignVue的upload文件上传功能的时候,我们为了规范前端代码,所以会封装接口请求到对应的ts或js文件并引用,这个时候我们会更多的使用自定义文件上传的功......
  • 死磕Java面试系列:深拷贝与浅拷贝的实现原理
    深拷贝与浅拷贝的问题,也是面试中的常客。虽然大家都知道两者表现形式不同点在哪里,但是很少去深究其底层原理,也不知道怎么才能优雅的实现一个深拷贝。其实工作中也常常需要......
  • 使用VSCode,学习JAVA Hello World
    使用VSCode,学习JAVA安装插件:ExtensionPackforJava安装好后ctrl+shift+p,输入Java,选择“创建java项目”,选择项目类型:Nobuildtools,创建一个HelloWorld项目 ......
  • python项目 打包成 exe 可执行文件
    pipinstallpyinstaller或者condainstallpyinstaller pyinstaller安装完成,显示界面如下:  运行到项目的主文件目录,执行:(-iapple.ico是使用apple图片作为exe......
  • 操作系统速成——4.文件系统
    四.文件系统 4.1文件、文件系统文件是以计算机硬盘为载体的存储在计算机上的信息集合文件系统就是操作系统中负责操作和管理文件的一整套设施,它实现文件的共享和保护,......
  • Nginx操作:显示代理IP与配置多个静态文件
    今天使用Nginx,排查一个问题,负载过程,登陆应用提示“暂无权限”!需要查看负载真实IP信息就有了这篇记录。先上脚本:#add_headerbackendIP$upstream_add......
  • IDEA的properties文件中文乱码
    1.IDEA设置:文件编码一般都修改为utf-8(setting-->fileencodings--->GlobalEncoding和ProjectEncoding都设置为UTF-8)2.文件编码设置界面中:重要属性 Transp......
  • Java中Number下各数据类型的范围说明
    转自:http://www.java265.com/JavaCourse/202111/1792.html下文笔者将着重讲述java基础知识,Number类型包含的类型简介说明,如下所示:Number类型有以下6种类型Byte类型......