JavaSEDemo28核心库与IO流

本文详细介绍了Java中的StringBuffer类,包括其构造方法、capacity()、length()方法以及append()和appendCodePoint()的使用。通过示例展示了如何向字符串缓冲区添加数据,并获取指定索引的字符和codepoint。此外,文章还涉及了UUID生成订单编号、Math类的数学运算、BigInteger和BigDecimal的算术操作、Random的随机数生成、日期和时间处理、资源国际化以及文件操作等Java核心概念。

简介

  • 本文是2021/04/26整理的笔记
  • 赘述可能有点多,还请各位朋友耐心阅读
  • 本人的内容和答案不一定是最好最正确的,欢迎各位朋友评论区指正改进

StringBuffer 字符串缓冲

无参构造 初始容量是16

有参构造(str) 初始容量是16+str的长度

capacity( ) 能装多少个字符

length( ) 实际装了多少个字符

    @Test
    public void test1(){
    //新创建一个StringBuffer
        StringBuffer buffer = new StringBuffer();
        //输出容量
        System.out.println("容量:"+buffer.capacity());
        //输出长度
        System.out.println("长度:"+buffer.length());
    }

append() 向字符串缓冲区放数据

  • 可以放boolean char char[ ] offset length double float int long object StringBuffer

appendCodePoint( ) 向字符串缓冲区放code point

charAt(index) 返回指定索引处的字符

  @Test
    public void testCharAt(){
        StringBuffer buffer = new StringBuffer("Hello");
        System.out.println("buffer.charAt(1) = " + buffer.charAt(1));
    }

charAtCodePoint() 返回指定索引处的的code point

    @Test
    public void testCharAtCodePoint(){
        StringBuffer buffer = new StringBuffer("hello");
        System.out.println("buffer.codePointAt(1) = " + buffer.codePointAt(1));
        System.out.println("buffer.codePointAt(1) = " + buffer.codePointBefore(1));
    }

课堂案例

使用uuid生成订单编号,要求如下:

  • 订单编号32位
  • 如果有字母,需要将字母全大写
  • 不要有-
    思路:
  1. uuid=UUID.randomUUID().toString()生成一个36位的字符串
  2. uuid.replaceAll("-"," ")
  3. uuid = uuid.toUpperCase()
    @Test
    public void testDemo(){
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid.replace("-", "").toUpperCase());
    }

Math类

  • ceil
  • floor
  • round
  • random

BigInteger

  • 构造方法 BigInteger(String str)
  • add(BigInteger)
  • subtract(BigInteger)
  • multiply(BigInteger)
  • divide(BigInteger)

BigDecimal

  • 构造方法 BigDecimal(String str)
  • add(BigDecimal)
  • subtract(BigDecimal)
  • multiply(BigDecimal)
  • divide(BigDecimal)
  • setCale(小数点后的位数,是否四舍五入)

Random

  • 构造方法 Random() Random(n)
  • nextInt() 返回随机整数
  • nextInt(bound) 返回0-bound-1间的随机整数 long double同理

UUID

  • UUID.randomUUID.toString() 生成36位带-的随机字符串
  • UUID.nameUUIDFromBytes(str.getBytes()) 根据字符串的字节数组生成uuid
  • 订单id:UUID.randomUUID().toString.replaceAll("-","");toUpperCase()

Date

  • Date() 得到当前的时间
  • Date(long)得到long对应的时间

Calendar

  • Calendar.getInstance();得到Calendar的实例对象
  • 设值:set(field,值) set(y,M,d,H,m,s)
  • 取值:get(field) Date date = instance.getTime()
  • 修改:add(field,val)

SimpleDateFormat

  • new SimpleDateFormat(pattern); yyyy-MM-dd HH:mm:ss SSS
  • format(new Date()) --> String
  • parse(String) --> Date

资源国际化

  1. 在src下创建资源文件 baseName_语言编码_国家编码.properties message_zh_CN.properties message_en_US.properties
  2. 创建Locale对象 Locale locale = new Locale(“语言编码”,“国家编码”)
  3. 通过ResourceBundle.getBundle()得到bundle对象
 //得到ResourceBundle对象
        ResourceBundle bundle1 = ResourceBundle.getBundle("message",locale1);
        ResourceBundle bundle2 = ResourceBundle.getBundle("message",locale2);
  1. bundle.getString(key)得到值
System.out.println(MessageFormat.format(bundle1.getString("order.msg"),"张三",200));
System.out.println(MessageFormat.format(bundle2.getString("order.msg"),"Tom",300));
  1. 格式化
    MessageFormat.format(pattern,…可变参数)
    pattern:你好,{0},你要付{1,number,currency}
    MessageFormat.format(pattern,“张三”,120)

文件

  1. 文件可以认为是相关记录或存放在一起的数据的集合
  2. 文件能代表文件或者目录
  3. 创建文件 New File(String)
@Test
    public void test() throws IOException {
        File file = new File("test.txt");
        //是否存在
        System.out.println("file.exists() = " + file.exists());
        //创建新的空文件
        System.out.println(file.createNewFile());
        System.out.println("file.exists() = " + file.exists());
        //是否是文件
        System.out.println(file.isFile());
        //是否是目录
        System.out.println(file.isDirectory());
        //返回文件的名称
        System.out.println(file.getName());
        //返回绝对路径
        System.out.println(file.getAbsolutePath());
        //返回文件的长度
        System.out.println(file.length());
        //删除文件
        System.out.println(file.delete());
    }
  1. 列出文件列表
    @Test
    public void testFileList(){
        File file = new File("D:\\AJavaStudyTest\\javaSE\\src\\GC\\demoM");
        System.out.println("file.isDirectory() = " + file.isDirectory());
        File[] files = file.listFiles();
        for (File file1 : files) {
            System.out.println("file1 = " + file1);
        }
    }
  1. 过滤文件
    @Test
    public void testFilenameFilter(){
        File file = new File("D:\\JVM");
        File[] files = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.startsWith(".png");
            }
        });
        for (File file1 : files) {
            System.out.println("file1 = " + file1);
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香鱼嫩虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值