博客
关于我
【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/

    你可能感兴趣的文章
    nginx 常用配置记录
    查看>>
    nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
    查看>>
    Nginx 我们必须知道的那些事
    查看>>
    Nginx 源码完全注释(11)ngx_spinlock
    查看>>
    Nginx 的 proxy_pass 使用简介
    查看>>
    Nginx 的 SSL 模块安装
    查看>>
    Nginx 的优化思路,并解析网站防盗链
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    Nginx 相关介绍(Nginx是什么?能干嘛?)
    查看>>
    Nginx 知识点一网打尽:动静分离、压缩、缓存、跨域、高可用、性能优化...
    查看>>
    nginx 禁止以ip形式访问服务器
    查看>>
    NGINX 端口负载均衡
    查看>>
    Nginx 结合 consul 实现动态负载均衡
    查看>>
    Nginx 负载均衡与权重配置解析
    查看>>
    Nginx 负载均衡详解
    查看>>
    Nginx 负载均衡配置详解
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置dist 加上跨域配置
    查看>>
    nginx 配置https(一)—— 自签名证书
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>