IO流的基本应用及问文件操作

package ss;

import java.io.*;
import java.util.Arrays;


public class I5O {
    public static void main(String[] args) {
        //qq();//文件的复制
        qw();//遍历目录

    }

    private static void qw() {
        //包括目录下的文件及目录
        File file = new File("d:/测试文件夹");
        //第一种遍历方法
       /* File[] files = file.listFiles();
        for (File file1 : files) {
            System.out.println(file1.getAbsolutePath());
        }*/
        //第二种遍历方法
        File[] fs = file.listFiles(((dir, name) -> name.startsWith("fd")));
        System.out.println(fs.length);
        System.out.println(Arrays.toString(fs));
        for (File f : fs) {
            System.out.println(f.getName());
            System.out.println(f.getAbsolutePath());
        }
    }

    private static void qq() {
        File file = new File("user/fd_02.jpg");
        File file1 = new File("user/fd_03.jpg");
        try (FileInputStream fis = new FileInputStream(file1); FileOutputStream fos = new FileOutputStream(file)) {

            int b;
            while ((b = fis.read()) != -1) {
                fos.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
package ss;

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

public class I8O {
    public static void main(String[] args) {
    tt();//判断两个文件是否是同一个文件
    }

    private static void tt() {
        String s1 = "d:/测试文件夹/a.txt";
        String s2 = "d:/测试文件夹/b.txt";
        File file1 = new File(s1);
        File file2 = new File(s2);
        //int b , c;
        int b,c;
        byte[] v = new byte[1024];//为了提高读取的速度
        try(var Fis = new FileInputStream(file1);var fis = new FileInputStream(file2)) {
            while ((b= Fis.read(v))!=-1&&(c=fis.read(v))!=-1){
                System.out.println(b);
                System.out.println(c);
                if (b==c){
                    System.out.println("√");
                }else {
                    System.out.println("×");
                }
            }
           
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

说明: 上面两段代码中的文件路径都是已经存在的,学习时应注意电脑中是否存在该文件夹,或者可以改变代码中的文件的路径

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值