Java核心类库(下)

java核心类库 Java 核心类库中常用类和工具类的实现和实践。 阅读详情

Java核心类库(下)

参加拉勾教育大数据训练营课程笔记

Java异常机制

Throwable类

  • java.lang.Object
    • java.lang.Throwable

All Implemented Interfaces:

Serializable

Direct Known Subclasses:

Error, Exception

是所有Error,Exception的父类(超类)。

  • Error是JVM遇到的无法解决的严重错误
  • Exception是程序错误,或者偶然外在因素导致的错误,可以通过修改代码解决

Exception分类

  1. RuntimeException,运行时异常,也称非检测性异常
  2. IOException和其他Exception,也叫检测性异常,是指 编译阶段能被检测出来

异常处理

多个异常捕获时,小的放前,大的放后

try {}
catch (ExceptionSmall e) {}
catch (ExceptionBig e) {}
finally {}

finally考点

public class Test {
    private static int t(int a, int b) {
        try {
            return a / b;
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
            return 1;
        } finally {
            return 2;
        }
    }
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        System.out.println(t(a, b));
    }
}

输出结果:

/ by zero
2

catch中的return得不到执行,而是进入了finally,会绕过return,continue,break。下面的代码块,在catch执行return前,finally代码块得到执行,然后回到catch,继续执行return 1返回1。

package phase01.module4.code;

public class Test {
    private static int t(int a, int b) {
        try {
            return a / b;
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
            return 1;
        } finally {
            System.out.println("finally");
        }
    }
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        System.out.println(t(a, b));
    }
}

运行结果:

/ by zero
finally
1

Notes

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

方法重写回顾:

  1. 参数相同

  2. 访问权限可以不变或更大

  3. 返回类型相同或父类

  4. 方法重写不能抛出更大的异常,或平级的不同异常,可以抛出相同或更小的异常,可以不抛出异常

抛出或捕获:

  1. 父类没有抛出异常时,重写方法时只能捕获处理

  2. 多层调用时,最后一次调用捕获处理异常

自定义异常类

  1. 继承Exception或其子类。
  2. 提供两个构造方法:无参,字符串参数
public class AgeException extends Exception {
    public AgeException() {
        super();
    }
    public AgeException(String message) {
        supper(message);
    }
}

File类

  • java.lang.Object
    • java.io.File

All Implemented Interfaces:

Serializable, Comparable<File>

public class File
extends Object
implements Serializable, Comparable<File>

字段

Modifier and TypeFieldDescription
static StringpathSeparatorThe system-dependent path-separator character, represented as a string for convenience.
static charpathSeparatorCharThe system-dependent path-separator character.
static StringseparatorThe system-dependent default name-separator character, represented as a string for convenience.
static charseparatorCharThe system-dependent default name-separator character.

构造方法

ConstructorDescription
File(String pathname)Creates a new File instance by converting the given pathname string into an abstract pathname.
File(File parent, String child)Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String parent, String child)Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)Creates a new File instance by converting the given file: URI into an abstract pathname.

常用方法

Modifier and TypeMethodDescription
booleancanExecute()Tests whether the application can execute the file denoted by this abstract pathname.
booleancanRead()Tests whether the application can read the file denoted by this abstract pathname.
booleancanWrite()Tests whether the application can modify the file denoted by this abstract pathname.
intcompareTo(File pathname)Compares two abstract pathnames lexicographically.
booleancreateNewFile()Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
booleandelete()Deletes the file or directory denoted by this abstract pathname.
voiddeleteOnExit()Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.
booleanequals(Object obj)Tests this abstract pathname for equality with the given object.
booleanexists()Tests whether the file or directory denoted by this abstract pathname exists.
FilegetAbsoluteFile()Returns the absolute form of this abstract pathname.
StringgetAbsolutePath()Returns the absolute pathname string of this abstract pathname.
StringgetName()Returns the name of the file or directory denoted by this abstract pathname.
StringgetParent()Returns the pathname string of this abstract pathname’s parent, or null if this pathname does not name a parent directory.
FilegetParentFile()Returns the abstract pathname of this abstract pathname’s parent, or null if this pathname does not name a parent directory.
booleanisDirectory()Tests whether the file denoted by this abstract pathname is a directory.
booleanisFile()Tests whether the file denoted by this abstract pathname is a normal file.
longlastModified()Returns the time that the file denoted by this abstract pathname was last modified.
longlength()Returns the length of the file denoted by this abstract pathname.
File[]listFiles()Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
booleanmkdir()Creates the directory named by this abstract pathname.
booleanmkdirs()Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
booleansetExecutable(boolean executable)A convenience method to set the owner’s execute permission for this abstract pathname.
booleansetExecutable(boolean executable, boolean ownerOnly)Sets the owner’s or everybody’s execute permission for this abstract pathname.
booleansetLastModified(long time)Sets the last-modified time of the file or directory named by this abstract pathname.
booleansetReadable(boolean readable)A convenience method to set the owner’s read permission for this abstract pathname.
booleansetReadable(boolean readable, boolean ownerOnly)Sets the owner’s or everybody’s read permission for this abstract pathname.
booleansetReadOnly()Marks the file or directory named by this abstract pathname so that only read operations are allowed.
booleansetWritable(boolean writable)A convenience method to set the owner’s write permission for this abstract pathname.
booleansetWritable(boolean writable, boolean ownerOnly)Sets the owner’s or everybody’s write permission for this abstract pathname.

