IO流——文件

本次博客带领大家了解IO流中文件的用法,了解文件的定义,文件的创建方式等等。

一、文件

  • 文件,对我们并不陌生,文件是保存数据的地方,比如大家经常使用的word文档,txt文档,excel文件等等都是文件。它既可以保存一张图片,也可以保存视频,声音等等。

二、文件流

  • 文件在程序中是以流的形式来操作的。
    请添加图片描述
  • 流:数据在数据源(文件)和程序(内存)之间经历的路径。
  • 输入流:数据从数据源(文件)到程序(内存)的路径。
  • 输出流:数据从程序(内存)到数据源(文件)的路径。

三、常用的文件操作

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

    • 相关方法

    • new File(String pathname)//根据路径构建一个File对象
      
    • new File(File parent,String child)//根据父目录文件+子路径构建
      
    • new File(String parent,String child)//根据父目录+子路径构建
      
    • createNewFile 创建新文件

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

    //方式2 new File(File parent,String child)//根据父目录文件+子路径构建
    @Test
    public void create02(){
        File parentfile = new File("d:\\111\\");
        String fileName = "news2.txt";
        //这里的file对象,在java程序中,只是一个对象。
        //只有执行了createNewFile 方法,才会真正的,在磁盘创建文件。
        File file = new File(parentfile,fileName);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式3 new File(String parent,String child)//根据父目录+子路径构建
    @Test
    public  void create03(){
        String parentPath = "d:\\111\\";
        String fileName = "news3.ext";
        File file = new File(parentPath,fileName);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 获取文件的相关信息
    public void info(){
        //先创建文件对象
        File file = new File("d:\\111\\new1.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());
    }
  • 目录的操作和文件删除
    • mkdir创建一级目录、mkdirs创建多级目录、delete删除空目录或文件。
    //判断 d:\\111\\new1.txt 是否存在,若果存在就删除
    @Test
    public void m1(){
        String filePath = "d:\\111\\new1.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:\\111\\demo02 是否存在,若果存在就删除,否则提示不存在
    //这里我们需要体会到,在java编程中,目录也被当做文件。
    @Test
    public void m2(){
        String filePath = "d:\\111\\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:\\111\\a\\b\\c 目录是否存在,如果存在就提示已经存在,否则就创建
    @Test
    public void m3(){
        String directoryPath = "d:\\111\\demo\\a\\b\\c";
        File file = new File(directoryPath);
        if(file.exists()){
            System.out.println(directoryPath + "存在...");
        } else {
            if(file.mkdirs()){
                System.out.println(directoryPath + "创建成功..");
            } else {
                System.out.println(directoryPath + "创建失败...");
            }
        }
    }
好的,我可以为你介绍一下如何使用 Java 编写程序实现文件拷贝。 Java 提供了非常方便的 I/O 类来处理文件读写操作。文件拷贝的基本思路是读取源文件中的数据,然后将数据写入到目标文件中。下面是一个简单的文件拷贝程序示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) throws IOException { // 源文件路径 String srcFilePath = "C:/temp/source.txt"; // 目标文件路径 String destFilePath = "C:/temp/dest.txt"; // 创建输入流和输出流 FileInputStream fis = new FileInputStream(srcFilePath); FileOutputStream fos = new FileOutputStream(destFilePath); // 定义缓冲区,每次读取 1024 个字节 byte[] buffer = new byte[1024]; int len; // 循环读取源文件中的数据,并将数据写入到目标文件中 while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } // 关闭输入流和输出流 fis.close(); fos.close(); System.out.println("文件拷贝完成!"); } } ``` 在上面的示例中,我们首先定义了源文件路径和目标文件路径。然后创建了输入流和输出流,用于读取源文件和向目标文件写入数据。我们定义了一个缓冲区,每次从输入流中读取 1024 个字节,并将这些字节写入到输出流中。最后,我们关闭输入流和输出流,并输出一条完成信息。 需要注意的是,上面的代码中使用了 try-catch 语句来捕获可能出现的 IOException 异常。这是因为在文件读写过程中可能会出现异常,比如文件不存在、文件无法读取等等。为了保证程序的健壮性,我们需要使用 try-catch 语句来处理这些异常。 希望这个简单的示例可以帮助你了解如何使用 Java 编写文件拷贝程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值