截取标签中的内容

本文介绍了一个Java程序,该程序从特定路径的文件中读取内容,并使用字符串操作来查找并打印出所有<uuid>标签之间的文本。通过使用BufferedReader和InputStreamReader,程序能够处理大型文件,避免一次性加载整个文件到内存中。此外,通过indexOf方法和substring方法,程序准确地定位和提取了所需的数据。
public class countwords {
  public static void main(String[] args) {
    String con="";
    List<Integer> list=new ArrayList<>();
    List<Integer> list1=new ArrayList<>();
    try {
      BufferedReader in=new BufferedReader(new InputStreamReader
       (new FileInputStream("F://3d50fbd8-2374-4cd7-a021-b29c32e4117f.txt"),"utf-8"));
      String str=null;
      while ((str=in.readLine())!=null){
        con=con+str;
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    String key = "<uuid>";
    int startindex = 0;
    while ((startindex = con.indexOf(key, startindex)) >= 0) {
      list.add(startindex);
      startindex += key.length();
    }
    String key2="</uuid>";
    int endindex = 0;
    while ((endindex = con.indexOf(key2, endindex)) >= 0) {
      list1.add(endindex);
      endindex += key2.length();
    }

    for (int i = 0; i <list.size() ; i++) {
      String getwords=con.substring(list.get(i)+8,list1.get(i));
      System.out.println(getwords);
    }
  }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值