Java解析txt中的url-字符串截取

txt文件内容:fetch("http://qnvod.renrenjiang.cn/fragments/z1.rrjqnlive.2217905015/1629979121023-1629979127852.ts", {"referrer": "http://ke.renrenjiang.cn/","referrerPolicy": "strict-origin-when-cross-origin","body": null,"method": "GET","mode": "cors"}); ;截取文件中的url。直接上代码:package com.zx.com.cn.dao;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.List;import org.apache.commons.io.FileUtils;/*** json格式中提取URL* @author admin**/public class JsonGetURL {public static void main(String[] args) {//已知json的源码getLocalFile("C:\\Users\\admin\\Desktop\\login.txt");}//解析json代码,得到相应的url,写入文件txt中。private static void getLocalFile(String localFilePath) {StringBuffer linkSB = new StringBuffer();if (localFilePath != null && localFilePath != "") {try {File input = new File(localFilePath);//要查找的数据开头:http://qnvod.renrenjiang.cn/fragments/z1.rrjqnlive.2217905015/String strSearch ="http://qnvod.renrenjiang.cn/fragments/z1.rrjqnlive.2217905015/";//要查找的数据的结尾:.tsString endSearch = ".ts";//获取文件中的字符串。List<String> lines = null;//采取读行的方式lines = FileUtils.readLines(input, "UTF-8");for (int i = 0; i < lines.size(); i++) {//判断是否包含开头的字符串if (lines.get(i).indexOf(strSearch) != -1) {//判断是否包含结尾的字符串if(lines.get(i).indexOf(endSearch) != -1) {System.out.println("url = "+lines.get(i));//截取中间的字符串String str1=lines.get(i).substring(0, lines.get(i).indexOf(strSearch));String id=lines.get(i).substring(str1.length(), lines.get(i).indexOf(endSearch));System.out.println("id = "+id);String realURL = id+".ts";System.out.println("realURL = " + realURL);linkSB.append(realURL);linkSB.append("\r\n");}}}//将数据写入文件中toFile(linkSB.toString(), "D:\\url.txt");} catch (IOException e) {e.printStackTrace();}}}/**** 将数据(字符串)写入文档。** @param line** @param toFilePath**/private static void toFile(String line, String toFilePath) {File des = new File(toFilePath);if (!des.exists()) { // 判断是否存在,不存在就创建try {// 创建文件des.createNewFile();} catch (IOException e) {e.printStackTrace();}}BufferedWriter writer;try {writer = new BufferedWriter(new FileWriter(toFilePath));System.out.println("line = " + line);writer.write(line);writer.newLine();writer.close();} catch (IOException e) {e.printStackTrace();}}/**** 截取指定字段**/public static String subString(String str, String strStart, String strEnd) {/* 找出指定的2个字符在 该字符串里面的 位置 */int strStartIndex = str.indexOf(strStart) + 3;int strEndIndex = str.indexOf(strEnd);/* index为负数 即表示该字符串中没有该字符 *//* 开始截取 */String result = str.substring(strStartIndex, strEndIndex);return result;}}结果:http://qnvod.renrenjiang.cn/fragments/z1.rrjqnlive.2217905015/1629979121023-1629979127852.ts

(0)

相关推荐