博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读取文件的目录结构和统计文件的代码信息
阅读量:6901 次
发布时间:2019-06-27

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

读取目录结构 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\n", file.getAbsolutePath());        System.out.printf("大小: %,d\n", size);        System.out.printf("目录: %,d\n", folderSize);        System.out.printf("文件: %,d\n", 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\n",total);        System.out.printf("代码行: %,d\n",codeLine);        System.out.printf("空行: %,d\n",blankLine);        System.out.printf("单注释行: %,d\n",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
View Code

结果:

 

转载于:https://www.cnblogs.com/zhangmingzhao/p/7256575.html

你可能感兴趣的文章
10. Python面向对象
查看>>
python3与 python2 urllib模块区别
查看>>
关于props 和state
查看>>
跟我学算法-tensorflow 实现线性拟合
查看>>
redis使用管道pipeline提升批量操作性能(php演示)
查看>>
python: file_handling 解决工作中出现的文件处理需求
查看>>
HTML5 拖放(Drag 和 Drop)功能开发——浅谈dataTransfer对象
查看>>
灰度图像亮度对比度调整的简单代码
查看>>
shell测试题上机实验
查看>>
[转]二维数组和二级指针的传递问题
查看>>
nginx+fastcgi+c/c++搭建高性能Web框架
查看>>
[转载]安装archlinux 以后没有 ifconfig,route ,nslo
查看>>
人见人爱A^B
查看>>
zoj 3795 Grouping tarjan缩点 + DGA上的最长路
查看>>
浏览器内核
查看>>
zabbix-server安装部署配置
查看>>
终于解决 xUnit.net 测试中无法输出到控制台的问题
查看>>
【素数筛】分解质因数
查看>>
【ADT】队列的基本C语言实现
查看>>
NYOJ-1057 寻找最大数(三)(贪心)
查看>>