java 格式判断

public class FormatChecker {
    
    /**
     * 判断是否含有汉字
     * @param string
     */
    public static boolean containChinese(String string){
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        return pattern.matcher(string).find();
    }
    
    /**
     * 判断是否含有空格
     */
    public static boolean containBlank(String string){
        Pattern pattern = Pattern.compile("[\\s]");
        return pattern.matcher(string).find(); 
    }
    
    /**
     * 判断是否为电话号码
     */
    public static boolean isPhone(String string){
        Pattern pattern = Pattern.compile("1[3,4,5,7,8]\\d{9}");
        return pattern.matcher(string).matches();
    }
    
    /**
     * 判断是否为手机号码
     */
    public static boolean isMobile(String string){
        Pattern pattern = Pattern.compile("1[3,4,5,7,8]\\d{9}");
        return pattern.matcher(string).matches();
    }
    
    /**
     * 判断是否为固定电话
     */
    public static boolean isTelephone(String string){
        Pattern pattern = Pattern.compile("(^\\+86\\.\\d{3,5}\\d{6,8}$)|(^((0\\d{2,3})-?)(\\d{7,8})(-(\\d{3,}))?$)");
        return pattern.matcher(string).matches();
    }
    
    /**
     * 判断是否为邮箱
     */
    public static boolean isEmail(String string){
        Pattern pattern = Pattern.compile("[&~#$*%\\u4e00-\\u9fa5_0-9a-z\\-\\.\\/\\\\]+@([\\u4e00-\\u9fa5-a-z0-9]+\\.){1,5}[\\u4e00-\\u9fa5a-z]+", Pattern.CASE_INSENSITIVE);
        return pattern.matcher(string).matches();
    }
    
    /**
     * 判断是否为链接地址
     */
    public static boolean isUrl(String string){
        Pattern pattern = Pattern.compile("((http|https):\\/\\/([\\w\\-]+\\.)+[\\w\\-]+(\\/[\\w\\u4e00-\\u9fa5\\-\\.\\/?\\@\\%\\!\\&=\\+\\~\\:\\#\\;\\,]*)?)", Pattern.CASE_INSENSITIVE );
        return pattern.matcher(string).matches();
    }
    
    /**
     * 判断是否为域名
     */
    public static boolean isDomain(String string){
        Pattern pattern = Pattern.compile("^([\\x{4e00}-\\x{9fa5}-a-z0-9]+\\.){1,5}[\\x{4e00}-\\x{9fa5}a-z]+$", Pattern.CASE_INSENSITIVE );
        return pattern.matcher(string).matches();
    }
    /**
     * 判断是否为数字
     */
    public static boolean isNumber(String string){
        Pattern pattern = Pattern.compile("^[1-9]\\d*$", Pattern.CASE_INSENSITIVE );
        return "0".equals(string) || pattern.matcher(string).matches();
    }
}

 

转载于:https://www.cnblogs.com/rubekid/p/4869029.html

### 如何在Java判断文件格式 为了确定文件的格式,在Java中有多种方法可以实现这一功能。一种常见的法是通过读取文件头(即文件签名),因为许多类型的文件在其开头部分存储有特定字节序列来标识其类型[^1]。 下面是一个简单的例子,展示如何基于文件头部信息识别一些常见图像文件格式: ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; public class FileFormatDetector { public static String detectImageFileType(Path filePath) throws IOException { byte[] headerBytes = Files.readAllBytes(filePath); if (headerBytes.length >= 4) { // Ensure enough bytes to check signatures // Check JPEG signature if ((headerBytes[0] & 0xFF) == 0xFF && (headerBytes[1] & 0xFF) == 0xD8 && (headerBytes[2] & 0xFF) == 0xFF) { return "JPEG"; } else if (headerBytes[0] == 'G' && headerBytes[1] == 'I' && headerBytes[2] == 'F' && headerBytes[3] == '8') { // Check GIF signature return "GIF"; } else if (headerBytes[0] == 0x89 && headerBytes[1] == 'P' && headerBytes[2] == 'N' && headerBytes[3] == 'G') { // Check PNG signature return "PNG"; } } return "Unknown"; } } ``` 此代码片段展示了如何检测几种流行的图片格式——JPEG、GIF 和 PNG 的文件头,并返回相应的字符串表示形式。当然,这种方法也可以扩展到其他类型的文件上,只需知道它们各自的魔数即可。 对于更复杂的场景或更多种类的文件支持,则可能需要借助第三方库的帮助,比如 Apache Tika 或者 JMimeMagic 等工具包,这些库提供了更为全面的功能用于自动探测各种不同类型的文档、音频视频等资源的真实MIME类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值