zoukankan      html  css  js  c++  java
  • 读取HTML文件并替换其中的特定内容,然后保存文件

    package com.albertyy.other;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    
    public class RemoveId {
    	public static void main(String[] args) {
    		changeContent("F://tag.html", "tag.html");
    	}
    
    	/**
    	 * 读取HTML文件并替换其中的特定内容,然后保存文件
    	 * @param filePath读取文件路径
    	 * @param savePath保存文件路径
    	 */
    	private static void changeContent(String filePath, String savePath) {
    		// filePath 要读取的文件 savePath 要写入的文件
    		BufferedReader br = null;
    		BufferedWriter bw = null;
    		
    		try {
    			// 以下读取和写入都转成UTF-8 防止乱码
    			br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
    			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savePath), "UTF-8"));
    			String line = null;
    			while ((line = br.readLine()) != null && (line != "")) {
    				bw.write(line.replaceAll("id="\S+"", ""));
    			}
    			bw.flush();
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (br != null){
    					br.close();
    				}
    				if (bw != null){
    					bw.close();
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
  • 相关阅读:
    MySQL-索引
    MySQL-存储引擎
    MySQL-基本概念
    Elasticsearch-分片原理2
    Elasticsearch-分片原理1
    [NOIP模拟33]反思+题解
    [NOIP模拟测试32]反思+题解
    [NOIP模拟测试31]题解
    [jzoj5840]Miner 题解(欧拉路)
    [NOIP模拟测试30]题解
  • 原文地址:https://www.cnblogs.com/yangxianyang/p/13675575.html
Copyright © 2011-2022 走看看