📄 easydefrag.java
字号:
package gongju;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
/**
* This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
* Builder, which is free for non-commercial use. If Jigloo is being used
* commercially (ie, by a corporation, company or business for any purpose
* whatever) then you should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details. Use of Jigloo implies
* acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
* PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
* ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
//方便进行磁盘整理
public class Easydefrag extends JFrame {
private JPanel jPanel1;
private JButton btnDELETE;
private JButton btnADD;
private JTable jTable1;
private JScrollPane panTalbe;
private JTextArea txtMSG;
private JComboBox cbxROOTS;
private JScrollPane jScrollPane1;
private JButton btnDEFRAG;
private DefaultTableModel jTable1Model;
private String root;
private Process process;
/**
* @param args
*/
public Easydefrag() {
super("\u788e\u7247\u6574\u7406");
init();
Exit();
}
public void init() {
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
btnDEFRAG = new JButton();
jPanel1.add(btnDEFRAG);
btnDEFRAG.setText("\u6574\u7406");
btnDEFRAG.setBounds(329, 84, 70, 28);
btnDEFRAG.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
defrag();
}
});
}
{
jScrollPane1 = new JScrollPane();
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(0, 168, 420, 77);
{
txtMSG = new JTextArea();
jScrollPane1.setViewportView(txtMSG);
}
}
{
// 取得电脑中所有的盘符
File[] dev = File.listRoots();
String[] roots = new String[dev.length];
for (int i = 0; i < dev.length; i++) {
roots[i] = dev[i].getPath().replace("\\", "");
}
ComboBoxModel cbxROOTSModel = new DefaultComboBoxModel(roots);
cbxROOTS = new JComboBox();
jPanel1.add(cbxROOTS);
cbxROOTS.setModel(cbxROOTSModel);
cbxROOTS.setBounds(7, 7, 56, 28);
}
{
panTalbe = new JScrollPane();
jPanel1.add(panTalbe);
panTalbe.setBounds(0, 77, 252, 91);
{
jTable1Model = new DefaultTableModel(new String[][] {},
new String[] { "盘符", "属性" });
jTable1 = new JTable();
panTalbe.setViewportView(jTable1);
jTable1.setModel(jTable1Model);
jTable1.getAutoResizeMode();
jTable1.setRowHeight(25);
}
}
{
btnADD = new JButton();
jPanel1.add(btnADD);
btnADD.setText("\u6dfb\u52a0");
btnADD.setBounds(105, 7, 63, 28);
btnADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String[] property = new String[2];
String choose = cbxROOTS.getSelectedItem().toString();
property[0] = choose;
jTable1Model.addRow(property);
txtMSG.setText("\u65b0\u589e\u6210\u529f");// 新增成功
}
});
}
{
btnDELETE = new JButton();
jPanel1.add(btnDELETE);
btnDELETE.setText("\u5220\u9664");
btnDELETE.setBounds(329, 119, 70, 28);
btnDELETE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
jTable1Model.removeRow(jTable1.getEditingRow());
txtMSG.setText("\u5220\u9664\u6210\u529f");// 删除成功
} catch (Exception e) {
txtMSG
.setText("\u8bf7\u9009\u62e9\u5220\u9664\u7684\u5217");// 请选择删除的列
}
}
});
}
}
{
setSize(425, 289);
setVisible(true);
}
}
public void defrag() {
StringBuffer err = new StringBuffer();
root = new String();// 存放所有行的值
int column = jTable1.getColumnCount();// 列
int row = jTable1.getRowCount();// 行
try {
jTable1.editCellAt(0, 0);// 离开焦点,使取最后一个值时不会取到NULL
} catch (Exception e) {
err.append("\u8bf7\u5148\u65b0\u589e\u4e00\u884c\u6570\u636e");// 请先新增一行数据
}
for (int i = 0; i < row; i++) {
root += jTable1.getValueAt(i, 0).toString() + " ";// 取得当前的值
}
System.out.print(root);
Runtime rt = Runtime.getRuntime();
try {
// 用法:
// defrag <volume> [-a] [-f] [-v] [-?]
// volume 驱动器号或装载点(d: 或 d:\vol\mountpoint)
// -a 只分析
// -f 即使可用空间不足,也强制碎片整理
// -v 详述结果
// -? 显示这段帮助文字
// eg:defrag c: -a -f -v ->c:\\1.log
process = rt.exec("CMD /C defrag " + root + "-a -f -v");
{
String msg;
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while ((msg = bufferedReader.readLine()) != null) {
System.out.println(msg);
txtMSG.append(msg + "\n");
}
}
// 执行错误命令时
{
String errmsg;
BufferedReader err_bufferedReader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
while ((errmsg = err_bufferedReader.readLine()) != null) {
System.out.println(errmsg);
txtMSG.append(errmsg + "\n");
}
}
// 进程等待
// process.waitFor();
// 结束进程
process.destroy();
} catch (Exception e) {
txtMSG.setText("未知错误!!!");// 打开文件失败
rt.gc();
}
}
public void Exit() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
// 结束进程
if (process != null)
process.destroy();
dispose();
}
});
}
public static void main(String[] args) {
new Easydefrag();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -