当前位置: 首页 > news >正文

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的搬运工

!!! 

相关文章:

  • ios CCLanguage.m
  • VMware ESXi 8.0U2c macOS Unlocker OEM BIOS Huawei (华为) FusionServer 定制版
  • 3D开发工具HOOPS为BIM应用提供支持:复杂大模型实现Web端轻量化!
  • 【Android面试八股文】Kotlin内置标准函数also的原理是什么?
  • Java学习 - 网络静态路由与动态路由 讲解
  • Ubuntu 24.04安装zabbix7.0.0图形中文乱码
  • MinIO 网络与覆盖网络
  • 【0-1系列】从0-1快速了解搜索引擎Scope以及如何快速安装使用(下)
  • rclone 上传资料到 onedrive 遇到限速问题解决
  • JS中正则表达式的语法与使用案例
  • 字节豆包全新图像Tokenizer:生成图像最低只需32个token,最高提速410倍
  • elementUI的table使用展开功能( type=“expand“ ),展开时合起上一次展开的内容,始终保持展开内容为一个,并且再次点击合起自身
  • 敏捷开发笔记(第7章节)--什么是敏捷设计
  • 八爪鱼现金流-027,以后别再做软件开发了,累了,要有自己的作品
  • deepin 加入甲辰计划,共建 RISC-V 繁荣生态
  • [译]CSS 居中(Center)方法大合集
  • ComponentOne 2017 V2版本正式发布
  • flutter的key在widget list的作用以及必要性
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • JavaScript新鲜事·第5期
  • js作用域和this的理解
  • Next.js之基础概念(二)
  • Objective-C 中关联引用的概念
  • REST架构的思考
  • Twitter赢在开放,三年创造奇迹
  • vue学习系列(二)vue-cli
  • 创建一个Struts2项目maven 方式
  • 仿天猫超市收藏抛物线动画工具库
  • 工作手记之html2canvas使用概述
  • 和 || 运算
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 浅谈web中前端模板引擎的使用
  • 区块链分支循环
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 应用生命周期终极 DevOps 工具包
  • scrapy中间件源码分析及常用中间件大全
  • # 详解 JS 中的事件循环、宏/微任务、Primise对象、定时器函数,以及其在工作中的应用和注意事项
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • (1)SpringCloud 整合Python
  • (C#)获取字符编码的类
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (七)理解angular中的module和injector,即依赖注入
  • (学习日记)2024.02.29:UCOSIII第二节
  • (原)本想说脏话,奈何已放下
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划