java工具类方法记录

来源:互联网 发布:java解析http请求 编辑:程序博客网 时间:2024/06/05 20:31

  • java依赖包一起打进去
  • txt文件最后一行文件内容覆写并追加
  • 字符串中获取手机号

java依赖包一起打进去

使用IDE:IntelJ IDEA

<build>        <plugins>            <plugin>                <artifactId>maven-assembly-plugin</artifactId>                <configuration>                    <archive>                        <manifest>                            <mainClass>com.allen.capturewebdata.Main</mainClass>                        </manifest>                    </archive>                    <descriptorRefs>                        <descriptorRef>jar-with-dependencies</descriptorRef>                    </descriptorRefs>                </configuration>            </plugin>        </plugins>    </build>

要将依赖包一起打包的话,需要使用maven-assembly-plugin插件。需要注意的是,使用此插件进行打包时,不再是使用mvn package 命令,而是使用mvn assembly:assembly命令。执行成功后会在target文件夹下多出一个以-jar-with-dependencies结尾的jar包。这个jar包中就包含了当前项目的所有依赖包,可参考这里

txt文件最后一行文件内容覆写并追加

  public static void rewriteendline(String filepath, String string)            throws Exception {        RandomAccessFile file = new RandomAccessFile(filepath, "rw");        long len = file.length();        long start = file.getFilePointer();        long nextend = start + len - 1;        int i = -1;        file.seek(nextend);        byte[] buf = new byte[1];        while (nextend > start) {            i = file.read(buf, 0, 1);            if (buf[0] == '\r') {                file.setLength(nextend - start);                break;            }            nextend--;            file.seek(nextend);        }        file.close();        writeendline(filepath, string);    }    public static void writeendline(String filepath, String string)            throws Exception {        RandomAccessFile file = new RandomAccessFile(filepath, "rw");        long len = file.length();        long start = file.getFilePointer();        long nextend = start + len - 1;        byte[] buf = new byte[1];        file.seek(nextend);        file.read(buf, 0, 1);        if (buf[0] == '\n')            file.writeBytes(string);        else            file.writeBytes("\r\n" + string);        file.close();    }

字符串中获取手机号

 /**     * 只取字符串中11位的数字是否为手机号,如果有多个,逗号隔开     * @param text     * @return     */    public static String getPhone11(String text){        Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[358]\\d{9})|(?:861[358]\\d{9}))(?!\\d)");        Matcher matcher = pattern.matcher(text);        StringBuffer bf = new StringBuffer(64);        while (matcher.find()) {            bf.append(matcher.group()).append(",");        }        int len = bf.length();        if (len > 0) {            bf.deleteCharAt(len - 1);        }        return bf.toString();    }    /**     * 查询符合的手机号码,包括从12位数字中截取手机号     * @param str     */    public static void checkCellphone(String str){        // 将给定的正则表达式编译到模式中        Pattern pattern = Pattern.compile("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}");        // 创建匹配给定输入与此模式的匹配器。        Matcher matcher = pattern.matcher(str);        //查找字符串中是否有符合的子字符串        while(matcher.find()){            //查找到符合的即输出            System.out.println("查询到一个符合的手机号码:"+matcher.group());        }    }/**同上**/    private static String checkNum(String num){        if(num == null || num.length() == 0){return "";}        Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[358]\\d{9})|(?:861[358]\\d{9}))(?!\\d)");        Matcher matcher = pattern.matcher(num);        StringBuffer bf = new StringBuffer(64);        while (matcher.find()) {            bf.append(matcher.group()).append(",");        }        int len = bf.length();        if (len > 0) {            bf.deleteCharAt(len - 1);        }        return bf.toString();    }    private static boolean isNumLegal(String str) throws PatternSyntaxException {        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";        Pattern p = Pattern.compile(regExp);        Matcher m = p.matcher(str);        return m.matches();    }
原创粉丝点击