Java IO File

读取文件内容

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

        File file = new File("E:\\learn\\b.txt");
        if (file.exists() && file.isFile()) {
            System.out.println("文件存在");
            /**
             * BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
             * 与
             * BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
             * 等价
             * 
             * class FileReader extends InputStreamReader
             * public FileReader(File file) throws FileNotFoundException {
             *      super(new FileInputStream(file));
             * }
             * */
            try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file)) ){
                String line;
                while ((line = bufferedReader.readLine()) != null){
                    System.out.println(line);
                }
            }catch (IOException e){
                System.out.println(e.getMessage());
            }
        } else {
            System.out.println("文件不存在");
        }

    }
}

在这里插入图片描述

文件重命名

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

        File file = new File("E:\\learn\\a.txt");
        if(file.exists()){
            System.out.println("文件存在");
            File destFile = new File("E:\\learn\\b.txt");
            boolean success = file.renameTo(destFile);
            System.out.println(success);
        }else{
            System.out.println("文件不存在");
        }

    }
}

Java文件重命名
Java文件重命名

删除文件

 public static void main(String[] args) {

        File file = new File("E:\\learn\\b.txt");
        if (file.exists() && file.isFile()) {
            System.out.println("文件存在");
            boolean success = file.delete();
            if(success){
                System.out.println("文件删除成功");
            }
        } else {
            System.out.println("文件不存在");
        }

    }

复制文件

   public static void main(String[] args) throws IOException {
        File file = new File("E:\\learn\\hello.txt");
        if (file.exists() && file.isFile()) {

            /**
             * 不会更新文件的修改时间,同源文件保持一致
             * */
            File copyFile = new File("E:\\learn\\hello_copy.txt");
            Files.copy(file.toPath(), copyFile.toPath());

            /**
             * 会更新文件的修改时间
             * */
            File copyFile2 = new File("E:\\learn\\hello_copy2.txt");
            try (FileChannel inputChannel = new FileInputStream(file).getChannel();
                 FileChannel outputChannel = new FileOutputStream(copyFile2).getChannel()
            ) {
                outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
            }
            
        }
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

i余数

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

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

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

打赏作者

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

抵扣说明:

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

余额充值