【Java文件】按UTF-8编码读取文本文件(逐行方式),排序,打印到控制台
代码:
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)
测试文件:
1 package com.hy; 2 3 import java.util.Collections; 4 import java.util.LinkedList; 5 import java.util.List; 6 7 /** 8 * Json对象类 9 * @author 逆火 10 * 11 * 2019年12月2日 下午8:17:06 12 */ 13 public class Json implements Comparable<Json>{ 14 15 // There are value types 16 public static final int Type_String=1; 17 public static final int Type_Array=2; 18 public static final int Type_List=3; 19 20 // Key always is String 21 private String key; 22 private Json parent; 23 24 // There are three types of value 25 private int valueType; 26 private String valueString; 27 private List<Json> valueList; 28 29 // indent depth 30 private int depth; 31 32 public Json() { 33 34 } 35 36 /** 37 * Contructor1 38 */ 39 public Json(String key,String value) { 40 this.key=key; 41 this.valueType=Type_String; 42 this.valueString=value; 43 this.depth=0; 44 } 45 46 public Json(String key,int type) { 47 this.key=key; 48 49 if(type==Type_List) { 50 this.valueType=Type_List; 51 this.valueList=new LinkedList<Json>(); 52 }else if(type==Type_Array) { 53 this.valueType=Type_Array; 54 this.valueList=new LinkedList<Json>(); 55 } 56 } 57 58 public List<Json> getValueList() { 59 return valueList; 60 } 61 62 public void addJsonToList(Json json) { 63 if(valueList!=null) { 64 valueList.add(json); 65 json.parent=this; 66 67 adjustDepth(); 68 } 69 } 70 71 public void addJsonToArray(Json json) { 72 if(valueList!=null) { 73 valueList.add(json); 74 json.parent=this; 75 adjustDepth(); 76 } 77 } 78 79 private void adjustDepth() { 80 if(valueType==Type_List) { 81 for(Json json:valueList) { 82 json.depth=this.depth+1; 83 json.adjustDepth(); 84 } 85 86 87 } 88 89 if(valueType==Type_Array) { 90 for(Json json:valueList) { 91 json.depth=this.depth+1; 92 json.adjustDepth(); 93 } 94 } 95 } 96 97 public String toString() { 98 StringBuilder sb=new StringBuilder(); 99 100 // key 101 String tabs=getIndentSpace(); 102 sb.append(tabs); 103 //sb.append("\""+(key==null?"":key)+"\""); 104 105 if(key!=null) { 106 //sb.append("\""+key+"\"");// 以对象构建时恢复 107 sb.append(key);// 以文件构建时打开 108 sb.append(":"); 109 }else { 110 111 } 112 113 // value 114 if(valueType==Type_String) { 115 //sb.append("\""+valueString+"\"");// 以对象构建时恢复 116 sb.append(valueString);// 以文件构建时打开 117 }else if(valueType==Type_Array) { 118 sb.append("[\n"); 119 120 int n=valueList.size(); 121 for(int i=0;i<n;i++) { 122 Json json=valueList.get(i); 123 if(i!=n-1) { 124 sb.append(json.toString()+",\n"); 125 }else { 126 sb.append(json.toString()+"\n"); 127 } 128 } 129 130 sb.append(tabs+"]"); 131 }else if(valueType==Type_List) { 132 sb.append("{\n"); 133 134 Collections.sort(valueList); 135 136 int n=valueList.size(); 137 for(int i=0;i<n;i++) { 138 Json json=valueList.get(i); 139 if(i!=n-1) { 140 sb.append(json.toString()+",\n"); 141 }else { 142 sb.append(json.toString()+"\n"); 143 } 144 } 145 146 sb.append(tabs+"}"); 147 } 148 149 return sb.toString(); 150 } 151 152 public int compareTo(Json other) { 153 return this.key.compareTo(other.key); 154 } 155 156 private String getIndentSpace() { 157 return String.join("", Collections.nCopies(this.depth, " ")); 158 } 159 160 public String getKey() { 161 return key; 162 } 163 164 public void setKey(String key) { 165 this.key = key; 166 } 167 168 public Json getParent() { 169 return parent; 170 } 171 172 public void setParent(Json parent) { 173 this.parent = parent; 174 } 175 176 public static void main(String[] args) { 177 Json id1=new Json("id","001"); 178 Json name1=new Json("name","鐧借彍"); 179 180 Json title=new Json("title",3); 181 title.addJsonToList(id1); 182 title.addJsonToList(name1); 183 184 Json empty1=new Json(null,3); 185 empty1.addJsonToList(new Json("id","001")); 186 empty1.addJsonToList(new Json("id","浣犲ソ鐧借彍")); 187 188 Json empty2=new Json(null,3); 189 empty2.addJsonToList(new Json("id","001")); 190 empty2.addJsonToList(new Json("id","浣犲ソ钀濆崪")); 191 192 Json content=new Json("content",2); 193 content.addJsonToArray(empty1); 194 content.addJsonToArray(empty2); 195 196 Json data=new Json("data",3); 197 data.addJsonToList(title); 198 data.addJsonToList(content); 199 200 Json status=new Json("status","0000"); 201 Json message=new Json("message","success"); 202 203 Json root=new Json(null,3); 204 root.addJsonToList(status); 205 root.addJsonToList(message); 206 root.addJsonToList(data); 207 208 System.out.println(root.toString()); 209 } 210 }
--END-- 2019年11月30日17:42:48
分类:
Java.文本文件操作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)