本文主要介绍Java常用类的方法及用法。
Java常用类及方法总结
1. String
类
用于表示不可变的字符串。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
length() | 获取字符串长度 | 无 | int |
charAt(int index) | 获取指定索引处的字符 | 索引位置 | char |
substring(int beginIndex, int endIndex) | 获取从开始索引到结束索引的子字符串(不包括结束索引) | 开始索引、结束索引 | String |
indexOf(int ch) | 返回指定字符在字符串中首次出现的索引 | 要查找的字符 | int |
lastIndexOf(int ch) | 返回指定字符在字符串中最后一次出现的索引 | 要查找的字符 | int |
startsWith(String prefix) | 检查字符串是否以指定前缀开头 | 前缀字符串 | boolean |
endsWith(String suffix) | 检查字符串是否以指定后缀结尾 | 后缀字符串 | boolean |
contains(CharSequence s) | 检查字符串是否包含指定的字符序列 | 要检查的字符序列 | boolean |
replace(CharSequence target, CharSequence replacement) | 将字符串中的指定字符序列替换为新的字符序列 | 要替换的目标序列、替换后的序列 | String |
toLowerCase() | 将字符串转换为小写 | 无 | String |
toUpperCase() | 将字符串转换为大写 | 无 | String |
trim() | 去除字符串两端的空白字符 | 无 | String |
split(String regex) | 根据给定的正则表达式拆分字符串 | 正则表达式 | String[] |
示例代码
1 2 3 4 5 6 7
| String str = "Hello, World!"; System.out.println("长度: " + str.length()); System.out.println("第5个字符: " + str.charAt(4)); System.out.println("子字符串: " + str.substring(7, 12)); System.out.println("索引: " + str.indexOf('W')); System.out.println("是否以Hello开头: " + str.startsWith("Hello")); System.out.println("替换后: " + str.replace("World", "Java"));
|
2. StringBuilder
类
用于可变字符串的操作,效率较高。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
append(String str) | 在字符串末尾追加指定字符串 | 要追加的字符串 | StringBuilder |
insert(int offset, String str) | 在指定位置插入字符串 | 插入位置、要插入的字符串 | StringBuilder |
delete(int start, int end) | 删除指定范围内的字符 | 起始索引、结束索引 | StringBuilder |
reverse() | 反转字符串 | 无 | StringBuilder |
toString() | 将StringBuilder 转换为String | 无 | String |
示例代码
1 2 3 4 5 6
| StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); sb.insert(5, ","); sb.delete(5, 6); sb.reverse(); System.out.println(sb.toString());
|
3. ArrayList
类
动态数组,允许随机访问元素。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
add(E e) | 添加元素到列表末尾 | 要添加的元素 | boolean |
add(int index, E e) | 在指定位置插入元素 | 插入位置、要添加的元素 | void |
remove(int index) | 移除指定位置的元素 | 要移除的元素索引 | E |
get(int index) | 获取指定位置的元素 | 元素索引 | E |
set(int index, E e) | 替换指定位置的元素 | 元素索引、新元素 | E |
size() | 获取列表中的元素个数 | 无 | int |
contains(Object o) | 检查列表是否包含指定元素 | 要检查的元素 | boolean |
indexOf(Object o) | 返回指定元素首次出现的索引 | 要查找的元素 | int |
lastIndexOf(Object o) | 返回指定元素最后一次出现的索引 | 要查找的元素 | int |
clear() | 清空列表中的所有元素 | 无 | void |
示例代码
1 2 3 4 5 6 7 8 9 10
| ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Orange"); System.out.println("元素个数: " + list.size()); System.out.println("第二个元素: " + list.get(1)); list.set(1, "Grape"); System.out.println("修改后的列表: " + list); list.remove(2); System.out.println("移除后的列表: " + list);
|
4. HashMap
类
基于哈希表的映射,允许null
键和null
值。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
put(K key, V value) | 将键值对插入映射 | 键、值 | V |
get(Object key) | 获取指定键对应的值 | 键 | V |
remove(Object key) | 移除指定键及其对应的值 | 键 | V |
containsKey(Object key) | 检查映射是否包含指定键 | 键 | boolean |
containsValue(Object value) | 检查映射是否包含指定值 | 值 | boolean |
size() | 获取映射中的键值对个数 | 无 | int |
isEmpty() | 检查映射是否为空 | 无 | boolean |
clear() | 清空映射中的所有键值对 | 无 | void |
keySet() | 返回映射中所有键的集合视图 | 无 | Set<K> |
values() | 返回映射中所有值的集合视图 | 无 | Collection<V> |
示例代码
1 2 3 4 5 6 7 8 9
| HashMap<String, Integer> map = new HashMap<>(); map.put("Apple", 1); map.put("Banana", 2); map.put("Orange", 3); System.out.println("Apple的值: " + map.get("Apple")); System.out.println("映射大小: " + map.size()); map.remove("Banana"); System.out.println("移除后映射: " + map); System.out.println("是否包含Orange键: " + map.containsKey("Orange"));
|
5. Math
类
提供基本的数学函数。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
abs(double a) | 返回参数的绝对值 | 要计算绝对值的数 | double |
sqrt(double a) | 返回参数的平方根 | 要计算平方根的数 | double |
pow(double a, double b) | 返回a的b次幂 | 底数、指数 | double |
max(double a, double b) | 返回两个数中的较大值 | 两个数 | double |
min(double a, double b) | 返回两个数中的较小值 | 两个数 | double |
round(double a) | 返回最接近参数的整数 | 要四舍五入的数 | long |
random() | 返回一个介于0.0(包括)和1.0(不包括)之间的随机数 | 无 | double |
sin(double a) | 返回参数的正弦值 | 角度(弧度) | double |
cos(double a) | 返回参数的余弦值 | 角度(弧度) | double |
tan(double a) | 返回参数的正切值 | 角度(弧度) | double |
示例代码
1 2 3 4 5
| System.out.println("绝对值: " + Math.abs(-5.5)); System.out.println("平方根: " + Math.sqrt(25)); System.out.println("3的4次幂: " + Math.pow(3, 4)); System.out.println("最大值: " + Math.max(10, 20)); System.out.println("随机数: " + Math.random());
|
6. Date
和 Calendar
类
用于处理日期和时间。
Date
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
getTime() | 返回自1970年1月1日00:00:00 GMT以来的毫秒数 | 无 | long |
toString() | 返回日期时间的字符串表示 | 无 | String |
Calendar
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
getInstance() | 获取默认时区和语言环境的Calendar 对象 | 无 | Calendar |
get(int field) | 获取指定日历字段的值 | 日历字段(如YEAR 、MONTH 等) | int |
set(int field, int value) | 设置指定日历字段的值 | 日历字段、新值 | void |
add(int field, int amount) | 给指定日历字段添加或减去指定的时间量 | 日历字段、时间量 | void |
getTime() | 将Calendar 转换为Date 对象 | 无 | Date |
示例代码
1 2 3 4 5 6 7 8 9 10
| Date date = new Date(); System.out.println("当前日期时间: " + date.toString()); long millis = date.getTime(); System.out.println("自1970年1月1日以来的毫秒数: " + millis);
Calendar calendar = Calendar.getInstance(); System.out.println("当前年份: " + calendar.get(Calendar.YEAR)); calendar.add(Calendar.DAY_OF_MONTH, 5); Date newDate = calendar.getTime(); System.out.println("新日期: " + newDate.toString());
|
7. File
类
用于操作文件和目录。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
exists() | 检查文件或目录是否存在 | 无 | boolean |
isFile() | 检查是否为文件 | 无 | boolean |
isDirectory() | 检查是否为目录 | 无 | boolean |
length() | 获取文件的大小(字节) | 无 | long |
getName() | 获取文件或目录的名称 | 无 | String |
getParent() | 获取父目录的路径名 | 无 | String |
getPath() | 获取文件或目录的路径名 | 无 | String |
getAbsolutePath() | 获取绝对路径名 | 无 | String |
list() | 返回目录中的文件和目录名数组 | 无 | String[] |
listFiles() | 返回目录中的文件和目录对象数组 | 无 | File[] |
mkdir() | 创建目录 | 无 | boolean |
mkdirs() | 创建多层目录 | 无 | boolean |
delete() | 删除文件或目录 | 无 | boolean |
renameTo(File dest) | 重命名文件或目录 | 新的文件或目录对象 | boolean |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| File file = new File("example.txt"); if (!file.exists()) { try { file.createNewFile(); System.out.println("文件创建成功"); } catch (IOException e) { e.printStackTrace(); } } System.out.println("文件大小: " + file.length() + "字节"); File dir = new File("testDir"); if (!dir.exists()) { dir.mkdirs(); System.out.println("目录创建成功"); } File[] files = dir.listFiles(); if (files != null) { for (File f : files) { System.out.println("目录中的文件: " + f.getName()); } }
|
8. Scanner
类
用于获取用户输入。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
next() | 获取下一个输入的字符串(以空白符分隔) | 无 | String |
nextLine() | 获取整行输入 | 无 | String |
nextInt() | 获取下一个输入的整数 | 无 | int |
nextDouble() | 获取下一个输入的双精度浮点数 | 无 | double |
hasNext() | 检查是否有更多输入可用 | 无 | boolean |
close() | 关闭扫描器 | 无 | void |
示例代码
1 2 3 4 5 6 7
| Scanner scanner = new Scanner(System.in); System.out.print("请输入您的名字: "); String name = scanner.nextLine(); System.out.print("请输入您的年龄: "); int age = scanner.nextInt(); scanner.close(); System.out.println("名字: " + name + ", 年龄: " + age);
|
9. PrintStream
类
用于输出数据,System.out
是其对象。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
print(String s) | 输出字符串,不换行 | 要输出的字符串 | void |
println(String s) | 输出字符串,并换行 | 要输出的字符串 | void |
printf(String format, Object... args) | 格式化输出 | 格式字符串、参数列表 | PrintStream |
示例代码
1 2 3
| PrintStream ps = System.out; ps.println("Hello, World!"); ps.printf("圆周率: %.2f%n", Math.PI);
|
10. Math
类
提供基本的数学函数。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
abs(double a) | 返回参数的绝对值 | 要计算绝对值的数 | double |
sqrt(double a) | 返回参数的平方根 | 要计算平方根的数 | double |
pow(double a, double b) | 返回a的b次幂 | 底数、指数 | double |
max(double a, double b) | 返回两个数中的较大值 | 两个数 | double |
min(double a, double b) | 返回两个数中的较小值 | 两个数 | double |
round(double a) | 返回最接近参数的整数 | 要四舍五入的数 | long |
random() | 返回一个介于0.0(包括)和1.0(不包括)之间的随机数 | 无 | double |
sin(double a) | 返回参数的正弦值 | 角度(弧度) | double |
cos(double a) | 返回参数的余弦值 | 角度(弧度) | double |
tan(double a) | 返回参数的正切值 | 角度(弧度) | double |
示例代码
1 2 3 4 5
| System.out.println("绝对值: " + Math.abs(-5.5)); System.out.println("平方根: " + Math.sqrt(25)); System.out.println("3的4次幂: " + Math.pow(3, 4)); System.out.println("最大值: " + Math.max(10, 20)); System.out.println("随机数: " + Math.random());
|
11. System
类
提供系统相关的属性和方法。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
currentTimeMillis() | 返回当前时间的毫秒数 | 无 | long |
nanoTime() | 返回当前时间的纳秒数 | 无 | long |
arraycopy(Object src, int srcPos, Object dest, int destPos, int length) | 数组复制 | 源数组、源起始位置、目标数组、目标起始位置、复制长度 | void |
getProperty(String key) | 获取系统属性值 | 属性键 | String |
示例代码
1 2 3 4 5 6 7
| long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis(); System.out.println("执行时间: " + (endTime - startTime) + "ms");
String osName = System.getProperty("os.name"); System.out.println("操作系统名称: " + osName);
|
12. Integer
类
int
基本类型的包装类。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
parseInt(String s) | 将字符串转换为整数 | 要转换的字符串 | int |
toString(int i) | 将整数转换为字符串 | 要转换的整数 | String |
valueOf(String s) | 返回表示指定字符串的Integer 对象 | 要转换的字符串 | Integer |
max(int a, int b) | 返回两个整数中的较大值 | 两个整数 | int |
min(int a, int b) | 返回两个整数中的较小值 | 两个整数 | int |
bitCount(int i) | 返回整数二进制表示中1的位数 | 整数 | int |
compare(int x, int y) | 比较两个整数的大小 | 两个整数 | int |
示例代码
1 2 3 4 5 6 7 8 9
| String numStr = "123"; int num = Integer.parseInt(numStr); System.out.println("字符串转换为整数: " + num);
int maxNum = Integer.max(10, 20); System.out.println("最大值: " + maxNum);
int bitCount = Integer.bitCount(7); System.out.println("二进制中1的位数: " + bitCount);
|
13. Double
类
double
基本类型的包装类。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
parseDouble(String s) | 将字符串转换为双精度浮点数 | 要转换的字符串 | double |
toString(double d) | 将双精度浮点数转换为字符串 | 要转换的浮点数 | String |
isNaN(double v) | 检查是否为NaN (非数字) | 浮点数值 | boolean |
isInfinite(double v) | 检查是否为无穷大 | 浮点数值 | boolean |
compare(double d1, double d2) | 比较两个双精度浮点数的大小 | 两个浮点数 | int |
示例代码
1 2 3 4 5 6
| String doubleStr = "3.14"; double value = Double.parseDouble(doubleStr); System.out.println("字符串转换为浮点数: " + value);
boolean isNan = Double.isNaN(0.0 / 0.0); System.out.println("是否为NaN: " + isNan);
|
14. Character
类
char
基本类型的包装类。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
isDigit(char c) | 判断字符是否为数字 | 要判断的字符 | boolean |
isLetter(char c) | 判断字符是否为字母 | 要判断的字符 | boolean |
isUpperCase(char c) | 判断字符是否为大写字母 | 要判断的字符 | boolean |
isLowerCase(char c) | 判断字符是否为小写字母 | 要判断的字符 | boolean |
toUpperCase(char c) | 将字符转换为大写 | 要转换的字符 | char |
toLowerCase(char c) | 将字符转换为小写 | 要转换的字符 | char |
toString(char c) | 将字符转换为字符串 | 要转换的字符 | String |
示例代码
1 2 3 4
| char ch = 'A'; System.out.println("是否为字母: " + Character.isLetter(ch)); System.out.println("是否为大写: " + Character.isUpperCase(ch)); System.out.println("转换为小写: " + Character.toLowerCase(ch));
|
15. Arrays
类
提供操作数组的静态方法。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
toString(Object[] array) | 返回数组内容的字符串表示 | 数组 | String |
sort(T[] array) | 对数组进行升序排序 | 数组 | void |
binarySearch(T[] array, T key) | 在有序数组中通过二分法查找指定元素 | 数组、要查找的元素 | int |
copyOf(T[] array, int newLength) | 复制数组,新数组长度可能不同 | 数组、新长度 | T[] |
fill(Object[] array, Object val) | 用指定值填充数组 | 数组、填充值 | void |
equals(T[] a, T[] b) | 比较两个数组是否相等 | 两个数组 | boolean |
示例代码
1 2 3 4 5 6 7 8 9
| int[] arr = {3, 1, 4, 1, 5, 9}; Arrays.sort(arr); System.out.println("排序后的数组: " + Arrays.toString(arr));
int index = Arrays.binarySearch(arr, 4); System.out.println("元素4的位置: " + index);
int[] newArr = Arrays.copyOf(arr, 4); System.out.println("复制后的数组: " + Arrays.toString(newArr));
|
16. Collections
类
提供操作集合的静态方法。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
sort(List<T> list) | 对列表进行升序排序 | 列表 | void |
reverse(List<T> list) | 反转列表中元素的顺序 | 列表 | void |
shuffle(List<T> list) | 随机打乱列表中元素的顺序 | 列表 | void |
max(Collection<? extends T> coll) | 返回集合中的最大元素 | 集合 | T |
min(Collection<? extends T> coll) | 返回集合中的最小元素 | 集合 | T |
frequency(Collection<?> coll, Object o) | 返回集合中指定元素的出现次数 | 集合、元素 | int |
replaceAll(List<T> list, T oldVal, T newVal) | 将列表中的所有旧值替换为新值 | 列表、旧值、新值 | void |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| ArrayList<Integer> list = new ArrayList<>(); list.add(3); list.add(1); list.add(4); list.add(1); list.add(5); list.add(9);
Collections.sort(list); System.out.println("排序后的列表: " + list);
Collections.reverse(list); System.out.println("反转后的列表: " + list);
int max = Collections.max(list); System.out.println("最大值: " + max);
int count = Collections.frequency(list, 1); System.out.println("元素1的出现次数: " + count);
|
17. Random
类
用于生成随机数。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
nextInt() | 返回一个介于0(包括)和2^32(不包括)之间的随机整数 | 无 | int |
nextInt(int bound) | 返回一个介于0(包括)和指定值(不包括)之间的随机整数 | 上界 | int |
nextDouble() | 返回一个介于0.0(包括)和1.0(不包括)之间的随机双精度浮点数 | 无 | double |
nextBoolean() | 返回一个随机的布尔值 | 无 | boolean |
nextGaussian() | 返回一个均值为0.0,标准差为1.0的高斯分布的随机双精度浮点数 | 无 | double |
示例代码
1 2 3 4 5 6 7 8 9
| Random random = new Random(); int randInt = random.nextInt(100); System.out.println("随机整数: " + randInt);
double randDouble = random.nextDouble(); System.out.println("随机浮点数: " + randDouble);
boolean randBool = random.nextBoolean(); System.out.println("随机布尔值: " + randBool);
|
用于格式化输出,支持多种格式。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
format(String format, Object... args) | 格式化指定的参数并返回结果字符串 | 格式字符串、参数列表 | Formatter |
close() | 关闭格式化器 | 无 | void |
示例代码
1 2 3 4
| Formatter formatter = new Formatter(); formatter.format("姓名: %s, 年龄: %d, 身高: %.2f", "张三", 25, 175.6); System.out.println(formatter.toString()); formatter.close();
|
19. Pattern
和 Matcher
类
用于正则表达式操作。
Pattern
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
compile(String regex) | 编译正则表达式,返回Pattern 对象 | 正则表达式字符串 | Pattern |
matcher(CharSequence input) | 创建一个Matcher 对象,用于在输入字符序列中匹配模式 | 输入字符序列 | Matcher |
Matcher
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
find() | 查找输入序列中下一个与模式匹配的子序列 | 无 | boolean |
matches() | 判断整个输入序列是否与模式匹配 | 无 | boolean |
group() | 返回上一次匹配操作中匹配的整个子序列 | 无 | String |
group(int groupIndex) | 返回上一次匹配操作中指定捕获组匹配的子序列 | 捕获组索引 | String |
start() | 返回上一次匹配操作中匹配子序列的起始索引 | 无 | int |
end() | 返回上一次匹配操作中匹配子序列的结束索引(不包括) | 无 | int |
replaceAll(String replacement) | 使用指定字符串替换所有与模式匹配的子序列 | 替换字符串 | String |
示例代码
1 2 3 4 5 6 7 8 9 10
| Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher("年龄: 25, 身高: 175.6"); while (matcher.find()) { System.out.println("匹配到的数字: " + matcher.group()); System.out.println("起始索引: " + matcher.start()); System.out.println("结束索引: " + matcher.end()); }
String replaced = matcher.replaceAll("XX"); System.out.println("替换后的字符串: " + replaced);
|
20. FileReader
和 FileWriter
类
用于字符文件的读写操作。
FileReader
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
read() | 读取单个字符 | 无 | int |
read(char[] cbuf) | 读取字符到缓冲区 | 字符数组 | int |
close() | 关闭流 | 无 | void |
FileWriter
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
write(int c) | 写入单个字符 | 要写入的字符 | void |
write(String str) | 写入字符串 | 要写入的字符串 | void |
write(char[] cbuf) | 写入字符数组 | 字符数组 | void |
flush() | 刷新缓冲区 | 无 | void |
close() | 关闭流 | 无 | void |
示例代码
1 2 3 4 5 6 7 8 9 10 11
| FileWriter writer = new FileWriter("example.txt"); writer.write("Hello, World!"); writer.close();
FileReader reader = new FileReader("example.txt"); char[] buffer = new char[100]; int length = reader.read(buffer); System.out.println("读取的内容: " + new String(buffer, 0, length)); reader.close();
|
21. BufferedReader
和 BufferedWriter
类
用于提高字符输入输出的效率,带有缓冲区。
BufferedReader
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
readLine() | 读取一行文本 | 无 | String |
read() | 读取单个字符 | 无 | int |
close() | 关闭流 | 无 | void |
BufferedWriter
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
write(String str) | 写入字符串 | 要写入的字符串 | void |
newLine() | 写入换行符 | 无 | void |
flush() | 刷新缓冲区 | 无 | void |
close() | 关闭流 | 无 | void |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt")); writer.write("第一行文本"); writer.newLine(); writer.write("第二行文本"); writer.close();
BufferedReader reader = new BufferedReader(new FileReader("example.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println("读取的行: " + line); } reader.close();
|
用于字节流的输入输出操作,是所有字节流类的父类。
方法名 | 描述 | 参数 | 返回值 |
---|
read() | 读取单个字节 | 无 | int |
read(byte[] b) | 读取字节到缓冲区 | 字节数组 | int |
close() | 关闭流 | 无 | void |
OutputStream
类常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
write(int b) | 写入单个字节 | 要写入的字节 | void |
write(byte[] b) | 写入字节数组 | 字节数组 | void |
flush() | 刷新缓冲区 | 无 | void |
close() | 关闭流 | 无 | void |
示例代码
1 2 3 4 5 6 7 8 9 10
| OutputStream os = new FileOutputStream("example.bin"); os.write(65); os.close();
InputStream is = new FileInputStream("example.bin"); int data = is.read(); System.out.println("读取的字节: " + data); is.close();
|
带缓冲区的字节流,提高读写效率。
常用方法
与InputStream
和OutputStream
类似,但在读写时使用缓冲区减少磁盘I/O操作。
示例代码
1 2 3 4 5 6 7 8 9 10 11
| BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("example.bin")); bos.write(65); bos.flush(); bos.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("example.bin")); int data = bis.read(); System.out.println("读取的字节: " + data); bis.close();
|
24. Properties
类
用于读取和写入属性文件(键值对)。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
load(InputStream inStream) | 从输入流加载属性列表 | 输入流 | void |
store(OutputStream out, String comments) | 将属性列表存储到输出流 | 输出流、注释 | void |
getProperty(String key) | 获取指定键对应的值 | 键 | String |
setProperty(String key, String value) | 设置键值对 | 键、值 | Object |
list(PrintStream out) | 将属性列表打印到指定输出流 | 输出流 | void |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Properties props = new Properties(); props.setProperty("name", "张三"); props.setProperty("age", "25");
try (OutputStream output = new FileOutputStream("config.properties")) { props.store(output, "配置文件"); } catch (IOException e) { e.printStackTrace(); }
Properties props = new Properties(); try (InputStream input = new FileInputStream("config.properties")) { props.load(input); System.out.println("name: " + props.getProperty("name")); System.out.println("age: " + props.getProperty("age")); } catch (IOException e) { e.printStackTrace(); }
|
25. GregorianCalendar
类
提供了更全面的日期和时间功能。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
get(int field) | 获取指定日历字段的值 | 日历字段(如YEAR 、MONTH 等) | int |
set(int field, int value) | 设置指定日历字段的值 | 日历字段、新值 | void |
add(int field, int amount) | 给指定日历字段添加或减去指定的时间量 | 日历字段、时间量 | void |
getTime() | 将GregorianCalendar 转换为Date 对象 | 无 | Date |
setTime(Date date) | 使用Date 对象设置日历时间 | Date 对象 | void |
示例代码
1 2 3 4 5
| GregorianCalendar calendar = new GregorianCalendar(); System.out.println("当前年份: " + calendar.get(Calendar.YEAR)); calendar.add(Calendar.MONTH, 1); Date date = calendar.getTime(); System.out.println("新日期: " + date.toString());
|
26. TimeZone
类
用于获取和操作时区信息。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
getDefault() | 获取默认时区 | 无 | TimeZone |
getAvailableIDs() | 获取所有可用的时区ID | 无 | String[] |
getTimeZone(String ID) | 获取指定ID的时区 | 时区ID | TimeZone |
useDaylightTime() | 判断该时区是否使用夏令时 | 无 | boolean |
getDisplayName() | 获取时区的显示名称 | 无 | String |
示例代码
1 2 3 4 5
| TimeZone defaultZone = TimeZone.getDefault(); System.out.println("默认时区ID: " + defaultZone.getID()); System.out.println("默认时区名称: " + defaultZone.getDisplayName()); TimeZone newYorkZone = TimeZone.getTimeZone("America/New_York"); System.out.println("纽约时区是否使用夏令时: " + newYorkZone.useDaylightTime());
|
用于格式化和解析日期。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
format(Date date) | 格式化日期为字符串 | Date 对象 | String |
parse(String source) | 将字符串解析为Date 对象 | 字符串 | Date |
示例代码
1 2 3 4 5 6 7 8 9 10 11
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date currentDate = new Date(); String formattedDate = sdf.format(currentDate); System.out.println("格式化后的日期: " + formattedDate);
try { Date parsedDate = sdf.parse("2022-07-01 10:30:45"); System.out.println("解析后的日期: " + parsedDate.toString()); } catch (ParseException e) { e.printStackTrace(); }
|
28. BigInteger
类
用于表示任意精度的整数。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
add(BigInteger val) | 加法运算 | 另一个BigInteger | BigInteger |
subtract(BigInteger val) | 减法运算 | 另一个BigInteger | BigInteger |
multiply(BigInteger val) | 乘法运算 | 另一个BigInteger | BigInteger |
divide(BigInteger val) | 除法运算 | 另一个BigInteger | BigInteger |
mod(BigInteger val) | 取模运算 | 另一个BigInteger | BigInteger |
pow(int exponent) | 幂运算 | 指数 | BigInteger |
compareTo(BigInteger val) | 比较两个BigInteger 的大小 | 另一个BigInteger | int |
toString() | 返回BigInteger 的字符串表示 | 无 | String |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| BigInteger a = new BigInteger("12345678901234567890"); BigInteger b = new BigInteger("98765432109876543210");
BigInteger sum = a.add(b); System.out.println("加法结果: " + sum);
BigInteger product = a.multiply(b); System.out.println("乘法结果: " + product);
BigInteger quotient = a.divide(b); System.out.println("除法结果: " + quotient);
BigInteger remainder = a.mod(b); System.out.println("取模结果: " + remainder);
|
29. BigDecimal
类
用于表示任意精度的浮点数。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
add(BigDecimal augend) | 加法运算 | 另一个BigDecimal | BigDecimal |
subtract(BigDecimal subtrahend) | 减法运算 | 另一个BigDecimal | BigDecimal |
multiply(BigDecimal multiplicand) | 乘法运算 | 另一个BigDecimal | BigDecimal |
divide(BigDecimal divisor) | 除法运算 | 另一个BigDecimal | BigDecimal |
divide(BigDecimal divisor, int scale, RoundingMode roundingMode) | 除法运算,指定精度和舍入模式 | 另一个BigDecimal 、精度、舍入模式 | BigDecimal |
setScale(int newScale, RoundingMode roundingMode) | 设置小数位数并舍入 | 小数位数、舍入模式 | BigDecimal |
compareTo(BigDecimal val) | 比较两个BigDecimal 的大小 | 另一个BigDecimal | int |
toString() | 返回BigDecimal 的字符串表示 | 无 | String |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| BigDecimal a = new BigDecimal("1234567890.1234567890"); BigDecimal b = new BigDecimal("9876543210.9876543210");
BigDecimal sum = a.add(b); System.out.println("加法结果: " + sum);
BigDecimal product = a.multiply(b); System.out.println("乘法结果: " + product);
BigDecimal quotient = a.divide(b, 2, RoundingMode.HALF_UP); System.out.println("除法结果(保留两位小数): " + quotient);
BigDecimal scaled = a.setScale(4, RoundingMode.HALF_UP); System.out.println("设置小数位数为4: " + scaled);
|
30. Math
类
提供基本的数学函数。
常用方法
方法名 | 描述 | 参数 | 返回值 |
---|
abs(double a) | 返回参数的绝对值 | 要计算绝对值的数 | double |
sqrt(double a) | 返回参数的平方根 | 要计算平方根的数 | double |
pow(double a, double b) | 返回a的b次幂 | 底数、指数 | double |
max(double a, double b) | 返回两个数中的较大值 | 两个数 | double |
min(double a, double b) | 返回两个数中的较小值 | 两个数 | double |
round(double a) | 返回最接近参数的整数 | 要四舍五入的数 | long |
random() | 返回一个介于0.0(包括)和1.0(不包括)之间的随机数 | 无 | double |
sin(double a) | 返回参数的正弦值 | 角度(弧度) | double |
cos(double a) | 返回参数的余弦值 | 角度(弧度) | double |
tan(double a) | 返回参数的正切值 | 角度(弧度) | double |
示例代码
1 2 3 4 5
| System.out.println("绝对值: " + Math.abs(-5.5)); System.out.println("平方根: " + Math.sqrt(25)); System.out.println("3的4次幂: " + Math.pow(3, 4)); System.out.println("最大值: " + Math.max(10, 20)); System.out.println("随机数: " + Math.random());
|