博客
关于我
【Java】编程练习:文件的读写
阅读量:751 次
发布时间:2019-03-21

本文共 2077 字,大约阅读时间需要 6 分钟。

作业要求与代码优化

本任务要求开发一个批处理Java源文件的程序,该程序应具备以下功能:

1. 在文件夹中处理所有Java源文件(.java)

- 支持多个包结构,无需手动编译,可以自动处理相关依赖文件。
- 生成编译后的字节码文件,并与源文件保持在同一目录下。

2. 文件选择与处理

- 使用JFileChoose工具选择文件夹,支持选择无名包所在的文件夹(默认路径E:\tmp)。 - 批量复制所有源程序,保留子目录结构,存储到archive下。

3. 插入Logo操作

- 在每个源文件的开头插入指定的logo图片路径。

4. 显示处理结果

- 完成处理后,通过消息对话框显示总共处理了多少个文件。

技术要求:

I. 调用JFileChoose的方法:

dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

II. 文件处理逻辑:

for (File file : fileList) { if (file.isDirectory()) { // 递归处理子目录 AddLogo(file.getAbsolutePath()); } else if (file.getName().endsWith(".java")) { // 处理源文件 // 复制文件并添加logo // 更新文件数 fileNum++; }}

III. 代码结构:

public class LogoAdder { private static JFileChooser dirChooser = new JFileChooser(); private static int dirChooserResult;
public static void main(String[] args) throws IOException {    dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);    dirChooserResult = dirChooser.showOpenDialog(null);    if (dirChooserResult == JFileChooser.APPROVE_OPTION) {        Global.originalPath = dirChooser.getSelectedFile().getAbsolutePath();        Global.AddLogo(Global.originalPath + "\\archive");        msg = "处理完毕:共处理了" + Global.fileNum + "个文件。";        JOptionPane.showMessageDialog(null, msg, "处理完成", JOptionPane.INFORMATION_MESSAGE);    }}

}

class Global {private static byte[] logo = "...".getBytes();private static byte[] content;private static String originalContent;private static int fileNum = 0;

private static void AddLogo(String path) throws IOException {    File dir = new File(path);    File[] fileList = dir.listFiles();    for (File file : fileList) {        if (file.isDirectory()) {            AddLogo(file.getAbsolutePath());        } else if (file.getName().endsWith(".java")) {            // 复制文件并添加logo            fileNum++;            // 具体实现细节...        }    }}

}

测试结果与修复:

在实际运行过程中发现的问题:

  • 文件选择功能异常
    解决方案:

  • dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);// 确保正确处理文件夹选择
    1. 处理文件数量不统计
      解决方案:

    2. Global.fileNum++;
      1. logo插入位置错误
        解决方案:

      2. // 在文件读取前或读取后插入logo

        最终测试结果显示,程序能够正确批量处理多个文件夹内的Java源文件,自动添加logo,统计文件数并弹出完成提示。

        通过优化,本程序已经能够满足要求,且代码结构清晰易懂,功能完整。

    转载地址:http://rrtez.baihongyu.com/

    你可能感兴趣的文章
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    node
    查看>>
    node exporter完整版
    查看>>
    node HelloWorld入门篇
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node JS: < 二> Node JS例子解析
    查看>>
    Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime(93)解决
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>
    Node 裁切图片的方法
    查看>>
    node+express+mysql 实现登陆注册
    查看>>
    Node+Express连接mysql实现增删改查
    查看>>
    node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
    查看>>
    Node-RED中Button按钮组件和TextInput文字输入组件的使用
    查看>>
    vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
    查看>>
    Node-RED中Switch开关和Dropdown选择组件的使用
    查看>>
    Node-RED中使用exec节点实现调用外部exe程序
    查看>>
    Node-RED中使用function函式节点实现数值计算(相加计算)
    查看>>
    Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
    查看>>