「Java」常用的文件操作 java的文件类型

createh55个月前 (12-26)技术教程59

目录

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 + "创建失败...");
            }
        }


    }

}

演示结果

相关文章

五年开发,你还记得Java基础代码中的文件I/O操作么?

文件I/O操作作为Java开发中的一个重要组成部分,主要负责Java代码中对于文件的读写操作,可能对于一些长时间不使用Java文件API的开发者来说早都忘记了相关的操作,下面我们就来详细介绍一下Jav...

吃透Java IO:字节流、字符流、缓冲流

前言有人曾问fastjson的作者(阿里技术专家高铁):“你开发fastjson,没得到什么好处,反而挨了骂背了锅,这种事情你为什么要做呢?”高铁答道:“因为热爱本身,就是奖励啊!”这个回答顿时触动了...

Linux删除系统自带的jdk linux卸载jdk1.7

Linux删除系统自带的jdk命令:rpm -qa|grep java // 这里删除两个文件 rpm -e --nodeps xxx.x86_64 rpm -e --nodepsxxx.x8...

万字详文:Java内存泄漏、性能优化、宕机死锁的N种姿势

导读本文介绍Java诸多优化实例:第一,排查堆上、堆外内存泄露;第二,使用arthas、jaeger、tcpdump、jstack做性能优化;第三,排查进程异常退出的原因,如被杀、System.exi...