资源简介

由 Luiz Arantes Sa编写,并不是随书配套的答案,可以作为参考

资源截图

代码片段和文件信息

package AppUtils;


import java.io.*;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Created by Luiz Arantes Sa on 1/29/15.
 */
public class JavaStringRemover {

    public static void main(String[] args) {

        Queue queue = new PriorityQueue<>(1000);
        queue.add(new File(“/Users/Kernel/Desktop/intro-to-java-10th-edition/src“));

        int count = 0;
        while (!queue.isEmpty()) {
            File file = queue.poll();
            if (file.isDirectory()) {
                File[] listFiles = file.listFiles();
                for (File listFile : listFiles) {
                    queue.add(listFile);
                }
            } else {
                if (!file.getName().matches(“.*.java“))
                    continue;
                try (BufferedReader in = new BufferedReader(new FileReader(file))) {
                    StringBuilder sb = new StringBuilder(100000);
                    char[] chars;

                    while ((in.read(chars = new char[(int)file.length()])) != -1)
                        sb.append(chars);


                    in.close();
                    System.out.println(file.getName());
                    String s = filterString(sb.toString());
                    try (BufferedWriter out = new BufferedWriter(new FileWriter(file false))) {
                        out.write(s.toCharArray());
                    }
                    count++;
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }

    public static String filterString(String string) {
        StringBuilder sb = new StringBuilder(string);
        int startIndex = 0;
        while ((startIndex = resetToNextState(sb startIndex)) != -1) {
            if (states[IN_BLOCK])
                startIndex = removeBlockComment(sb startIndex);
            else if (states[IN_LINE])
                startIndex = removeInlineComment(sb startIndex);
            else if (states[IN_STRING])
                startIndex = removeStringLiteral(sb startIndex);
            else if (states[IN_CHAR])
                startIndex = removeCharLiteral(sb startIndex);
        }

        return sb.toString();
    }

    private static int removeCharLiteral(StringBuilder sb int startIndex) {
        return removeString(sb  startIndex tokens[IN_CHAR] “\\“ EXCLUSIVE);
    }

    private static int removeStringLiteral(StringBuilder sb int startIndex) {
        return removeString(sb startIndex  tokens[IN_STRING] “\\“ EXCLUSIVE);
    }

    private static int removeInlineComment(StringBuilder sb int startIndex) {
        return removeString(sb startIndex tokens[IN_LINE] ““ KEEP_END);
    }

    private static int removeBlockComment(StringBuilder sb int startIndex) {
        return removeString(sb  startIndex tokens[IN_BLOCK] ““ INCLUSIVE);
    }


    private static final int INCLUSIVE = 0;
    private static final int EXCLUSIVE = 1;
    private static fin

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-11-09 15:40  intro-to-java-10th-edition-master\
     文件         110  2015-11-09 15:40  intro-to-java-10th-edition-master\.gitignore
     文件         339  2015-11-09 15:40  intro-to-java-10th-edition-master\README.md
     目录           0  2015-11-09 15:40  intro-to-java-10th-edition-master\src\
     目录           0  2015-11-09 15:40  intro-to-java-10th-edition-master\src\AppUtils\
     文件        6057  2015-11-09 15:40  intro-to-java-10th-edition-master\src\AppUtils\JavaStringRemover.java
     目录           0  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\
     文件         480  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_01.java
     文件         370  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_02.java
     文件         614  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_03.java
     文件         526  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_04.java
     文件         369  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_05.java
     文件         439  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_06.java
     文件         437  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_07.java
     文件         568  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_08.java
     文件         524  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_09.java
     文件         634  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_10.java
     文件        1222  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_11.java
     文件        1176  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_12.java
     文件         839  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_01\Exercise_13.java
     目录           0  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\
     文件         758  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exercise_01.java
     文件         781  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exercise_02.java
     文件         628  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exercise_03.java
     文件         737  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exercise_04.java
     文件         879  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exersice_05.java
     文件         956  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exersice_06.java
     文件         872  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exersice_07.java
     文件        1515  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exersice_08.java
     文件         579  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exersice_09.java
     文件         777  2015-11-09 15:40  intro-to-java-10th-edition-master\src\Chapter_02\Exersice_10.java
............此处省略662个文件信息

评论

共有 条评论