IO流

按读写数据类型分:

  1. 字节流,可以读写任意类型的文件
  2. 字符流(2个字节),只能读取文本文件

以运行程序视角,按方向分为:

  1. 输入流
  2. 输出流

按流的角色分为:

  1. 节点流,直接与文件(输入输出流)直接关联
  2. 处理流,与其他流关联

体系结构表

分类字节输入流字节输出流字符输入流字符输出流
抽象基类InputStreamOutputStreamReaderWriter
访问文件FileInputStreamFileOutputStreamFileReaderFileWriter
访问数组ByteArrayInputStreamByteArrayOutputStreamCharArrayReaderCharArrayWriter
访问管道PipedInputStreamPipedOutputStreamPipedReaderPipedWriter
访问字符串--StringReaderStringWriter
缓冲流BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter
转换流--InputStreamReaderOutputStreamWriter
对象流ObjectInputStreamObjectOutputStream--
FilterInputStreamFilterOutputStreamFilterReaderFilterWriter
打印流-PrintStream-PrintWriter
推回输入流PushbackInputStream-PushbackReader-
特殊流DataInputStreamDataOutputStream--

课程掌握:
在这里插入图片描述

<<---------------- 未完待续 ---------------- >>

58、Java核心类库深入解析 本文深入解析了Java核心类库中的多个重要接口和类,包括CharSequence、Class、ClassLoader、Cloneable、Comparable等,并详细介绍了它们的功能、方法及应用场景。同时,还讲解了Java中相关的异常类及其使用情况,帮助开发者更好地理解和掌握Java底层机制,编写高效、健壮的Java程序。 阅读详情

相关推荐

java核心类库使用大全

PDF格式的java核心类库详解

Throwable 类

Throwable类

墨染的博客 4447

晶圆测试厂wafer map管理建议

按照Recipe设定bin定义 应有版本管理概念,一般使用最新版本做为活动版本即可 Bin明细中应包含 BinID、BinCode、BinName、BinType、BinColor、Description BinID可使用数字类型 BinCode可参照实际机台的Bin,用于匹配 有些公司也将BinID、BinCode理解为SBin和HBin 即 Software Bin & Hardware Bin 一般也将BinID对应到客户的Bin BinType

凯神做开发 1万+

Java的Throwable类

