Java自学笔记——文件

Java自学笔记——文件

  • 文件是保存数据的地方

创建

package com.hz.file;

import org.junit.Test;

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

/**
 * @author Lspero
 * @version 1.0
 * 创建文件
 */
/*
文件是保存数据的地方
文件流:
    流:数据在数据源(文件)和程序(内存)之间经历的路径
    输入输出流(以程序为主体)
*/
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;

/**
 * @author Lspero
 * @version 1.0
 */
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;

/**
 * @author Lspero
 * @version 1.0
 */
/


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("创建成功");
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值