【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)
测试文件:
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编码读取文本文件(逐行方式),排序,打印到控制台的更多相关文章
- jdk编译java文件时出现:编码GBK的不可映射字符
出现此问题的几种解决办法: 1.cmd下使用javac编译java文件 如: javac test.java 解决办法:编译时加上encoding选项 javac -encoding UTF-8 te ...
- Java文件读写操作指定编码方式防乱码
读文件:BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而提供字符.数组和行的高效读取. 可以指定缓冲区的大小,或者可使用默认的大小.大多数情况下,默认值就足够大了. 通常,Re ...
- Java文件读写操作指定编码方式。。。。。
读: File file=new File(this.filePath);BufferedReader br=new BufferedReader(new InputStreamReader(new ...
- 读取文本文件中的中文打印到Eclipse控制台为何显示问号
原因:未将文本文件存为utf-8编码格式而是ascii编码格式.
- JAVA支持字符编码读取文件
文件操作,在java中很常用,对于存在特定编码的文件,则需要根据字符编码进行读取,要不容易出现乱码 /** * 读取文件 * @param filePath 文件路径 */ public static ...
- myeclipse下java文件乱码问题解决
中文乱码是因为编码格式不一致导致的.1.进入Eclipse,导入一个项目工程,如果项目文件的编码与你的工具编码不一致,将会造成乱码.2.如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文 ...
- linux下sort命令使用详解---linux将文本文件内容加以排序命令
转载自:http://www.cnblogs.com/hitwtx/archive/2011/12/03/2274592.html linux下sort命令使用详解---linux将文本文件内容加以排 ...
- 自动判断文本文件编码来读取文本文件内容(.net版本和java版本)
.net版本 using System; using System.IO; using System.Text; namespace G2.Common { /// <summary> / ...
- (转) Java读取文本文件中文乱码问题
http://blog.csdn.net/greenqingqingws/article/details/7395213 最近遇到一个问题,Java读取文本文件(例如csv文件.txt文件等),遇到中 ...
随机推荐
- 网卡启动安装dell服务器OS
参照视频 需要将boot改成bois启动 https://www.dell.com/support/contents/cn/zh/cndhs1/videos/videoPlayer/k1ajZzdjo ...
- js中绑定事件处理函数,使用event以及传递额外数据
IE8中使用attachEvent绑定事件处理函数时,不能直接向event 对象添加数据属性.可以用属性复制的方法,包装新的event对象. 1. 属性复制var ObjectExtend = fun ...
- 使用Mybatis
Pom.xml:项目对象模型,主要描述了项目---包括配置文件,开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性以及其它与项目相关的因素.
- H5性能测试,首屏时间统计(Argus)
Argus 腾讯质量开发平台,官网链接:https://wetest.qq.com/product/argus 主要针对性:H5的游戏性能测试 主要介绍: 独家首屏时间统计: 告别人工掐秒 自动统计首 ...
- Mycat简单配置
最近项目中需要用到Mycat来作为Mysql的分表中间件.所以稍微研究了一下. Mycat使用起来是非常方便,而且最重要的是配置简单,稍显麻烦的就是需要对库中的每一个表都进行配置. 记录一下最重要的几 ...
- Hive节点及原理
1.什么是Hive:hive是一种基于hadoop的数据仓库,能够将结构化的数据映射成一张表,并提供HQL进行查询.其数据是存储在hdfs上,本质是将sql命令转化成MapReduce来执行. 2.H ...
- 180908 python 字符串内建函数
Python 的字符串内建函数 Python 的字符串常用内建函数如下: 序号 方法及描述 1 capitalize()将字符串的第一个字符转换为大写 2 center(width, fillchar ...
- Tensorflow细节-P89-collection的使用
知识总结 (1)再次注意summary的使用 (2)x = rdm.rand(dataset_size, 2) y_ = [[x1**2 + x2**2] for (x1, x2) in x]这里的问 ...
- 比较ping,tracert和pathping等命令之间的关系
无论你是一个网络维护人员,还是正在学习TCP/IP协议,了解和掌握一些常用的网络测试命令将会有助于您更快地检测到网络故障所在,同时也会有助你您了解网络通信的内幕. 下面我们逐步介绍几个常用的命令: 1 ...
- 012——matlab判断变量是否存在
(一)参考文献:https://www.ilovematlab.cn/thread-48319-1-1.html (二) clc clear a = exist('a') ans =1 clc cle ...