Java宝藏实验资源库(2)字节流

一、实验目的

  1. 掌握输入输出流的基本概念。
  2. 掌握字节流处理类的基本结构。
  3. 掌握使用字节流进行输入输出的基本方法。

二、实验内容过程及结果

  • *17.10 (Split files) Suppose you want to back up a huge file (e.g., a 10-GB AVI file) to a CD-R. You can achieve it by splitting the file into smaller pieces and backing up these pieces separately. Write a utility program that splits a large file into smaller ones using the following command: java
  • Exercise17_10 SourceFile numberOfPieces
  • The command creates the files SourceFile.1, SourceFile.2, . . . , SourceFile.n, where n is numberOfPieces and the output files are about the same size.

*17.10(拆分文件)假设你想备份一个大文件(例如,一个10gb的AVI文件)到CD-R。您可以通过将文件分割成更小的部分并进行备份来实现它这些部分是分开的。编写一个实用程序,将大文件分割成小文件使用以下命令的:

SourceFile numberOfPieces

该命令创建SourceFile文件。1,源文件。2、……SourceFile.n,

其中n为numberOfPieces,输出文件大小大致相同。

运行代码如下 :

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

class FileSplitter {
    public static void main(String[] args) {
        if (args.length!= 2) {
            System.out.println("Usage: java FileSplitter <C:\\Users\\DELL\\Desktop\\算法大题.docx> <3>");
            return;
        }
        String sourceFile = args[0];
        int numberOfPieces = Integer.parseInt(args[1]);
        splitFile(sourceFile, numberOfPieces);
    }

    public static void splitFile(String sourceFile, int numberOfPieces) {
        File file = new File(sourceFile);
        long fileSize = file.length();
        long pieceSize = fileSize / numberOfPieces;
        try (FileInputStream fis = new FileInputStream(file)) {
            for (int i = 1; i <= numberOfPieces; i++) {
                String outputFile = sourceFile + "." + i;
                try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                    int bytesRead;
                    byte[] buffer = new byte[1024];
                    while ((bytesRead = fis.read(buffer))!= -1) {
                        fos.write(buffer, 0, bytesRead);
                        if (fos.getChannel().position() >= pieceSize) {
                            break;
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果 

*17.12 (Combine files) Write a utility program that combines the files together into a new file using the following command:

java Exercise17_12 SourceFile1 . . . SourceFilen TargetFile

The command combines SourceFile1, . . . , and SourceFilen into TargetFile.

FIGURE 17.21

(a)The program splits a file. (b) The program combines files into a new file.

*17.12(组合文件)编写一个实用程序,使用以下命令将文件组合成一个新文件:

java Exercise17_12 SourceFile1…SourceFilen TargetFile

该命令将SourceFile1,…,并将SourceFilen放入TargetFile中。

图17.21

(a)程序拆分文件。(b)程序将文件合并成一个新文件。

运行代码如下 :

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

public class FileCombiner {
    public static void main(String[] args) {
        if (args.length < 3) {
            System.out.println("Usage: java FileCombiner <C:\\Users\\DELL\\Desktop\\实验4题目.docx> <C:\\Users\\DELL\\Desktop\\实验报告格式说明-2 - 副本.docx> <C:\\Users\\DELL\\Desktop\\1.docx>");
            return;
        }
        String targetFile = args[args.length - 1];
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(targetFile);
            for (int i = 0; i < args.length - 1; i++) {
                String sourceFile = args[i];
                FileInputStream fis = new FileInputStream(sourceFile);
                int bytesRead;
                byte[] buffer = new byte[1024];
                while ((bytesRead = fis.read(buffer))!= -1) {
                    fos.write(buffer, 0, bytesRead);
                }
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos!= null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果

三、实验结论 

       通过本次实验实践了拆分与整合知识和操作,得到了代码思想决定了你这个代码的优劣性与可执行性,只有平常多注重思维的培养,才能真写出更高效率程序的感悟,在代码思维方面可以再努力!

 结语   

思维决定高度

态度决定成败

不要成为AI的搬运工

!!! 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT 青年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值