IO操作实验(JAVA)

2021.06.07上机题
题目描述:
在这里插入图片描述
17.17代码:

package Practice;

import java.io.*;

public class Ch17Q17 {
    public static void main(String[] args) {
        try {
            FileOutputStream MyFile = new FileOutputStream(new File("outFile.dat"));
            BitOutputStream MyOutString = new BitOutputStream(MyFile);
            for (int i = 0; i < 10; i++) {
                MyOutString.writeBit('0');
                MyOutString.writeBit('1');
            }
            MyOutString.writeBit("01010101010101011");
            MyOutString.close();
        }
        catch (IOException io) {
            io.getStackTrace();
        }
        System.out.println("Finish");
    }
}


class BitOutputStream extends FilterOutputStream {
    private int count = 0;
    private int value;
    private int flag = 1;       //00000001

    public BitOutputStream(OutputStream out) {
        super(out);
    }

    public void writeBit(char bit) throws IOException {
        count++;
        value = value << 1;

        if (bit == '1')
            value = value | flag;

        if (count == 8) {
            out.write(value);
            count = 0;
        }
    }

    public void writeBit(String string) throws IOException {
        for (int i = 0; i < string.length(); i++)
            writeBit(string.charAt(i));
    }

    public void close() throws IOException {
        if (count > 0) {
            value = value << (8 - count);
            out.write(value);
        }
        super.close();
    }
}

17.10代码:

import java.io.*;
//java Exercise17_10 test.mp4 3

public class Exercise17_10 {
    public static void main(String[] args) throws java.lang.Exception {
        if (args.length != 2) {
            System.out.println("Usage: java Exercise17_10 SourceFile numberOfPieces");
            System.exit(1);
        }

        try (
                BufferedInputStream input = new BufferedInputStream(new FileInputStream(
                        new File(args[0])));
        ) {
            int numberOfPieces = Integer.parseInt(args[1]);

            System.out.println("File size: " + input.available() + " bytes");
            long fileSize = input.available();
            int splitFileSize = (int)
                    Math.ceil(1.0 * fileSize / numberOfPieces);

            for (int i = 1; i <= numberOfPieces; i++) {
                try (
                        BufferedOutputStream output = new BufferedOutputStream(
                                new FileOutputStream(new File(args[0] + "." + i)));
                ) {
                    int value;
                    int count = 0;

                    while (count++ < splitFileSize && (value = input.read()) != -1) {
                        output.write(value);
                    }
                }
            }
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值