为了熟悉文件的基本操作,写了一个文件计数器 .. 界面有些简陋..
1: 文件计数的具体实现
import java.io.File;
import javax.swing.JOptionPane;
/**
* 指定路径下文件夹和文件的数目及大小.
* @author d.s
*z
*/
public class FileCount {
/**
* 得到文件数目
*
* @param path
* :指定的路径
* @return 文件个数
*/
public int fCount(String path) {
int countF = 0;
File file = new File(path);
// 路径存在时:
if (file.exists()) {
// 该路径下是一个文件的时候
if (file.isFile()) {
System.out.println("文件路径为:" + file.getAbsolutePath());
countF++;
}
if (file.isDirectory()) {// 该路径下是一个文件夹的时候
// 依次访问该文件夹下的所有文件和文件夹.
File f[] = file.listFiles();
for (int i = 0; i < f.length; i++) {
// 递归调用fCount方法
countF += fCount(f[i].getAbsolutePath());
}
}
} else {
JOptionPane.showMessageDialog(null, "统计文件数目的路径错误!!!");
}
return countF;
}
/**
* 该路径下的文件夹数目
*
* @param path指定的路径
* @return 文件夹数目
*/
public int Dircount(String path) {
int countD = 0;
File file = new File(path);
// 如果路径正确
if (file.exists()) {
// 是文件夹的时候
if (file.isDirectory()) {
System.out.println(file.getAbsolutePath());
countD++;
File f[] = file.listFiles();
for (int i = 0; i < f.length; i++) {
// 递归
countD += Dircount(f[i].getAbsolutePath());
}
}
} else {
JOptionPane.showMessageDialog(null, "统计文件夹的路径错误!!!");
}
return countD;
}
/**
* 得到指定路径下所有文件的大小之和
*
* @param path指定的路径
* @return 指定路径下所有文件的大小之和
*/
public long fileLength(String path) {
long length = 0;
File file = new File(path);
// 如果路径正确
if (file.exists()) {
//如果是文件
if(file.isFile()){
length += file.length();
}
//如果是文件夹
if(file.isDirectory()){
File f[] = file.listFiles();
for(int i = 0; i< f.length;i++){
length += fileLength(f[i].getAbsolutePath());
}
}
}
return length;
}
}
2: 计数器的一个简单界面
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FileCountUI extends JFrame {
public static void main(String args[]) {
FileCountUI ui = new FileCountUI();
ui.showUI();
}
public void showUI() {
this.setTitle("FileCount");
this.setSize(350, 150);
JLabel jl = new JLabel("指定目录:");
JTextField jf = new JTextField(20);
this.add(jl);
this.add(jf);
JButton but1 = new JButton("文件数目");
JButton but2 = new JButton("文件夹数目");
JButton but3 = new JButton("文件的总大小");
//加按钮
this.add(but1);
this.add(but2);
this.add(but3);
this.setLayout(new FlowLayout());//流体式布局
this.setLocationRelativeTo(null);//居中显示
this.setResizable(false);//不可改变大小
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭界面时退出程序
this.setVisible(true);//显示窗体
// 添加监听器
ActionListenerImpl l = new ActionListenerImpl(jf);
but1.addActionListener(l);
but2.addActionListener(l);
but3.addActionListener(l);
}
}
3:监听器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextField;
public class ActionListenerImpl implements ActionListener{
private JTextField jf = new JTextField();
public ActionListenerImpl(JTextField jf){
this.jf = jf;
}
public void actionPerformed(ActionEvent e) {
JButton but= (JButton)e.getSource();//获取事件源
FileCount fc = new FileCount();
if(but.getText() == "文件数目"){
System.out.println("文件数目为:"+fc.fCount(jf.getText()));
}
if(but.getText() == "文件夹数目"){
System.out.println("文件夹的数目为:"+fc.Dircount(jf.getText()));
}
if(but.getText() == "文件的总大小"){
System.out.println(fc.fileLength(jf.getText()));
}
}
}
界面如下