IO流Part1练习题

1

private static void copydir(File src, File dest) throws IOException {
    dest.mkdirs();
    // 递归
    // 1. 进入数据源
    File[] files = src.listFiles();
    // 2. 遍历数组
    for (File file : files) {
        if (file.isFile()) {
            // 3. 判断文件,拷贝
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(new File(dest, file.getName()));
            byte[] bytes = new byte[1024];
            int len;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
            fos.close();
            fis.close();
        } else {
            // 4. 判断文件夹,递归
            copydir(file, new File(dest, file.getName()));
        }
    }
}

2

//1.创建对象关联原始文件
FileInputStream fis = new FileInputStream("myio\\ency.jpg");
//2.创建对象关联加密文件
FileOutputStream fos = new FileOutputStream("myio\\redu.jpg");
//3.加密处理
int b;
while((b = fis.read()) != -1){
    fos.write(b ^ 2);
}
//4.释放资源
fos.close();
fis.close();

3

//1.读取数据
FileReader fr = new FileReader("myio\\a.txt");
StringBuilder sb = new StringBuilder();
int ch;
while((ch = fr.read()) != -1){
    sb.append((char)ch);
}
fr.close();
System.out.println(sb);

//2.排序
String str = sb.toString();
String[] arrStr = str.split("-");
ArrayList<Integer> list = new ArrayList<>();
for (String s : arrStr) {
    int i = Integer.parseInt(s);
    list.add(i);
}
Collections.sort(list);
System.out.println(list);

//3.写出
FileWriter fw = new FileWriter("myio\\a.txt");
for (int i = 0; i < list.size(); i++) {
    if(i == list.size() - 1){
        fw.write(list.get(i) + "");
    }else{
        fw.write(list.get(i) + "-");
    }
}

//1.读取数据
FileReader fr = new FileReader("myio\\a.txt");
StringBuilder sb = new StringBuilder();
int ch;
while((ch = fr.read()) != -1){
    sb.append((char)ch);
}
fr.close();
System.out.println(sb);

//2.排序
Integer[] arr = Arrays.stream(sb.toString())
    .split("-") Stream<String>
    .map(Integer::parseInt) Stream<Integer>
    .sorted()
    .toArray(Integer[]::new);

//3.写出
FileWriter fw = new FileWriter("myio\\a.txt");
String s = Arrays.toString(arr).replace(",", replacement: "-");
String result = s.substring(1, s.length() - 1);
fw.write(result);
fw.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值