博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Java】编程练习:文件的读写
阅读量:748 次
发布时间:2019-03-21

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

作业要求

程序功能:

批处理JAVA源文件。

(1) 当有多个包组成一个应用时,编译主类,将自动编译其它相关的类,生成的字节码文件与其源文件在同一目录下。自己编写主类Main在无名包中,类A在包Adam中,类C在包Adam.Carol中,类B在包Betty中。然后编译,让各文件夹中既有源文件,也有字节码文件。

(2) 运行你的程序,通过JFileChoose选择无名包所在的文件夹(假设为E:\tmp),把所有的源程序都拷贝出来,放到

原文件夹的archive下面,仍然保留原来的子目录结构。

(3) 在每个源文件的头部插入如下的logo信息。

在这里插入图片描述

(4) 处理完成,调用消息对话框显示完成信息–共处理了多少个文件。

注:

I.让JFileChoose可以选择文件夹,需调用它的方法:

setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)

II.通过File类,读取一个文件夹下的所有条目,若是源文件,就完成上述的文件操作,读取(复制)+插入logo信息;若是子目录,递归处理。

III.粘贴源码与截图(多张)

代码

import javax.swing.JFileChooser;import javax.swing.JOptionPane;import java.io.*;public class LogoAdder {
static String msg; static JFileChooser dirChooser = new JFileChooser(); 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.Duplicate(Global.originalPath = dirChooser.getSelectedFile().getAbsolutePath()); Global.AddLogo(dirChooser.getSelectedFile().getAbsolutePath() + "\\archive"); msg = "The number of processed files is " + Global.fileNum + "."; JOptionPane.showMessageDialog(null, msg, "Complete!", JOptionPane.INFORMATION_MESSAGE); } }}class Global {
static final byte[] logo = new String("/****************************************\n*Author: aaaabbbbccccdddd *\n*Date: 2020/5/31 *\n****************************************/\n").getBytes(); static byte[] content; static String originalContent, originalPath; static int fileNum = 0; static void Duplicate(String Path) throws IOException {
File dir = new File(Path), archive; File[] fileList = dir.listFiles(); String archivePath; for (File file : fileList) {
if (file.isDirectory()) {
archivePath = originalPath + "\\archive\\" + file.getAbsolutePath().substring(originalPath.length()); archive = new File(archivePath); archive.mkdirs(); Duplicate(file.getAbsolutePath()); } else if (file.getName().endsWith(".java")) {
archivePath = originalPath + "\\archive\\" + file.getAbsolutePath().substring(originalPath.length()); archive = new File(archivePath); if (!archive.exists()) {
archive.createNewFile(); } FileInputStream inputStream = new FileInputStream(file); content = new byte[(int) file.length()]; inputStream.read(content); originalContent = new String(content); FileOutputStream outputStream = new FileOutputStream(archive); outputStream.write(originalContent.getBytes()); inputStream.close(); outputStream.close(); } } } 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")) {
FileInputStream inputStream = new FileInputStream(file); content = new byte[(int) file.length()]; inputStream.read(content); originalContent = new String(content); FileOutputStream outputStream = new FileOutputStream(file); outputStream.write(logo); outputStream.write(originalContent.getBytes()); inputStream.close(); outputStream.close(); ++fileNum; } } }}

结果

修改前:

在这里插入图片描述修改后:

在这里插入图片描述

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

你可能感兴趣的文章