SCAU JAVA 送给嗷嗷待哺的你

[/Library/rmi]   Result:

Files Details:
+rmi
    +CORBA        -ClassDesc.java                              Total:        48,Blank:         4,       685 Bytes
        -GetORBPropertiesFileAction.java             Total:       108,Blank:        18,      2660 Bytes
        -PortableRemoteObjectDelegate.java           Total:        84,Blank:         9,      1929 Bytes
        -Stub.java                                   Total:       261,Blank:        43,      7679 Bytes
        -StubDelegate.java                           Total:        93,Blank:        10,      2169 Bytes
        -Tie.java                                    Total:        85,Blank:         9,      1726 Bytes
        -Util.java                                   Total:       421,Blank:        55,     13813 Bytes
        -UtilDelegate.java                           Total:       135,Blank:        19,      3070 Bytes
        -ValueHandler.java                           Total:        96,Blank:         9,      2816 Bytes
        -ValueHandlerMultiFormat.java                Total:        70,Blank:         4,      1970 Bytes
    +ssl        -SslRMIClientSocketFactory.java              Total:       212,Blank:        10,      8013 Bytes
        -SslRMIServerSocketFactory.java              Total:       378,Blank:        22,     14735 Bytes
    -.DS_Store                                   Total:         9,Blank:         2,      8196 Bytes
    -PortableRemoteObject.java                   Total:       293,Blank:        42,      9411 Bytes
Total:
                            14 JavaFiles
                          2293 lines in files
                           256 blank lines
                         78872 bytes

-.DS_Store 是隐藏文件 我删不掉

package com.company;

import java.io.File;
import java.util.Comparator;

public class CompareName implements Comparator<File> {
     
    @Override
    public int compare(File p1,File p2){
     
        return p1.getName().compareTo(p2.getName());
    }
}

package com.company;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
     

    ///Library/rmi
    ///Users/tangmaoxin/IdeaProjects/Exercise/src/com/company/result
    // 记得到时候把文件删掉
    public static void main(String[] args) throws IOException {
     
        String path = "/Users/tangmaoxin/IdeaProjects/Exercise/src/com/company/result";

        System.out.println("-------MENU-------");
        System.out.println("1.分析目录中源文件");
        System.out.println("2.查看分析结果");
        System.out.println("0.退出程序");
        System.out.println("------------------");


        int n;
        Scanner cin = new Scanner(System.in);
        n = cin.nextInt();
        cin.nextLine();
        if(n == 1){
     
            String files;
            System.out.println("输入一个目录名:");
            files = cin.nextLine();

            File file = new File(files);
            JavaSourceCounter counter = new JavaSourceCounter();


            if(file.isDirectory()) {
     

                File file2 = new File(path,file.getName()+".txt" );

                if (!file2.exists()) {
     
                    try {
     
                        file2.createNewFile();
                    } catch (IOException e) {
     
                        e.printStackTrace();
                    }
                }

                FileOutputStream fos = new FileOutputStream(path + "/" + file.getName()+".txt");

                    int b = 1;

                String str = "["+file.getAbsoluteFile()+"]   Result:"+"\n\n"+"Files Details:"+"\n"+"+"+file.getName()+"\n";
                byte[] bytes = str.getBytes();
                fos.write(bytes);

                File[] items = file.listFiles();
                Arrays.sort(items,new CompareName());

                for(int i=0;i<items.length;i++){
     
                    if(items[i].isDirectory()){
     
                        for(int j=0;j<b*4;j++){
     
                            fos.write(' ');
                        }
                        fos.write('+');
                        str = items[i].getName();
                        bytes = str.getBytes();
                        fos.write(bytes);

                        File[] files1 = items[i].listFiles();
                        Arrays.sort(files1,new CompareName());
                        for(int i1=0;i1< files1.length;i1++){
     
                            counter.setNumberOfJavaFile(1+counter.getNumberOfJavaFile());
                            counter.setNumberOfLine((int)counter.getLineNumber(files1[i1])+counter.getNumberOfLine());
                            counter.setNumberOfbyte(counter.getNumberOfbyte()+(int)counter.getFileLength(files1[i1]));
                            counter.setNumberOfBlank(counter.getNumberOfBlank()+(int)counter.getBlank(files1[i1]));

                            for(int j=0;j<(b+1)*4;j++){
     
                                fos.write(' ');
                            }
                            str = String.format("-%-40s    Total:%10d,Blank:%10d,%10d Bytes\n",files1[i1].getName(),counter.getLineNumber(files1[i1]),counter.getBlank(files1[i1]),counter.getFileLength(files1[i1]));
                            bytes = str.getBytes();
                            fos.write(bytes);
                        }
                    }

                }
                for(int i=0;i<items.length;i++){
     
                    if(items[i].isFile()){
     
                        counter.setNumberOfJavaFile(1+counter.getNumberOfJavaFile());
                        counter.setNumberOfLine((int)counter.getLineNumber(items[i])+counter.getNumberOfLine());
                        counter.setNumberOfbyte(counter.getNumberOfbyte()+(int)counter.getFileLength(items[i]));
                        counter.setNumberOfBlank(counter.getNumberOfBlank()+(int)counter.getBlank(items[i]));

                        for(int j=0;j<b*4;j++){
     
                            fos.write(' ');
                        }
                        str = String.format("-%-40s    Total:%10d,Blank:%10d,%10d Bytes\n",items[i].getName(),counter.getLineNumber(items[i]),counter.getBlank(items[i]),counter.getFileLength(items[i]));
                        bytes = str.getBytes();
                        fos.write(bytes);
                    }
                }

                str = String.format("Total:\n%30d JavaFiles\n%30d lines in files\n%30d blank lines\n%30d bytes",counter.getNumberOfJavaFile(),counter.getNumberOfLine(),counter.getNumberOfBlank(),counter.getNumberOfbyte());
                bytes = str.getBytes();
                fos.write(bytes);
                fos.close();


            }

            else {
     
                System.out.println("["+files + "]不是合法的目录名称!");
            }

        }

        else if(n==2){
     
            File file = new File(path);
            File[] result = file.listFiles();
            for(int i=1;i<=result.length;i++)
            {
     
                System.out.println("------------------");
                System.out.println(i+"."+result[i-1].getName());
                System.out.println("------------------");
            }
            int w;
            w = cin.nextInt();
            if(w>0&&w<=result.length+1){
     
                try{
     
                    BufferedReader in = new BufferedReader(new FileReader(result[w-1]));
                    String s="";
                    while((s=in.readLine())!=null)
                        System.out.println(s);
                    in.close();
                }
                catch(Exception e) {
     
                    e.printStackTrace();
                }
            }


        }

    }
}
package com.company;

