文章目录
简介
- 本文是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位
- 如果有字母,需要将字母全大写
- 不要有-
思路:
- uuid=UUID.randomUUID().toString()生成一个36位的字符串
- uuid.replaceAll("-"," ")
- 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
资源国际化
- 在src下创建资源文件 baseName_语言编码_国家编码.properties message_zh_CN.properties message_en_US.properties
- 创建Locale对象 Locale locale = new Locale(“语言编码”,“国家编码”)
- 通过ResourceBundle.getBundle()得到bundle对象
//得到ResourceBundle对象
ResourceBundle bundle1 = ResourceBundle.getBundle("message",locale1);
ResourceBundle bundle2 = ResourceBundle.getBundle("message",locale2);
- bundle.getString(key)得到值
System.out.println(MessageFormat.format(bundle1.getString("order.msg"),"张三",200));
System.out.println(MessageFormat.format(bundle2.getString("order.msg"),"Tom",300));
- 格式化
MessageFormat.format(pattern,…可变参数)
pattern:你好,{0},你要付{1,number,currency}
MessageFormat.format(pattern,“张三”,120)
文件
- 文件可以认为是相关记录或存放在一起的数据的集合
- 文件能代表文件或者目录
- 创建文件 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());
}
- 列出文件列表
@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);
}
}
- 过滤文件
@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);
}
}
本文详细介绍了Java中的StringBuffer类,包括其构造方法、capacity()、length()方法以及append()和appendCodePoint()的使用。通过示例展示了如何向字符串缓冲区添加数据,并获取指定索引的字符和codepoint。此外,文章还涉及了UUID生成订单编号、Math类的数学运算、BigInteger和BigDecimal的算术操作、Random的随机数生成、日期和时间处理、资源国际化以及文件操作等Java核心概念。

被折叠的 条评论
为什么被折叠?



