代码:

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. SQL SERVER-查询存储运行状态

    SELECT p.name as SPName, qs.last_elapsed_time as [LastExecTime(ms)], (total_elapsed_time as [AvgExec ...

  2. git 分支查看与切换

    git 分支查看与切换 # 1.查看所有分支 > git branch -a # 2.查看当前使用分支(结果列表中前面标*号的表示当前使用分支) > git branch # 3.切换分支 ...

  3. IDEA远程连接和上传文件到服务器

    公司电脑是win,所以远程控制服务器就不能用之前自己笔记本ubuntu自带的终端了. 后来在万能的群友的提醒下,IDEA本身就自带了远程功能,摸索了一下,使用IDEA连接服务器并且可以上传文件了. 这 ...

  4. docer安装之pure-ftp

    https://hub.docker.com/r/stilliard/pure-ftpd Docker Pure-ftpd Server https://hub.docker.com/r/stilli ...

  5. WCF 学习系列——WCF的学习基础

    这个系列的博客由WCF4 高级编程学习记录,如有错误请指正. 首先介绍一些概念: SOA: (Service-Oriented Architecture 面向服务架构),一种架构方法,也是一种编程模式 ...

  6. [转] ubuntu16.04添加系统 service, 并设置开机自动启动

    转:https://www.jianshu.com/p/1958878646bd 1. 创建pfly.service文件 2.  执行 systemctl daemon-reload 3. 执行 sy ...

  7. Nmap扫描工具实验报告

    实验报告 实验内容 通过ping进行操作系统探测 利用Zenmap/Nmap进行TCP connet扫描.TCP SYN扫描和操作系统扫描 实验目的 了解扫描的一般步骤 熟练使用ping命令并能够进行 ...

  8. vue中移动端自适应的postcss-plugin-px2rem插件

    flexible 主要是用来响应式改变根元素的字体大小 安装命令 npm install lib-flexible --save 在main.js里面导入命令import 'lib-flexible' ...

  9. 使用Mybatis

    Pom.xml:项目对象模型,主要描述了项目---包括配置文件,开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性以及其它与项目相关的因素.

  10. [DUBBO] qos-server can not bind localhost:22222错误解决

    问题描述 在启动dubbo-consumer工程时报错,信息如下: 2019-11-29 09:22:18.001 ERROR [RMI TCP Connection(2)-127.0.0.1] [o ...