代码:

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. 漫谈五种IO模型(主讲IO多路复用)

    首先引用levin的回答让我们理清楚五种IO模型 1.阻塞I/O模型 老李去火车站买票,排队三天买到一张退票. 耗费:在车站吃喝拉撒睡 3天,其他事一件没干. 2.非阻塞I/O模型 老李去火车站买票, ...

  2. LeetCode - 61、旋转链表

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  3. IDEA实用教程(八)—— 创建JavaWeb项目

    七. 创建JavaWeb项目 创建工程 1) 第一步 2) 第二步 3) 第三步 如果要修改JavaEE版本,请根据下图所示进行修改 4) 第四步 2. 发布工程 1) 第一步 2) 第二步 3) 第 ...

  4. 使用VISIO绘制组织结构图的操作方法

    1.确保电脑已安装VISIO软件,然后在电脑指定位置点击鼠标右键新建VISIO绘图文档. 2.打开文档,在弹出的选择绘图类型中点击商务.组织结构图,之后点击确定. 3.之后进入绘图主界面,在左侧是形状 ...

  5. 一个关于gcd的等式的证明

    证:$a > b$ 且 $gcd(a,b)=1$,有 $gcd(a^n-b^n, a^m-b^m) = a^{gcd(n, m)} - b^{gcd(n,m)}$. 证明: 假设 $n > ...

  6. httpclient工具使用(org.apache.httpcomponents.httpclient)

    httpclient工具使用(org.apache.httpcomponents.httpclient) 引入依赖 <dependency> <groupId>org.apac ...

  7. iota妙用

    itoa可以套公式,下面的依旧会按照公式运算 package main import "fmt" func main() { const ( b = 1 << (10 ...

  8. rc.local配置

    1.让系统默认启动的时候执行rc.local 启动我们想要启动进程:如:nginx ,memcached,或者是 php-fpm等! /usr/local/bin/redis-server /etc/ ...

  9. Python学习之--用户输入以及运算

    一. 用户输入:input 1. 函数input() 让程序暂停运行,等待用户输入一些文本. 2.使用int()将字符串转为数值 二.运算:+-*. 1.求模运算符 (%)将两个数相除并返回余数: 2 ...

  10. Code Chef October Challenge 2019题解

    传送门 \(MSV\) 设个阈值搞一搞就行了 //quming #include<bits/stdc++.h> #define R register #define pb emplace_ ...