发新帖

[Java] Java 去除字符串HTML、替换问号、读取文件

零下一度 2018-6-4 1404
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
  

public class DealWithStr {
    
     //去除html 标签
     public static String deleteAllHTMLTag(String source) {

          if(source == null) {
               return "";
          }

          String s = source;
          /** 删除普通标签  */
          s = s.replaceAll("<(S*?)[^>]*>.*?|<.*? />", "");
          /** 删除转义字符 */
          s = s.replaceAll("&.{2,6}?;", "");
          return s;
    }  
    
    //替换所有问号 
    public static String deleteQuesTag(String source){
        
        if(source == null) {
               return "";
          }

          String s = source;  
          s = s.replaceAll("[?]", "");  
          return s;
    } 
      
    //读取文件
    public static String readToString(String fileName) {  
        String encoding = "UTF-8";  
        File file = new File(fileName);  
        Long filelength = file.length();  
        byte[] filecontent = new byte[filelength.intValue()];  
        try {  
            FileInputStream in = new FileInputStream(file);  
            in.read(filecontent);  
            in.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        try {  
            return new String(filecontent, encoding);  
        } catch (UnsupportedEncodingException e) {  
            System.err.println("The OS does not support " + encoding);  
            e.printStackTrace();  
            return null;  
        }  
    }  
     
}


最新回复 (0)
返回
零下一度
主题数
931
帖子数
0
注册排名
1