try {System.err.println("捕获到自定义异常: " + e.getMessage());throw new UnderageException("未成年人年龄不足: " + age);System.out.println("年龄验证通过");

weixin_63490470的博客 1733

保姆级教程:用再生龙Clonezilla Live给Ubuntu系统做全盘备份与恢复(含BIOS设置避坑)

本文提供了一份详细的Clonezilla Live教程,指导Ubuntu用户如何轻松完成全盘备份与恢复。从启动盘制作、BIOS设置避坑到备份参数解析和恢复操作,涵盖了所有关键步骤和常见问题解决方案,帮助用户高效保护系统数据。

weixin_42116058的博客 646

2025-E题国一-简易自行瞄准装置:基于嘉立创地猛星/天猛星与K230的视觉巡线云台系统设计

本文详细介绍了2025年全国大学生电子设计竞赛E题一等奖作品——基于嘉立创地猛星/天猛星与K230的视觉巡线云台系统设计。该系统通过地猛星主控实现小车巡线,天猛星主控驱动云台,结合K230开发板的视觉识别能力,构建了一套完整的视觉巡线云台系统,实现了自动巡线与精准瞄准功能。文章深入剖析了硬件选型、双闭环PID控制算法、视觉识别与目标跟踪等核心技术,并分享了系统调试与避坑经验。

weixin_42596011的博客 676

详解Throwable类,精心制作

java.lang 类 Throwable java.lang.Object 继承者 java.lang.Throwable 所有已实现的接口: java.io.Serializable 直接已知子类: Error, Exception Throwable类是所有异常信息的超类(父类)。 直接子类:Error类,Exception类。 Error类: 是程序无法处理的错误,表示运行应用程序中较严重问题。 就是程序运行时候抛出的最严重级别的错误了,如VirtualMachineEr...

深海大冒险的博客 1489

手把手教你用键盘在Ubuntu上给Lerobot机械臂采集HIL-SERL训练数据(附完整配置文件)

本文详细介绍了在Ubuntu系统中使用键盘控制Lerobot机械臂进行HIL-SERL训练数据采集的完整流程。从环境准备、机械臂校准到键盘遥操系统配置和数据采集优化,提供了实战指南和常见问题解决方案,帮助读者高效获取高质量训练数据,推动具身智能和强化学习应用的发展。

weixin_30733003的博客 481

Throwable类与异常

Throwable类,异常

跋涉者 7861

第4章 部署负载均衡 Keepalived + HAProxy

本章在所有 Master 节点部署 Keepalived + HAProxy,为 kube-apiserver 提供高可用入口。Keepalived 实现 VIP(192.168.3.59)漂移,HAProxy 轮询转发请求至三个 Master 的 6443 端口(监听 6444 避免冲突)。健康检查脚本连续 5 次失败则停止 Keepalived,触发 VIP 自动切换。本章还包含配置详解、日常运维和问题排查。

幸福街--linux技术分享 123

手把手教你学simulink--永磁同步电机场景实例:使用Simulink进行PMSM的矢量控制仿真

矢量控制的重要性提高效率:通过精确控制磁场方向,可以优化电机的工作状态,减少损耗。增强动态性能:矢量控制能够快速响应负载变化,提供更好的调速性能。简化设计:将复杂的交流电机控制问题转化为类似直流电机的控制问题,便于设计和实现。通过上述步骤,我们简要介绍了如何基于Simulink实现PMSM的矢量控制仿真。

MHD0815的博客 422

Throwable类的详细解释

概述  软件包   类  使用  树  已过时  索引  帮助    JavaTM 2 Platform Standard Ed. 5.0    上一个类   下一个类 框架    无框架    所有类   摘要: 嵌套 | 字段 | 构造方法 | 方法 详细信息: 字段 | 构造方法 | 方法  -----------------------------------------

李政的专栏 3万+

第076篇:大涡模拟进阶

大涡模拟(Large Eddy Simulation, LES)是介于直接数值模拟(DNS)和雷诺平均方法(RANS)之间的重要湍流模拟方法。本文深入介绍动态Smagorinsky模型、尺度相似模型、WALE模型和隐式LES等高级亚格子模型。通过Python实现槽道湍流和圆柱绕流的LES求解器,详细展示近壁面处理、动态系数计算和亚格子应力建模技术。大涡模拟,亚格子模型,动态模型,Smagorinsky,WALE模型,尺度相似模型,近壁面处理,槽道湍流LES的核心思想是对Navier-Stokes方程进行空间

2401_87715305的博客 740

JAVA-核心类库

Java核心类库是指Java语言提供的一组基础类和接口,用于处理常见的任务和操作,包括字符串处理、集合操作、IO操作、多线程等。熟练掌握Java核心类库的使用,可以提高开发效率和编写高质量的Java应用程序的能力。

分享,学习,专注,努力,向上。 2139

Qt实现思维导图功能5『纵向分布模式』

思维导图纵向分布模式:模式一

浮生流年的博客 1062

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Java核心类库

Java核心类库

qq_50048558的博客 4327

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Java核心类库(上)

Java核心类库(上) 参加拉勾教育大数据训练营课程笔记 学会查阅API文档,根据包索引,搜索等。 常用类 java.lang - 唯一一个Java虚拟机自动访问,所以System等不需要import就可以使用,例如:System, String等。 java.util - 提供大量工具类和集合类等,例如:Scanner , List, Random等。 java.io - 输入输出包,提供了文件读写,流读写等,例如:FileInputStream, FileOutputStream等。 java.n

一叶知秋 801

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Java常用核心类库总结二

前言 本文为java核心类库的另外一部分,主要介绍常用类库中的Date、DateFormat、Calendar、System和String类的常用方法。 【java常用核心类库总结一】添加链接描述 一、Date Date类归属于java.util包,Date类表示特定的时刻,精度为毫秒。 【常用构造方法】 1、Date () 1)作用描述:分配date对象并对其进行初始化,使其表示分配时间,精度到毫秒。 2)返回值类型:时间表示形式 3)Demo演示: 2、Date(long date) 1)作用描述:分

weixin_46623319的博客 260

Java 核心类库一览

作者:白色蜗牛来源:蜗牛互联网阅读本文你将收获:类库与 JAR 文件什么是类库我们知道,在面向对象的程序设计里,一个类是可以调用另外一个类的方法,只要把被调用的那个类引入到 classp...

程序员蜗牛 3045

Java 常用核心类库,很详细!常用!

​Collection​​ListArrayListLinkedList​SetHashSetTreeSet​MapHashMapTreeMap类/接口包用途Stringjava.lang字符串处理java.lang可变字符串(单线程)ArrayListjava.util动态数组HashMapjava.util键值对存储(无序)HashSetjava.util唯一值集合LinkedListjava.util双向链表java.time日期时间处理java.util。

m0_73884648的博客 967

Java核心类库(上)

java 核心类库 一 文章目录java 核心类库 一一、常用类的使用1. 常用的包1.1 常用的包1.2 Object类1.3 Integer 类1.4 Double 类1.5 Boolean 类1.6 Character 类1.7 包装类 (Wrapper) 的使用总结1.8 Math 类1.9 BigDecimal 类1.10 BigInteger 类二、String 类的使用2.1 String 类 的常量池2.2 常用的构造方法2.3 常用的成员方法2.4 正则表达式三、可变字符串类 以及 日期相

thisisBenjamin 328
上一篇: Java核心类库(上)
下一篇: Maven添加本地jar包
DataPeak
博客等级 码龄15年 32粉丝 68原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值