2021-05-20 文件的操作

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

public class domo01 {
    public static void main(String[] args) throws IOException {
      File file1 = new File("D:\\暴风激活15.0.exe");
      File file2 = new File("d:\\大电影");
      File file3 = new File("d:/domo01/domo01.1");
      File file4 = new File("d:/domo01/domo01.1");
      File file5 = new File("D:\\test.txt");
      File file6 = new File("D ://gesf");
      //  1 创建一个文件
        file2.createNewFile();
        // 2 创建一个文件夹 mkdir 一个只能创建一个文件夹,如果父路径没有,则创建失败
        boolean res = file3.mkdir();
        System.out.println(res);
        // 3 mkdirs 创建多个文件夹,如果父路径不存在,则一次性创建全部路径
        file4.mkdirs();
        //  4 删除文件
        file2.delete();
        // 5 renameTo 移动文件
        file1.renameTo(new File(""));
         // 6 exists
        System.out.println(file2.exists());
        // 7 isFile  是否是文件
        System.out.println(file4.isFile());
        // 8 isDirectory  是否是目录
        System.out.println(file4.isDirectory());
        // 9 getPath() 获取路径
        System.out.println(file6.getPath());
    }
}
package com.company;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class domo02 {

public static void main(String[] args) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try { //1 准备输入输出流
        fis = new FileInputStream(new File("c:/jdk8.0.2_windows-x64_bin.exe"));
        fos = new FileOutputStream(new File("c:/jik8.exe"));
        //2 循环读取操作
        int len = 0;
        long start = System.currentTimeMillis();
        byte[] buff = new byte[1024];//开辟缓冲区
        while ((len = fis.read(buff)) != -1) {
            fos.write(buff, 0, len);
        }
        long end = System.currentTimeMillis();
        System.out.println("时间" + (end - start));//1265
        //3 关闭输入输出资源
        fis.close();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}}
package com.company;

import java.io.*;

public class domo03{
        public static void main(String[] args)throws Exception {
                BufferedInputStream bis = null ;
                BufferedOutputStream bos = null ;
                try {
            //1 准备输入输出流
             bis=new BufferedInputStream( new FileInputStream(new File("c:/jdk8.0.2_windows-x64_bin.exe")));
             bos=new BufferedOutputStream(new FileOutputStream(new File("c:/jik8.exe")));
            //2 循环读取操作
            int len=0;
            byte[] buff=new byte[1024];//开辟缓冲区
            long start = System.currentTimeMillis(); // 开始计时
            while ((len=bis.read(buff))!=-1){
                bos.write(buff, 0,len);
            }
            long end = System.currentTimeMillis(); // 结束计时
                    System.out.println("时间"+ ( end - start)); // 453ms
            //3 关闭输入输出资源
            bis.close();
            bos.close();
        }catch (Exception e) {
                    e.printStackTrace();
                }
    }
}
jpackage com.company;

import java.io.*;

public class domo04 {
    public static void main(String[] args) throws IOException {
        // reader 和 writer 只能操作文本内容
        Reader reader = new FileReader("d:/1.txt");
        FileWriter fileWriter = new FileWriter("d:/111.txt");
        char [] buff = new char[1024];
        int len = 0 ;
        while ( ( len = reader.read(buff)) != -1) {
            fileWriter.write(buff,0,len);
        }
        fileWriter.flush();

        // 关闭资源
        reader.close();
        fileWriter.close();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 茉莉味的情话 又是这个时间醒来,一段时间的习惯,已经持续了半年。轻轻打开房门,蜷在阳台的吊蓝里。如同暗夜里的猫。警...
    秋虹阅读 1,454评论 0 1
  • 天天都酒天天都茶,已经不知道是多少个月了。从没想过牢骚,只怪自己不够牛逼,在这大圈圈里,只懂一小半,但从未害怕面对...
    一夕秉阅读 1,193评论 0 0
  • 初一马上要结束了,这时我才发现如何去指导孩子,如何去利用教辅书,如何去记忆知识挖掘重难点,好像陪孩子学习变...
    郭宜欣泽阅读 1,463评论 2 1
  • 彩排完,天已黑
    刘凯书法阅读 9,775评论 1 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 127,742评论 2 7

友情链接更多精彩内容