Java自学笔记——文件

Java自学笔记——文件,第1张

Java自学笔记——文件 Java自学笔记——文件
  • 文件是保存数据的地方
创建
package com.hz.file;

import org.junit.Test;

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



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

    }

    @Test
    //方式一 new File(String pathname)
    public void creat01(){
        String filePath = "c:\D\news1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    //方式二 new File(File parent, String child) 父目录文件加子路径
    public void creat02(){
        File parentfile = new File("c:\D\");
        String fileName = "news2.txt";
        //只是在内存中创建一个File对象,没写入磁盘
        File file = new File(parentfile, fileName);
        try {
            //真正创建文件,写入磁盘
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    //方法三 new File(String parent, String child) 父目录加子路径
    public void creat03(){
        String parentPath = "c:\D\";
        String fileName = "news3.txt";
        File file = new File(parentPath, fileName);

        try {
            file.createNewFile();
            System.out.println("创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
获取相关信息
package com.hz.file;

import org.junit.Test;

import java.io.File;


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

    }

    @Test
    //获取信息
    public void info(){
        File file = new File("c:\D\news1.txt");
        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());
    }
}
目录 *** 作
package com.hz.file;

import org.junit.Test;

import java.io.File;


/


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

    }

    @Test
    //判断news1.txt是否存在,是就删除
    public void m1(){
        String filePath = "c:\D\news1.txt";
        File file = new File(filePath);
        if(file.exists()){
            if(file.delete()){
                System.out.println("删除成功");
            }
        }else {
            System.out.println("文件不存在");
        }
    }

    @Test
    //判断目录A是否存在,是就删除
    public void m2(){
        String filePath = "c:\D\A";
        File file = new File(filePath);
        if(file.exists()){
            if(file.delete()){
                System.out.println("删除成功");
            }
        }else {
            System.out.println("目录不存在");
        }
    }

    @Test
    //判断目录B是否存在,没有就创建
    public void m3(){
        String filePath = "c:\D\A";
        File file = new File(filePath);
        if(file.exists()){
            System.out.println("该目录存在");
        }else {
            //mkdir()创建一级目录
            //mkdirs()创建多级目录
            if(file.mkdirs()){
                System.out.println("创建成功");
            }
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5660079.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存