Author Topic: [java] LinesCounter  (Read 1198 times)

0 Members and 1 Guest are viewing this topic.

Offline sherlock

  • /dev/null
  • *
  • Posts: 7
  • Cookies: 2
    • View Profile
[java] LinesCounter
« on: April 17, 2014, 01:30:27 pm »
This is just some sloppy code i quickly wrote.

It simply counts the amount of lines in all files you select.
extensions -> only processes files, which have one of the extensions
count empty lines -> counts empty lines when toggled

Code: [Select]
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class LinesCounter extends JPanel {

    public static void main(String[] args) {
        LinesCounter linesCounter = new LinesCounter();
    }

    private final JFrame frame;

    private final JCheckBox countEmptyLines;
    private final JTextField extensions;
    private final JButton chooseFiles;
    private final JLabel label;

    private JFileChooser fileChooser;
    private File[] files;
    private int returnVal;

    private final ArrayList<String> extensionStrings;

    private final ActionListener chooseFilesListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fileChooser.setMultiSelectionEnabled(true);
            returnVal = fileChooser.showDialog(LinesCounter.this, "Choose Files");
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                files = fileChooser.getSelectedFiles();
                JOptionPane.showMessageDialog(null,
                        "" + countLines(files, getExtensions(), countEmptyLines.isSelected()),
                        "Amount of lines", JOptionPane.INFORMATION_MESSAGE);
            }
        }

    };

    public LinesCounter() {
        super(new GridLayout(4, 1));

        extensionStrings = new ArrayList<String>();
        countEmptyLines = new JCheckBox("count empty lines");
        extensions = new JTextField();
        chooseFiles = new JButton("choose files");
        chooseFiles.addActionListener(chooseFilesListener);
        label = new JLabel("eg:.java;.doc;.class");
        this.add(countEmptyLines);
        this.add(extensions);
        this.add(label);
        this.add(chooseFiles);

        frame = new JFrame();
        frame.add(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private int countLines(final File[] files, final String[] extensions,
            final boolean countEmptyLines) {
        if (files == null) {
            return 0;
        }
        int lines = 0;
        for (final File file : files) {
            lines += countLines(file, extensions, countEmptyLines);
        }
        return lines;
    }

    private int countLines(final File file, final String[] extensions, final boolean countEmptyLines) {
        if (file == null || !file.exists()) {
            return 0;
        }
        if (file.isDirectory()) {
            int lines = 0;
            final File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                for (final File f : files) {
                    lines += countLines(f, extensions, countEmptyLines);
                }
            }
            return lines;
        } else {
            final String fileName = file.getName();
            if (fileName != null && stringEndsWith(fileName, extensions)) {
                try {
                    final BufferedReader bR = new BufferedReader(new FileReader(file));
                    String line = bR.readLine();
                    int lines = 0;
                    while (line != null) {
                        if (!line.isEmpty()) {
                            lines++;
                        } else {
                            if (countEmptyLines) {
                                lines++;
                            }
                        }
                        line = bR.readLine();
                    }
                    bR.close();
                    return lines;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    private boolean stringEndsWith(final String string, final String... extensions) {
        if (string == null || extensions == null) {
            return false;
        }
        for (final String extension : extensions) {
            if (string.endsWith(extension)) {
                return true;
            }
        }
        return false;
    }

    private String[] getExtensions() {
        final Scanner scanner = new Scanner(extensions.getText());
        scanner.useDelimiter(";");
        extensionStrings.clear();
        while (scanner.hasNext()) {
            extensionStrings.add(scanner.next());
        }
        scanner.close();
        return extensionStrings.toArray(new String[extensionStrings.size()]);
    }

}