62,612
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
import static java.nio.file.StandardOpenOption.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test{
public static void main(String[] args){
String source = loadFile();
String content = getContent(source);
System.out.println(content);
}
private static String getContent(String source){
String regex="\\[.*?\\]";//如果不验证有效性的话直接用. regex="\\[.*?\\]";
//如果不够用,需要验证有效性的话可以在回复里留言.我再写.
Matcher matcher = Pattern.compile(regex).matcher(source);
String result = null;
if(matcher.find()){
result = matcher.group(0);
}
return result;
}
private static String loadFile(){
StringBuffer content = new StringBuffer();
try(Scanner file = new Scanner(Files.newInputStream(path,READ));){
while(file.hasNextLine()){
content.append(file.nextLine());
}
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
return content.toString();
}
private static final Path path = Paths.get(System.getProperty("user.dir")).resolve("data.txt");
/*
数据源:data.txt内容:
jsonp1({
"ajaxResult":{
"code":1,
"message":"有数据"
},
"chapterlist":{
"asc":0,
"chapterCount":29,
"chapters":[{"chapterId":24884172,"chapterName":"正文 第一章 死亡与新生","level":0,"orderNum":1,"tomeId":1232432},
{"chapterId":25239091,"chapterName":"正文 第二章 入门","level":0,"orderNum":2,"tomeId":1232432},
{"chapterId":25239096,"chapterName":"正文 第三章 初窥门径","level":0,"orderNum":4,"tomeId":1232432},
{"chapterId":25239176,"chapterName":"正文 第四章 送上门的半吊子师父","level":0,"orderNum":5,"tomeId":1232432}],"pageNum":1,"pageSize":20}})
*/
}