import java.io.*;
import java.util.Scanner;

public class JavaSourceCounter {
     
    private int  numberOfJavaFile ;
    private int numberOfLine;
    private int numberOfBlank;
    private int numberOfbyte;

    public int getNumberOfBlank() {
     
        return numberOfBlank;
    }

    public void setNumberOfBlank(int numberOfBlank) {
     
        this.numberOfBlank = numberOfBlank;
    }

    public int getNumberOfbyte() {
     
        return numberOfbyte;
    }

    public void setNumberOfbyte(int numberOfbyte) {
     
        this.numberOfbyte = numberOfbyte;
    }

    public int getNumberOfLine() {
     
        return numberOfLine;
    }

    public void setNumberOfLine(int numberOfLine) {
     
        this.numberOfLine = numberOfLine;
    }

    public int getNumberOfJavaFile() {
     
        return numberOfJavaFile;
    }

    public void setNumberOfJavaFile(int numberOfJavaFile) {
     
        this.numberOfJavaFile = numberOfJavaFile;
    }



    JavaSourceCounter(){
     
        this.numberOfJavaFile = 0;
        this.numberOfLine = 0;
                this.numberOfBlank =0;
        this.numberOfbyte = 0;
    }

    public long getLineNumber(File file) throws IOException {
     
        if(file.isFile()){
     
            String line;
            int lines = 0;
            BufferedReader in = new BufferedReader(new FileReader(file));
            line = in.readLine();
            while(null != line){
     
                lines++;
                line = in.readLine();
            }
            in.close();
            return lines;
        }
        return 0;
    }

    public int getBlank(File file) throws IOException {
     
        if(file.isFile()){
     
            String line;
            int blank = 0;
                BufferedReader in = new BufferedReader(new FileReader(file));
                line = in.readLine();
                while(null != line){
     
                    if(line.length()==0){
     
                        blank++;
                    }
                    line = in.readLine();
                }
                in.close();
               return blank;
        }
        return 0;
    }

    public long getFileLength(File file){
     
        return file.length();
    }

    public int getFilesNumber(File folder){
     
        int numberOfJavaFiles =0;
        File[] items = folder.listFiles();
        for (File item : items) {
     
            if (item.isFile()) {
     
                numberOfJavaFiles++;
            }
    }
        return numberOfJavaFiles;
}





}

你可能感兴趣的:(Java基础)