【JAVADYA13-文件操作】

创建文件

package File;

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

public class CreatFile {
    public static void main(String[] args) {
        //三种创建文件得方法(3种不同得构造器)
        //方式一
        creat1();
        creat2();
        creat3();
    }
    public static void creat1(){
        String filepath="E:\\javaProject\\IO\\a.txt";
        File file = new File(filepath);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("创建成功!");
    }
    public static void creat2(){
        File file = new File("E:\\javaProject\\IO\\","b.txt");
        try {
            file.createNewFile();
            System.out.println("创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void creat3(){
        File file = new File("E:\\\\javaProject\\IO\\", "c.txt");
        try {
            file.createNewFile();
            System.out.println("创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

文件操作的基本方法

package File;

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

public class FileInformation {
    public static void main(String[] args) {
        File file = new File("E:\\JavaProject\\IO\\a01.txt");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("绝对路径"+file.getAbsolutePath());
        System.out.println("得到文件父级目录:"+file.getParent());
        System.out.println("文件大小"+file.length()+"B");
        System.out.println("文件是否存在"+file.exists());
        System.out.println("是不是一个目录"+file.isDirectory());
        System.out.println("是不是一个文件"+file.isFile());
    }
}

删除文件/创建目录

package File;

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

public class FileInformation02 {
    public static void main(String[] args) {
        File file = new File("D:\\a0.txt");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //判断D是否存在a.txt,存在就删除
        if(file.exists()){
            if (file.delete()) {
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败");
            }
        }else{
            System.out.println("该文件不存在");
        }

        //判断b文件是否存在,存在则删除
        File file1 = new File("E:\\ccccc\\bbbbb");
        if(file1.exists()){
            if (file1.delete()) {
                System.out.println("删除目录成功 !");
            }else {
                System.out.println("删除失败");
            }
        }else{
            System.out.println("该目录不存在");
        }
        //任务3,判断目录是否存在,不存在就创建

        File file2 = new File("E:\\ccccc\\bbbbb");

        if(file2.exists()){
            System.out.println("该目录存在");
        }else{
            System.out.println("该目录不存在,正在创建!");
            if (file2.mkdirs()) {
                System.out.println("目录创建完成!");
            }else{
                System.out.println("目录创建失败!");
            }
        }
    }
}

读文件操作FileInputStream

package InputStream;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStream001 {
    static String filepath = "D:\\hello.txt";

    public static void main(String[] args) throws IOException {

        File file = new File(filepath);
        file.createNewFile();
        m();
        m2();
    }
    @Test
    public static void m() throws FileNotFoundException {
        //该方法用于读取该文本
        FileInputStream fileInputStream = new FileInputStream(filepath);
        try {
            int read = 0;
            while (true) {
                if ((read=fileInputStream.read()) != -1) {
                    System.out.print((char) read);
                } else {
                    System.out.println("读取完毕!");
                    fileInputStream.close();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public static void m2() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(filepath);
        int read=0;
        byte[] bytes = new byte[8];//一次性读8个
        //使用2个参数得read方法读
        while (true){
            //read返回读取得个数,最后一次read返回-1
            if((read=fileInputStream.read(bytes))!=-1){
                System.out.print(new String(bytes,0,read));
            }else{
                System.out.println("读取完毕");
                fileInputStream.close();
                break;
            }
        }
    }
}

写文件操作FileOutputStream

package File;

import java.io.*;

public class FileOutputStream1 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\zhangsan.txt");
        m(file);
    }
    public static void m(File file) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(file/*可以写true,追加到后面,不然就覆盖了*/);
        fileOutputStream.write('a');
        String s="hello,world!";
        fileOutputStream.write(s.getBytes());
        fileOutputStream.close();

    }
}

文件拷贝

package File;

import java.io.*;

public class FileCopy {
    public static void main(String[] args) throws IOException {
        copyFileUsingFileStreams(new File("C:\\Users\\TMJIE5200\\Music\\莫文蔚-忽然之间_2.flac"), new File("E:\\忽然之间.flac"));
    }
    private static void copyFileUsingFileStreams(File source, File dest)
            throws IOException {
        FileInputStream fileInputStream = new FileInputStream(source);
        FileOutputStream fileOutputStream = new FileOutputStream(dest);
        int read;
        byte[] bytes = new byte[1024];
        while ((read=fileInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,read);
        }
        if(dest.isFile()){
            System.out.println("复制完成!!");
        }else
        {
            System.out.println("复制失败!");
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}


以上就是文件的常用方法,以及增删改查的方式了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Keyle777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值