Java宝藏实验资源库(5)字符流

一、实验目的

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

二、实验内容过程及结果  

**12.12 (Reformat Java source code) Write a program that converts the Java source

code from the next-line brace style to the end-of-line brace style. For example,

the following Java source in (a) uses the next-line brace style. Your program

converts it to the end-of-line brace style in (b).

HexFormatException

VideoNote

 

Your program can be invoked from the command line with the Java source

code file as the argument. It converts the Java source code to a new format. For

example, the following command converts the Java source-code file Test.java

to the end-of-line brace style.

java Exercise12_12 Test.java

* * 12.12(重新格式化Java源代码)编写一个程序转换Java源代码从下一行大括号样式到行尾大括号样式的代码。

例如,下面(a)中的Java源代码使用了下一行大括号样式。你的程序将其转换为(b)中的行尾大括号样式。

       可以使用Java源代码从命令行调用您的程序代码文件作为参数。它将Java源代码转换为新的格式。为到行尾大括号样式。

java练习12_12测试

运行代码如下 :  

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class BraceStyleConverter {public static void main(String[] args) {if (args.length == 0) {System.out.println("Usage: java BraceStyleConverter <input_file>");return;}String inputFile = args[0];String outputFile = "converted_" + inputFile;try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));FileWriter writer = new FileWriter(outputFile)) {String line;boolean insideBlock = false;while ((line = reader.readLine()) != null) {line = line.trim();if (line.startsWith("{")) {insideBlock = true;}if (insideBlock) {writer.write(line);if (line.endsWith("}")) {writer.write("\n");insideBlock = false;} else {writer.write(" ");}} else {writer.write(line + "\n");}}} catch (IOException e) {e.printStackTrace();}System.out.println("Conversion completed. Output written to " + outputFile);}
}

运行结果  

 

**12.18 (Add package statement) Suppose you have Java source files under the direc

tories chapter1, chapter2, . . . , chapter34. Write a program to insert the

statement package chapteri; as the first line for each Java source file under

the directory chapteri. Suppose chapter1, chapter2, . . . , chapter34

are under the root directory srcRootDirectory. The root directory and

chapteri directory may contain other folders and files. Use the following

command to run the program:

java Exercise12_18 srcRootDirectory

* * 12.18(添加包语句)假设在目录下有Java源文件托利党第一章,第二章……, chapter34。编写一个程序来插入语句包章节;作为下面每个Java源文件的第一行目录章。假设第一章,第二章,…, chapter34都在根目录srcRootDirectory下。根目录和Chapteri目录可能包含其他文件夹和文件。使用以下命令命令运行程序:

