代码:

package findJavaMemberFunction;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 查找一个Java源文件中的成员函数名
 *
 */
public class FindFunctionNames {
    public static void main(String[] args) {

        try {
            // (\\s+):group(2) 匹配一个或多个空格
            // (\\S+):group(3) 匹配返回值如void,String
            // (\\s+):group(4) 匹配一个或多个空格
            // ([_a-zA-Z]+[_a-zA-Z0-9]*):group(5) 匹配函数名
            // ([(]([^()]*)[)]):group(1) 匹配函数的参数
            java.util.regex.Pattern pattern=Pattern.compile("(\\s+)(public|protected|private|static)(\\s+)(\\S+)(\\s+)([_a-zA-Z]+[_a-zA-Z0-9]*)([(]([^()]*)[)])");

            List<String> list=new ArrayList<String>();
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\logs\\Json.java"), "UTF-8"));
            String line = null;
            int lineIndex=0;
            while( ( line = br.readLine() ) != null ) {
                lineIndex++;

                Matcher matcher=pattern.matcher(line);
                while(matcher.find()) {
                    System.out.println("Line " + lineIndex +":" + matcher.group(6)+ matcher.group(7));
                }

                list.add(line);
            }
            br.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

输出:

Line 58:getValueList()
Line 62:addJsonToList(Json json)
Line 71:addJsonToArray(Json json)
Line 79:adjustDepth()
Line 97:toString()
Line 152:compareTo(Json other)
Line 156:getIndentSpace()
Line 160:getKey()
Line 164:setKey(String key)
Line 168:getParent()
Line 172:setParent(Json parent)
Line 176:main(String[] args)

测试文件:

 package com.hy;

 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;

 /**
  * Json对象类
  * @author 逆火
  *
  * 2019年12月2日 下午8:17:06
  */
 public class Json implements Comparable<Json>{

     // There are value types
     public static final int Type_String=1;
     public static final int Type_Array=2;
     public static final int Type_List=3;

     // Key always is String
     private String key;
     private Json parent;

     // There are three types of value
     private int valueType;
     private String valueString;
     private List<Json> valueList;

     // indent depth
     private int depth;

     public Json() {

     }

     /**
      * Contructor1
      */
     public Json(String key,String value) {
         this.key=key;
         this.valueType=Type_String;
         this.valueString=value;
         this.depth=0;
     }

     public Json(String key,int type) {
         this.key=key;

         if(type==Type_List) {
             this.valueType=Type_List;
             this.valueList=new LinkedList<Json>();
         }else if(type==Type_Array) {
             this.valueType=Type_Array;
             this.valueList=new LinkedList<Json>();
         }
     }

     public List<Json> getValueList() {
         return valueList;
     }

     public void addJsonToList(Json json) {
         if(valueList!=null) {
             valueList.add(json);
             json.parent=this;

             adjustDepth();
         }
     }

     public void addJsonToArray(Json json) {
         if(valueList!=null) {
             valueList.add(json);
             json.parent=this;
             adjustDepth();
         }
     }

     private void adjustDepth() {
         if(valueType==Type_List) {
             for(Json json:valueList) {
                 json.depth=this.depth+1;
                 json.adjustDepth();
             }

         }

         if(valueType==Type_Array) {
             for(Json json:valueList) {
                 json.depth=this.depth+1;
                 json.adjustDepth();
             }
         }
     }

     public String toString() {
         StringBuilder sb=new StringBuilder();

         // key
         String tabs=getIndentSpace();
         sb.append(tabs);
         //sb.append("\""+(key==null?"":key)+"\"");

         if(key!=null) {
             //sb.append("\""+key+"\"");// 以对象构建时恢复
             sb.append(key);// 以文件构建时打开
             sb.append(":");
         }else {

         }

         // value
         if(valueType==Type_String) {
             //sb.append("\""+valueString+"\"");// 以对象构建时恢复
             sb.append(valueString);// 以文件构建时打开
         }else if(valueType==Type_Array) {
             sb.append("[\n");

             int n=valueList.size();
             for(int i=0;i<n;i++) {
                 Json json=valueList.get(i);
                 if(i!=n-1) {
                     sb.append(json.toString()+",\n");
                 }else {
                     sb.append(json.toString()+"\n");
                 }
             }

             sb.append(tabs+"]");
         }else if(valueType==Type_List) {
             sb.append("{\n");

             Collections.sort(valueList);

             int n=valueList.size();
             for(int i=0;i<n;i++) {
                 Json json=valueList.get(i);
                 if(i!=n-1) {
                     sb.append(json.toString()+",\n");
                 }else {
                     sb.append(json.toString()+"\n");
                 }
             }

             sb.append(tabs+"}");
         }

         return sb.toString();
     }

     public int compareTo(Json other) {
         return this.key.compareTo(other.key);
     }

     private String getIndentSpace() {
         return String.join("", Collections.nCopies(this.depth, "    "));
     }

     public String getKey() {
         return key;
     }

     public void setKey(String key) {
         this.key = key;
     }

     public Json getParent() {
         return parent;
     }

     public void setParent(Json parent) {
         this.parent = parent;
     }

     public static void main(String[] args) {
         Json id1=new Json("id","001");
         Json name1=new Json("name","鐧借彍");

         Json title=new Json("title",3);
         title.addJsonToList(id1);
         title.addJsonToList(name1);

         Json empty1=new Json(null,3);
         empty1.addJsonToList(new Json("id","001"));
         empty1.addJsonToList(new Json("id","浣犲ソ鐧借彍"));

         Json empty2=new Json(null,3);
         empty2.addJsonToList(new Json("id","001"));
         empty2.addJsonToList(new Json("id","浣犲ソ钀濆崪"));

         Json content=new Json("content",2);
         content.addJsonToArray(empty1);
         content.addJsonToArray(empty2);

         Json data=new Json("data",3);
         data.addJsonToList(title);
         data.addJsonToList(content);

         Json status=new Json("status","0000");
         Json message=new Json("message","success");

         Json root=new Json(null,3);
         root.addJsonToList(status);
         root.addJsonToList(message);
         root.addJsonToList(data);

         System.out.println(root.toString());
     }
 }

--END-- 2019年11月30日17:42:48

【Java文件】按UTF-8编码读取文本文件(逐行方式),排序,打印到控制台的更多相关文章

  1. jdk编译java文件时出现:编码GBK的不可映射字符

    出现此问题的几种解决办法: 1.cmd下使用javac编译java文件 如: javac test.java 解决办法:编译时加上encoding选项 javac -encoding UTF-8 te ...

  2. Java文件读写操作指定编码方式防乱码

    读文件:BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而提供字符.数组和行的高效读取. 可以指定缓冲区的大小,或者可使用默认的大小.大多数情况下,默认值就足够大了. 通常,Re ...

  3. Java文件读写操作指定编码方式。。。。。

    读: File file=new File(this.filePath);BufferedReader br=new BufferedReader(new InputStreamReader(new ...

  4. 读取文本文件中的中文打印到Eclipse控制台为何显示问号

    原因:未将文本文件存为utf-8编码格式而是ascii编码格式.

  5. JAVA支持字符编码读取文件

    文件操作,在java中很常用,对于存在特定编码的文件,则需要根据字符编码进行读取,要不容易出现乱码 /** * 读取文件 * @param filePath 文件路径 */ public static ...

  6. myeclipse下java文件乱码问题解决

    中文乱码是因为编码格式不一致导致的.1.进入Eclipse,导入一个项目工程,如果项目文件的编码与你的工具编码不一致,将会造成乱码.2.如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文 ...

  7. linux下sort命令使用详解---linux将文本文件内容加以排序命令

    转载自:http://www.cnblogs.com/hitwtx/archive/2011/12/03/2274592.html linux下sort命令使用详解---linux将文本文件内容加以排 ...

  8. 自动判断文本文件编码来读取文本文件内容(.net版本和java版本)

    .net版本 using System; using System.IO; using System.Text; namespace G2.Common { /// <summary> / ...

  9. (转) Java读取文本文件中文乱码问题

    http://blog.csdn.net/greenqingqingws/article/details/7395213 最近遇到一个问题,Java读取文本文件(例如csv文件.txt文件等),遇到中 ...

随机推荐

  1. Android自动化测试探索(四)uiautomator2简介和使用

    uiautomator2简介 项目Git地址: https://github.com/openatx/uiautomator2 安装 #1. 安装 uiautomator2 使用pip进行安装, 注意 ...

  2. mac安装openjdk8-maven-mysql-git-docker

    1. openjdk8安装命令查看地址:https://github.com/AdoptOpenJDK/homebrew-openjdk#other-versions   感觉上面命令地址不靠谱,还是 ...

  3. 初识面向对象(钻石继承,super,多态,封装,method,property,classmethod,staticmethod)

    组合 什么有什么的关系 一个类的对象作为另一个类的对象继承 子类可以使用父类中的名字(静态属性 方法)抽象类和接口类 只能不继承,不能被实例化 子类必须实现父类中的同名方法———规范代码 metacl ...

  4. Linux系统运维相关的面试题 (问答题)

    这里给大家整理了一些Linux系统运维相关的面试题,有些问题没有标准答案,希望要去参加Linux运维面试的朋友,可以先思考下这些问题.   一.Linux操作系统知识 1.常见的Linux发行版本都有 ...

  5. websocket实现心跳连接

    在使用websocket的时候,遇到了一个websocket在连接一段时间就异常断开连接了.第一想法就是重新去连接websocket(websock.onopen),后来发现这种方式是错误的,查阅文档 ...

  6. 题解 洛谷P2258 【子矩阵】

    应该很容易想到暴力骗分. 我们考虑暴力\(dfs\)枚举所有行的选择,列的选择,每次跑一遍记下分值即可. 时间复杂度:\(O(C_n^r \times C_m^c \times r \times c) ...

  7. JQuery实现品牌展示

    最近验收了ITOO,老师当时验收的时候对于界面的设计非常敏感,只要看了一个大体轮廓,就能给出我们建议,这是二十年积累的经验,我们要做的就是站在巨人的肩膀上,让我们成长更快! 老师说了一下关于界面设计的 ...

  8. Oracle的instr()函数和substr()函数

    INSTR()函数 可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符. 在一个字符串中查找指定的字符,返回被查找到的指定的字符的位置. 语法: instr(sourceString ...

  9. 创建型模式(二) 工厂方法模式(Factory Method)

    一.动机(Motivation) 在软件系统创建过程中,经常面临着"某个对象"的创建工作:由于需求的变化,这个对象(的具体实现)经常面临着剧烈的变化,但是它却拥有比较稳定的接口.如 ...

  10. 脚本安装redis

    #!/bin/bash read -p 'input the version you want(like 5.0.5):' version read -p 'input redis password: ...