代码:

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. p7.BTC-挖矿总结

    全节点: 1 一直在线 2 在本地硬盘上维护完全的区块链信息 3 在内存里维护UTXO集合,以便快速检验交易的正确性 4 监听比特币网络上的交易信息,验证每个交易的合法性 5 决定哪些交易会被打包到区 ...

  2. mysql数据库备份,主从复制及半同步复制

    1.使用mysqldump备份数据库并通过备份及二进制日志还原数据(备份完后再写入数据,然后再删库) mysqldump -A --single-transaction -F --master-dat ...

  3. World发布博客测试

    姓名 地址 博客 www.bk.com  插入一个单元格并放入代码 using System; using System.Collections.Generic; using System.Linq; ...

  4. 搭建React项目环境【1】

    1.安装NodeJS6.0以上自带npm依赖包管理工具 2.webstrom 2019.2 工具 1.在cmd输入node -v就可以看到node的当前版本 2.在输入node进入node环境 3.查 ...

  5. 解决 SSH Connection closed by foreign host 问题

    Xshell 报错 : 1.有可能是IP和别人冲突,改一下IP就好了 2.也有可能是文件权限的问题.改一下: cd /etc/ssh/ chmod ssh_host_* chmod *.pub 3.也 ...

  6. .net框架 - Enum枚举

    概要 在C#或C++,java等一些计算机编程语言中,枚举类型是一种基本数据类型而不是构造数据类型. 在C语言等计算机编程语言中,它是一种构造数据类型. 它用于声明一组命名的常数,当一个变量有几种可能 ...

  7. Spring源码窥探之:Condition

    采用注解的方式来注入bean 1. 编写config类 /** * @author 70KG * @Title: ConditionConfig * @Description: * @date 201 ...

  8. c++ templat乱测

    该上机实验环境 linux mint  IDE:qt5.11   代码复制到windows下vs2017报错,提示char* 类型不能直接赋值字符串 在linux mint下可以运行,测试目的:检验复 ...

  9. 3D-2D透视投影数学推导

    请尊重作者,禁止私自盗用图片!

  10. LightOJ - 1170 - Counting Perfect BST(卡特兰数)

    链接: https://vjudge.net/problem/LightOJ-1170 题意: BST is the acronym for Binary Search Tree. A BST is ...