java Exercise12_18 srcRootDirectory

 运行代码如下 :

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class PackageStatementInserter {public static void main(String[] args) {if (args.length == 0) {System.out.println("Usage: java PackageStatementInserter <srcRootDirectory>");return;}String srcRootDirectory = args[0];insertPackageStatements(srcRootDirectory);System.out.println("Package statements inserted successfully.");}public static void insertPackageStatements(String srcRootDirectory) {File rootDir = new File(srcRootDirectory);if (!rootDir.exists() || !rootDir.isDirectory()) {System.out.println("Invalid source root directory.");return;}File[] chapterDirs = rootDir.listFiles(File::isDirectory);if (chapterDirs == null) {System.out.println("No subdirectories found in the source root directory.");return;}for (File chapterDir : chapterDirs) {File[] javaFiles = chapterDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".java"));if (javaFiles == null) {continue; // Skip directories without Java files}for (File javaFile : javaFiles) {try {insertPackageStatement(javaFile);} catch (IOException e) {e.printStackTrace();}}}}private static void insertPackageStatement(File javaFile) throws IOException {String originalFilePath = javaFile.getAbsolutePath();String tempFilePath = originalFilePath + ".tmp";try (BufferedReader reader = new BufferedReader(new FileReader(originalFilePath));FileWriter writer = new FileWriter(tempFilePath)) {// Insert package statement as the first lineString packageStatement = getPackageStatement(javaFile);if (packageStatement != null) {writer.write(packageStatement + "\n");}// Copy the rest of the fileString line;while ((line = reader.readLine()) != null) {writer.write(line + "\n");}}// Replace the original file with the modified temp filejavaFile.delete();new File(tempFilePath).renameTo(new File(originalFilePath));}private static String getPackageStatement(File javaFile) throws IOException {String chapterName = javaFile.getParentFile().getName(); // Assumes chapter directories are named appropriatelyString packageName = "chapter" + chapterName; // Adjust as needed based on your directory structurereturn "package " + packageName + ";";}
}

 运行结果 

 

**12.20 (Remove package statement) Suppose you have Java source files under the directories chapter1, chapter2, . . . , chapter34. Write a program to remove the statement package chapteri; in the first line for each Java source file under the directory chapteri.    Supposechapter1, chapter2, . . . , chapter34 are under the root directory srcRootDirectory. The root directory and chapteri directory may contain other folders and files. Use the following command to run the program:

java Exercise12_20 srcRootDirectory

* * 12.20(删除包语句)假设Java源文件在目录chapter1, chapter2,…, chapter34。编写程序…删除语句包章节;在每个Java的第一行中目录chapteri下的源文件。假设第一,第二章,……、chapter34都在根目录srcRootDirectory下。根目录和chapteri目录可能包含其他文件夹和文件。使用执行以下命令运行程序:

java Exercise12_20 srcRootDirectory

 运行代码如下 :

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class PackageStatementRemover {public static void main(String[] args) {if (args.length == 0) {System.out.println("Usage: java PackageStatementRemover <srcRootDirectory>");return;}String srcRootDirectory = args[0];removePackageStatements(srcRootDirectory);System.out.println("Package statements removed successfully.");}public static void removePackageStatements(String srcRootDirectory) {File rootDir = new File(srcRootDirectory);if (!rootDir.exists() || !rootDir.isDirectory()) {System.out.println("Invalid source root directory.");return;}File[] chapterDirs = rootDir.listFiles(File::isDirectory);if (chapterDirs == null) {System.out.println("No subdirectories found in the source root directory.");return;}for (File chapterDir : chapterDirs) {File[] javaFiles = chapterDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".java"));if (javaFiles == null) {continue; // Skip directories without Java files}for (File javaFile : javaFiles) {try {removePackageStatement(javaFile);} catch (IOException e) {e.printStackTrace();}}}}private static void removePackageStatement(File javaFile) throws IOException {String originalFilePath = javaFile.getAbsolutePath();String tempFilePath = originalFilePath + ".tmp";try (BufferedReader reader = new BufferedReader(new FileReader(originalFilePath));FileWriter writer = new FileWriter(tempFilePath)) {// Skip the first line (package statement)reader.readLine(); // Assuming the first line is always the package statement// Copy the rest of the fileString line;while ((line = reader.readLine()) != null) {writer.write(line + "\n");}}// Replace the original file with the modified temp filejavaFile.delete();new File(tempFilePath).renameTo(new File(originalFilePath));}
}

运行结果  

三、实验结论   

       通过本次实验实践了字符流输入输出功能的知识和操作,得到了调用子功能包时一定要注重效率,感悟到对程序的底层逻辑一定要了解,不能一味地求速学,该精学时还得筑牢基础。

 结语   

 喜欢的事情就要做到极致

决不能半途而废

!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/31823.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

RPCMon:一款基于ETW的RPC监控工具

关于RPCMon RPCMon是一款基于事件跟踪的WindowsRPC监控工具&#xff0c;该工具是一款GUI工具&#xff0c;可以帮助广大研究人员通过ETW&#xff08;Event Tracing for Windows&#xff09;扫描RPC通信。 RPCMon能够为广大研究人员提供进程之间RPC通信的高级视图&#xff0c;该…

【DICOM】BitsAllocated字段值为8和16时区别

一、读取dicom C# 使用fo-dicom操作dicom文件-CSDN博客 二、DICOM中BitsAllocated字段值为8和16时区别 位深度差异&#xff1a; 当BitsAllocated为8时&#xff0c;意味着每个像素使用8位来表示其灰度值。这允许每个像素有2^8256种不同的灰度等级&#xff0c;适用于那些不需要高…

WPF 深入理解一、基础知识介绍

基础知识 本系列文章是对个人 B站 up 微软系列技术教程 记录 视频地址 https://www.bilibili.com/video/BV1HC4y1b76v/?spm_id_from333.999.0.0&vd_source0748f94a553c71a2b0125078697617e3 winform 与 wpf 异同 1.winform 项目结构 编辑主要是在 Form1.cs(页面)&#…

AI音乐革命:创意产业的新篇章

随着科技的飞速发展&#xff0c;人工智能&#xff08;AI&#xff09;在各个领域的应用越来越广泛&#xff0c;特别是在音乐产业中&#xff0c;AI音乐大模型的涌现&#xff0c;正在重新定义音乐创作的边界。最近一个月&#xff0c;随着多个音乐大模型的轮番上线&#xff0c;素人…

顶顶通呼叫中心中间件-机器人测试流程(mod_cti基于FreeSWITCH)

感兴趣的话可以点后面链接添加联系方式顶顶通小孙 一、打开ccadmin-web并且创建分机 1、登录ccadmin-web 登录地址&#xff1a;http://ddcti.com:88 登录之后根据下图去登录ccadmin-web系统。 2、创建分机 点击呼叫中心 -> 点击分机设置 -> 点击新增&#xff0c;点击…

重新整理了新版JSON工具类和一些见解

封装好了&#xff0c;工具类&#xff0c;直接粘贴就好了:所需依赖 springboot&#xff0c;hutool package com.jmj.gulimall.product.utils;import cn.hutool.core.bean.BeanUtil; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annota…

技术管理转型之战:决策之道-管理中的智慧与策略

文章目录 引言一、决策的重要性二、常见的决策方式1. 理性决策&#xff08;Rational Decision Making&#xff09;2. 有限理性&#xff08;Bounded Rationality&#xff09;3. 直觉决策&#xff08;Intuitive Decision Making&#xff09;4. 循证管理&#xff08;Evidence-Base…

一文读懂Java多线程并发之内存模型

什么是内存模型? Java内存模型(Java Memory Model)描述了Java编程语言中的线程如何与内存进行交互,是和多线程相关的一组规范,需要各个 JVM 的实现来遵守 JMM 规范,以便于开发者可以利用这些规范,更方便地开发多线程程序。有了这些规范,即便同一个程序在不同操作系统的虚…

WHAT - HTTP keep-alive 持久性连接和内存泄漏问题

目录 一、介绍HTTP 持久性连接&#xff08;persistent connection&#xff09;实现细节示例持久性连接的优化管道化&#xff08;Pipelining&#xff09;HTTP/2 和 HTTP/3 二、Node.js HTTP Agent 开启 keepAlive 导致的内存泄漏问题Node.js HTTP Agent 和 Socket 池Keep-Alive …

聚焦 Navicat 17 新特性 | 查询与配置的革新之处

随着 Navicat 17 的发布&#xff0c;引起业界热烈讨论与关注&#xff0c;这也标志着 Navicat 的产品力再次飞跃。新版本引入的众多创新特性极大地提升了用户在数据库管理和数据分析方面的体验&#xff0c;涵盖模型设计与同步、数据字典、数据分析&#xff08;data profiling&am…

图说SpringCloudStream消息驱动

SpringCloud Stream消息驱动实现原理 通过定义Binder绑定器作为中间层&#xff0c;实现了应用程序和消息中间件之间实现细节的隔离。通过向应用程序暴露统一的Channel通道&#xff0c;可以让应用程序不再需要考虑各种不同的消息中间件实现的兼容性问题。当需要升级消息中间件&a…

第九届世界渲染大赛什么时候开始举办?

​第九届世界渲染大赛即将开启&#xff0c;全球设计师和艺术家将汇聚一堂&#xff0c;展现3D艺术的创新与美感。敬请期待这场业界顶级的视觉盛宴&#xff0c;让我们共同关注大赛的启幕时刻。 第九届世界渲染大赛开始时间 预计时间&#xff1a;2024年7月(中旬) 报名方法&#…

服务端代码编写中MySql大小写在Java中报错问题解决

报错信息&#xff1a; 原因&#xff1a;MySql和Java变量大小写产生的冲突。 经过查阅各个博客等&#xff0c;得出浅显结论&#xff08;不一定对&#xff09;&#xff1a;MySql大小写不敏感&#xff0c;Java大小写敏感&#xff0c;当Javabean转为MySql数据库表时&#xff0c;Ja…

微信小程序生命周期

微信小程序的生命周期包括两个主要部分&#xff1a;应用生命周期和页面生命周期。下面我将详细介绍它们的具体内容。 应用生命周期 onLaunch&#xff1a; 触发时机&#xff1a;小程序初始化完成时&#xff08;全局只触发一次&#xff09;。 用途&#xff1a;通常用于进行一些…

高效处理大数据:Kafka的13个核心概念详解

我是小米,一个喜欢分享技术的29岁程序员。如果你喜欢我的文章,欢迎关注我的微信公众号“软件求生”,获取更多技术干货! 大家好,我是你们的小米!今天我们来深入探讨一下Kafka这个强大而复杂的数据流平台。Kafka被广泛应用于高吞吐量、低延迟的数据流应用场景中。那么,我…

名校介绍|英国六所红砖大学

​近年来由于美国的拒签率增加&#xff0c;很多公派申请者&#xff0c;尤其是CSC资助的访问学者、公派联合培养学生及博士后研究学者&#xff0c;把出国目标改为其它发达国家&#xff0c;尤以英国居多&#xff0c;本文知识人网小编就重点介绍六所英国红砖大学。 我们在“英国大…

Standalone原理讲解与实操演示

这课的一个学习的话&#xff0c;我们基本上了解了flink对不同的一个集群资源管理器上的一个支持&#xff0c;以及我们的一个net ude部署模式上面的一个特点的一些个了解。通过本课的学习&#xff0c;我们将去逐步去了解一下flink on standard allow的这种集群部署模式上面的一个…

基于JSP技术的固定资产管理系统

开头语&#xff1a;你好呀&#xff0c;我是计算机学长猫哥&#xff01;如果有相关需求&#xff0c;文末可以找到我的联系方式。 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;JSPServlet 工具&#xff1a;MyEclipse、Tomcat 系统展示 首页 注册界面…

RocketMQ的安装和原理

.RocketMQ的安装 一.RocketMQ安装 1.1.下载RocketMQ 下载地址&#xff1a;http://rocketmq.apache.org/release_notes/release-notes-4.2.0/ 下载后解压 Bin : 可执行文件目录 config&#xff1a;配置文件目录 Lib : 依赖库&#xff0c;一堆Jar包 1.2.配置ROCKETMQ_HOME…

uniapp中Error: project.configjson: libVersion 字段需为 string. string

错误如下 找到manifestjson文件到源码视图 添加这段代码"libVersion": "latest",即可