作業要求
程序功能:
批處理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;
}
}
}
}
結果
修改前:
修改後: