读取文件的目录结构和统计文件的代码信息

读取目录结构 FileInfo.java:

package com.data.io;
import java.io.*;
/**
 * FileInfo.java 
 * 文件信息
 * @author Administrator
 *
 */
public class FileInfo {
    /**
     * 文件(目录)的大小 (字节)
     */
    private long size;
    private int fileSize;
    private int folderSize;
    private File file;
    
    public FileInfo(File file) {
        this.file = file;
    }
    public long getSize() {
        return size;
    }
    public void setSize(long size) {
        this.size = size;
    }
    public int getFileSize() {
        return fileSize;
    }
    public void setFileSize(int fileSize) {
        this.fileSize = fileSize;
    }
    public int getFolderSize() {
        return folderSize;
    }
    public void setFolderSize(int folderSize) {
        this.folderSize = folderSize;
    }

    
    public void print() {
        System.out.printf("文件:%s
", file.getAbsolutePath());
        System.out.printf("大小: %,d
", size);
        System.out.printf("目录: %,d
", folderSize);
        System.out.printf("文件: %,d
", fileSize);
    }
    
    
    
}
View Code

读取代码信息:CodeInfo.java:

package com.data.io;

public class CodeInfo {
    /**
     * 总行
     */
    int total;
    /**
     * 代码行
     */
    int codeLine;
    /**
     * 空行
     */
    int blankLine;
    /**
     * 注释行
     */
    int commentLine;
    
    public void print() {
        System.out.printf("总行:%,d
",total);
        System.out.printf("代码行: %,d
",codeLine);
        System.out.printf("空行: %,d
",blankLine);
        System.out.printf("单注释行: %,d
",commentLine);
    }    
}
View Code

具体实现功能的文件 Model.java:

package com.data.io;

import java.io.*;
import java.awt.*;

import javax.naming.directory.DirContext;
import javax.sound.midi.MidiDevice.Info;
import javax.swing.*;

import javafx.scene.shape.Line;
/**
 * Model.java
 * @author Administrator
 *
 */
public class Model {
    
    /**
     * 统计一个文件中的代码信息
     * 
     * @param sourceFile
     * @return
     */
    public static CodeInfo getCodeInfo(File sourceFile) {
        CodeInfo info = new CodeInfo();
        // 每次读取文件中的一行,这一行什么开始,(/* //),什么时候结束(*/)
        try (BufferedReader in = new BufferedReader(new FileReader(sourceFile))) {
            String line;
            boolean flag = false;
            while (null != (line = in.readLine())) {
                info.total++;

                // 截断前后的空格
                line = line.trim();
                if (line.startsWith("/*"))
                {
                    flag = true;
                }
                if (flag) {
                    // 放在前面防止单行的这个注释法
                    info.commentLine++;
                    if (line.endsWith("*/")) {
                        flag = false;
                    }
                } else {
                    if (line.length() == 0) {
                        // 空行
                        info.blankLine++;
                    }
                    if (line.startsWith("//")) {
                        info.commentLine++;
                    }
                }
            }
            info.codeLine = info.total - info.blankLine - info.commentLine;

        } catch (Exception e) {
            // TODO: handle exception
        }
        return info;
    }
    /**
     * 
     * @param file
     *             目标文件
     * @return FileInfo 文件的统计信息
     */
    public static FileInfo getFileInfo(File file) {
        FileInfo info = new FileInfo(file);
        
        info.setSize(countSize(file));
        info.setFileSize(countFileSize(file));
        info.setFolderSize(countFolderSize(file));
        return info;
    }

    private static int countFolderSize(File dir) {
        int size = 0;    
        
        // 获得目录中的所有内容
        File[] files = dir.listFiles();
        //遍历
        for(File f : files) {
            if(f.isDirectory()) {
                //文件夹则加一
                size++;    
                //进入该目录,统计目录中文件夹的数量
                //注意不能只写countFileSize(f),这样每层都有一个新的size
                size += countFolderSize(f);
            }
        }
        return size;
    }

    private static int countFileSize(File dir) {
        int size = 0;    
        
        // 获得目录中的所有内容
        File[] files = dir.listFiles();
        
        for(File f : files) {
            if(f.isFile()) {
                //文件则加一
                size++;
    
            }else {
                //目录:进入该目录,统计目录中文件的数量
                size += countFileSize(f);
            }
        }
        
        return size;        
        
    }

    private static long countSize(File dir) {
        long size = 0;
        
        // 获得目录中的所有内容
        File[] files = dir.listFiles();
        
        for(File f : files) {
            if(f.isFile()) {
                //文件
                long s = f.length();
                size += s;
            }else {
                //目录
                size += countSize(f);
            }
        }
        
        return size;
    }
}    
View Code

运行文件App.java:

package com.data.io;

import java.io.File;
/**
 * App.java
 * @author Administrator
 *
 */
public class App {
    public static void main(String[] args) {
//        File file = new File("G:/jdk1.8.0_131");
//        FileInfo info = Model.getFileInfo(file);
//        info.print();
        
        CodeInfo info = Model.getCodeInfo(new File("code.txt"));
        info.print();
    }
}
View Code

运行结果:

总行:17
代码行: 7
空行: 2
单注释行: 8

文件:G:jdk1.8.0_131
大小: 458,125,906
目录: 619
文件: 9,196

如果用Swing做个界面的话:

  用windowsbuilder做:

UI.java(运行接口)

package com.data.io;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.Insets;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;

public class UI extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JLabel lblNewLabel_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UI frame = new UI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public UI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 712, 554);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{1.0, 1.0, 0.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);
        
        JLabel lblNewLabel = new JLabel("New label");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        contentPane.add(lblNewLabel, gbc_lblNewLabel);
        
        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.insets = new Insets(0, 0, 5, 5);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 0;
        contentPane.add(textField, gbc_textField);
        textField.setColumns(10);
        
        JButton btnNewButton = new JButton("选择目录");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
//                chooser.setFileFilter(filter);
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int r = chooser.showOpenDialog(UI.this);
                if(r == JFileChooser.APPROVE_OPTION) {
                    File dir = chooser.getSelectedFile();
                    FileInfo info = Model.getFileInfo(dir);
                    //显示结果
                    textField.setText(dir.getAbsolutePath());
                    lblNewLabel.setText(String.format("大小:%,d", info.getSize()));
                }
            }
        });
        GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
        gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
        gbc_btnNewButton.gridx = 2;
        gbc_btnNewButton.gridy = 0;
        contentPane.add(btnNewButton, gbc_btnNewButton);
        
        lblNewLabel_1 = new JLabel("New label");
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_1.gridx = 0;
        gbc_lblNewLabel_1.gridy = 1;
        contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);
        
        JLabel lblNewLabel_2 = new JLabel("New label");
        GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
        gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_2.gridx = 0;
        gbc_lblNewLabel_2.gridy = 2;
        contentPane.add(lblNewLabel_2, gbc_lblNewLabel_2);
        
        JLabel lblNewLabel_3 = new JLabel("New label");
        GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
        gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_3.gridx = 0;
        gbc_lblNewLabel_3.gridy = 3;
        contentPane.add(lblNewLabel_3, gbc_lblNewLabel_3);
        
//        JPanel panel = new JPanel();
        PieView pie = new PieView();
        //        panel.add(pie);
                
                GridBagConstraints gbc_panel = new GridBagConstraints();
                gbc_panel.gridwidth = 3;
                gbc_panel.fill = GridBagConstraints.BOTH;
                gbc_panel.gridx = 0;
                gbc_panel.gridy = 4;
                contentPane.add(pie, gbc_panel);
    }

}
View Code

PieView.java:

package com.data.io;

import java.awt.Graphics;

import javax.swing.JPanel;

import com.sun.prism.paint.Color;

public class PieView extends JPanel{
    
//    double[] data;
//    
//    Color[] colors = {Color.BLUE,}
    @Override
    public void paint(Graphics g) {
        
        super.paint(g);
        //获得panel的宽,高
        int w = getWidth();
        int h = getHeight();
        int x = w/2;
        int y = h/2;
        int r = w<h? w : h ;        
//        g.fillOval(0, 0, w, h);
        g.fillOval(x- r/2, y- r/2, r, r);
//        g.fillRect(w- r/2, h- r/2, r, r);
        

    }
}
View Code

结果:

原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256575.html