zoukankan      html  css  js  c++  java
  • Java知识点梳理——常用方法总结

    1、查找字符串最后一次出现的位置

    String str = "my name is zzw";
    int lastIndex = str.lastIndexOf("zzw");
    if (lastIndex == -1) {
        System.out.println("zzw 404");
    } else {
        System.out.println(lastIndex);
    }
    字符串查找

    2、字符串分割

    // 第一种方法 split
    String str = "my name is zzw";
    String[] strs = str.split(" ");
    for (String s : strs) {
        System.out.println(s);
    }
    // 第二种方法 StringTokenizer可以设置不同分隔符来分隔字符串,
    // 默认的分隔符是:空格、制表符(	)、换行符(
    )、回车符(
    )
    String str2 = "hello everyone, my name is zzw";
    StringTokenizer st = new StringTokenizer(str2);
    while (st.hasMoreElements()) {
        System.out.println(st.nextElement());
    }
    StringTokenizer st2 = new StringTokenizer(str2, ",");
    while (st2.hasMoreElements()) {
        System.out.println(st2.nextElement());
    }
    字符串分割

    3、字符串大小写转换

    String str = "zzw";
    System.out.println(str.toUpperCase()); // 转大写
    System.out.println(str.toLowerCase()); // 转小写
    字符串转大小写

    4、数组元素查找

    int[] array = { 1, 6, 7, 5, 4, -6, 3, 9 };
    int index = Arrays.binarySearch(array, 5);
    System.out.println(index);
    数组元素查找

    5、获取当前年、月、日等

    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DATE);
    int doy = cal.get(Calendar.DAY_OF_YEAR);
    int dom = cal.get(Calendar.DAY_OF_MONTH);
    int dow = cal.get(Calendar.DAY_OF_WEEK);
    Date date = cal.getTime();
    System.out.println("当前时间:" + date + "," + year + "年" + month + "月" + day + "日" 
            + ",一年的第" + doy + "天,一月的第" + dom + "天,一周的第" + dow + "天");
    Calendar时间处理

    6、时间戳转换时间

    /*yyyy:年
        MM:月
        dd:日
        hh:1~12小时制(1-12)
        HH:24小时制(0-23)
        mm:分
        ss:秒
        S:毫秒
        E:星期几
        D:一年中的第几天
        F:一月中的第几个星期(会把这个月总共过的天数除以7)
        w:一年中的第几个星期
        W:一月中的第几星期(会根据实际情况来算)
        a:上下午标识
        k:和HH差不多,表示一天24小时制(1-24)
        K:和hh差不多,表示一天12小时制(0-11)
        z:表示时区
    */
    Long timeStamp = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E D F");
    String sTime = sdf.format(new Date(timeStamp));
    System.out.println(sTime); // 输出格式:2018-08-21 14:29:19 星期二 233 3
    时间戳

    7、九九乘法表

    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= i; j++) {
            System.out.print(j + "×" + i + "=" + i * j + "	");
        }
        System.out.println();
    }
    九九乘法表

    8、文件操作

    BufferedWriter out = new BufferedWriter(new FileWriter("d:\zzw.txt"));
    out.write("hello world");
    out.close();
    System.out.println("文件写成功");
    BufferedReader in = new BufferedReader(new FileReader("d:\zzw.txt"));
    String str;
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
    System.out.println("文件读成功");
    File file = new File("d:\zzw.log");
    if (file.createNewFile()) { // 创建文件
        System.out.println("文件创建成功");
    } else {
        System.out.println("文件创建失败");
    }
    if (file.exists()) { // 检测文件是否存在
        if (file.delete()) { // 删除文件
            System.out.println("文件删除成功");
        } else {
            System.out.println("文件删除失败");
        }
    } else {
        System.out.println("文件不存在");
    }    
    文件操作

    9、目录操作

    String directories = "d:\a\b\c\d\e\f\g\h\i";
    File file = new File(directories);
    boolean b = file.mkdirs(); // 创建目录
    if (b) {
        System.out.println("目录创建成功");
    } else {
        System.out.println("目录创建失败");
    }
    File f = new File("d:\a");
    if (f.isDirectory()) { // 检测是否为目录
        if (file.list().length > 0) { // 检测目录是否为空
            System.out.println("目录不为空");
        } else {
            System.out.println("目录为空");
        }
    } else {
        System.out.println("不是目录");
    }
    long size = FileUtils.sizeOfDirectory(f); // 检测目录大小
    System.out.println("目录大小:" + size);
    String parentDir = f.getParent(); // 获取上级目录
    System.out.println("上级目录:" + parentDir);
    String curDir = System.getProperty("user.dir"); // 获取当前工作目录
    System.out.println("当前目录:" + curDir);
    Date lastModified = new Date(f.lastModified()); // 获取目录最后修改时间
    System.out.println("目录最后修改时间:" + lastModified);
    目录操作

    10、递归遍历目录

    public static void main(String[] args) {
        File dir = new File("d:\a"); // a目录结构 a/b/c/d/e/f/g/h/i
        getAllDirsAndFiles(dir);
        /*
         * 输出结果 
         * d:a 
         * d:a 
         * d:ac 
         * d:acd 
         * d:acde 
         * d:acdef
         * d:acdefg 
         * d:acdefgh 
         * d:acdefghi
         */
    
    }
    
    // 递归遍历目录
    public static void getAllDirsAndFiles(File dir) {
        System.out.println(dir);
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                getAllDirsAndFiles(new File(dir, children[i]));
            }
        }
    }
    遍历目录

    11、递归删除目录

    public static void main(String[] args) throws IOException {
        File f = new File("d:\a"); // a目录结构 a/b/c/d/e/f/g/h/i
        deleteDir(f); // 递归方法
        /*
         * 输出结果 
         * i目录已被删除! 
         * h目录已被删除! 
         * g目录已被删除! 
         * f目录已被删除! 
         * e目录已被删除! 
         * d目录已被删除! 
         * c目录已被删除!
         * b目录已被删除! 
         * a目录已被删除!
         */
    }
    
    // 递归删除目录——先删除文件后删除目录
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        if (dir.delete()) {
            System.out.println(dir.getName() + "目录已被删除!");
            return true;
        } else {
            System.out.println(dir.getName() + "目录删除失败!");
            return false;
        }
    }
    删除目录

    12、数组、集合相互转换

    // 数组转集合
    String[] strs = { "zzw", "qq", "weixin" };
    List<String> lst = Arrays.asList(strs);
    for (String str : lst) {
        System.out.println(str);
    }
    // 集合转数组
    String[] sArrays = lst.toArray(new String[0]);
    for(String s : sArrays){
        System.out.println(s);
    }
    数组集合转换

    13、List集合

    List list = Arrays.asList("one Two three Four five six Two".split(" "));
    System.out.println("最大值: " + Collections.max(list)); // 获取集合最大值
    System.out.println("最小值: " + Collections.min(list)); // 获取集合最小值
    System.out.println("List: " + list);
    Collections.rotate(list, 3); // 循环移动元素 参数3代表移动的起始位置
    System.out.println("rotate: " + list); // 输出结果 [five, six, Two, one, Two, three, Four]
    List subList = Arrays.asList(new String[] { "Two" });
    int index = Collections.indexOfSubList(list, subList); // 检测子列表是否在列表中,返回子列表所在位置
    System.out.println(index);
    int lastIndex = Collections.lastIndexOfSubList(list, subList);
    System.out.println(lastIndex);    
    集合

    14、网页抓取

    URL url = new URL("https://www.baidu.com");
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    BufferedWriter writer = new BufferedWriter(new FileWriter("data.html"));
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
        writer.write(line);
        writer.newLine();
    }
    reader.close();
    writer.close();    
    网页抓取

     15、获取远程文件大小

    URL url = new URL("http://wx.qlogo.cn/mmopen/SPia0Eklic7mk65iaCc4LXiciaKpQFoI2gk6cwKKqPj7cSjSrmribre4B17DzVBQib3CVfYvKW6xc9DyDBUBScECnahZrYZa7wptTDJ/0"); // 微信头像
    URLConnection conn = url.openConnection();
    int size = conn.getContentLength();
    if (size < 0) {
        System.out.println("无法获取文件大小。");
    } else {
        System.out.println("文件大小为:" + size + " bytes");
    }
    conn.getInputStream().close();
    获取远程文件
  • 相关阅读:
    JavaScript之DOM查询
    JavaScript之this解析
    Qt之pro文件解析
    Qt5 调试之详细日志文件输出(qInstallMessageHandler)
    修改 Ubuntu的源为阿里源
    Unable to acquire the dpkg frontend lock
    gcc编译中文字符串后,windows控制台输出乱码
    stm32f103 time2配置,转载
    取反
    单片机,struct ,union定义标志,节约RAM
  • 原文地址:https://www.cnblogs.com/lbxx/p/9512228.html
Copyright © 2011-2022 走看看