第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架
前言
大家好,给大家带来详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架的概述,希望你们喜欢
JAVA 异常
try...catch...finally结构的使用方法
class Test{
public static void main(String args[]){
try{
int i = 1 / 0;
}
catch(Exception e){
e.printStackTrace();
}
finally{
System.out.println("finally");
}
System.out.println(5);
}
}
class Test{
public static void main(String args[]){
try{
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
throw和throws的作用区别:
class Person{
private int age;
public void setAge(int age) throws Exception{
if(age<0){
RuntimeException e = new RuntimeException("年龄不能小于0");
throw e;
}
this.age = age;
}
}
class Test{
public static void main(String args[]){
Person person = new Person();
try{
person.setAge(-1);
}
catch(Exception e){
System.out.println(e);
}
}
}
Error和Exception的区别
- Error是Throwable的子类用于标记严重错误
- Exception是Throwable的子类,指示合理的程序想去catch的条件,非严重错误。
try/catch的执行过程
如果出现异常,系统则会抛出一个异常,进行捕捉(catch操作),或在最后(finally)来进行处理。
throw和throws的区别
throws 出现在方法声明上,throw出现在方法体内。
异常分类
异常分类:可查异常,运行时异常和错误
说说IO
//第一种:输入流输出流
//第二种:字节流字符流
//第三种:节点流处理流
//FileInputStream
class Test{
public static void main(String args[]){
FileInputStream fis = null;
try{
fis = new FileInputStream("e:/read.txt");
byte[] buffer = new byte[100];
fis.read(buffer,0,buffer.length);
for(int i = 0;i<buffer.length;i++){
System.out.println(buffer[i]);
}
}
catch(Exception e){
System.out.println(e);
}
}
}
class Test{
public static void main(String args[]){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("e:/read.txt");
fos = new FileOutputStream("e:/write.txt");
byte[] buffer = new byte[100];
int temp = fis.read(buffer,0,buffer.length);
fos.write(buffer,0,temp);
}
catch(Exception e){
System.out.println(e);
}
}
}
class Test{
public static void main(String args[]){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("e:/read.txt");
fos = new FileOutputStream("e:/write.txt");
byte[] buffer = new byte[1024];
while(true){
int temp = fis.read(buffer,o,buffer.length);
if(temp = -1){
break;
}
fos.write(buffer,0,temp);
}
}catch(Exception e){
System.out.println(e);
}finally{
try{
fis.close();
fos.close();
}catch(Excepiton e){
System.out.println(e);
}
}
}
}
//字符流
public class TextChar
public static void main(String args[]){
FileReader fr = null;
FileWriter fw = null;
try{
fr = new FileReader("e:/read.txt");
fw = new FileWriter("e:/write.txt");
char[] buffer = new char[100];
int temp = fr.read(buffer,0,buffer.length);
fw.write(buffer,0,temp);
}
catch(Exception e){
System.out.println(e);
}finally{
try{
fr.close();
fw.close();
}
catch(Excepiton e){
System.out.println(e);
}
}
}
//FileReader和BufferedReader
class Test{
public static void main(String args[]){
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try{
fileReader = new FileReader("e:/read.txt");
bufferedReader = new BufferedReader(fileReader);
String line = null;
while(true){
line = bufferedReader.readLine();
if(line == null){
break;
}
System.out.println(line);
}
}catch(Exception e){
System.out.println(e);
}
finally{
try{
bufferedReader.close();
fileReader.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
}
public class Test{
public static void main(String[] args) throws Exception{
//字节流
FileInputStream in = new FileInputStream("c:/read.txt");
FileOutStream out = new FileOutputStream("c:/write.txt");
byte[] buffer = new byte[1024];
int len;
while( (len = in.read(buffer)) != -1){
out.write(buffer,0,len);
}
in.close();
out.close();
//字符流
BufferedReader bf = new BufferedReader(new FileReader("c:/read.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter("c:/write.txt");
String str;
while( (str=bf.readLine()) != null ){
bw.write(str);
bw.newLine();
}
bf.close();
bw.close();
}
}
- 字节流: InputStream字节输入流,OutputStream字节输出流
- 字符流 : Reader字符输入流 ,Writer字符输出流
- 数据流: DataInputStream 数据输入流 ,DataOutputStream 数据输出流
集合框架
一组类和接口,位于java.util包,主要用于存储和管理对象,主要分为三大类---集合,列表和映射。
什么是集合(Set)
集合中对象是没有顺序的,并且没有重复对象;
什么是列表(List)
集合中对象可以有重复的对象,可以按照顺序取,也可以指定取。
什么是映射(Map)
每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。
类集框架主体结构
interface
Iterator Collection
ListIterator List Set Map
LinkeList ArrayList HashSet SortedSet HashMap SortedMap
LinkedHashSet TreeSet LinkedHashMap TreeMap
Comparable Comparator Collections Arrays
//arrayList默认10,可无限长,关于泛型
public class Test{
public static void main(String args[]){
//ArrayList arrayList = new ArrayList();
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
//String s = arrayList.get(1);
//System.out.println(s);
for(int i=0;i<3;i++){
String s = arrayList.get(i);
System.out.println(s);
}
}
}
优化
public class Test{
public static void main(String args[]){
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
for(int i = 0; i<arrayList.size();i++){
String s = arrayList.get(i);
System.out.println(s);
}
}
}
类集框架
集合 无序 不可重复
列表 有序 可重复
映射
Set继承了Collection
public class Test{
public static void main(String args[]){
//HashSet<String> hashSet = new HashSet<String>();
//Set<String> set = new HashSet<String>();
//别管就是转,方便
Set<String> set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
int i = set.size();
System.out.println(i);
}
}
不可以重复
public class Test{
public static void main(String args[]){
//HashSet<String> hashSet = new HashSet<String>();
//Set<String> set = new HashSet<String>();
//别管就是转,方便
Set<String> set = new HashSet<String>();
boolean b1 = set.isEmpty();
System.out.println(b1);
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("c");
boolean b2 = set.isEmpty();
System.out.println(b2);
int i = set.size();
System.out.println("clear之前的长度"+i);
set.clear();
int j = set.size();
System.out.println(j);
}
}
取数据,迭代 iterate器 (Iterator)
public class Test{
public static void main(String args[]){
//HashSet<String> hashSet = new HashSet<String>();
//Set<String> set = hashSet;
//Iterator <-- Collection <-- Set <-- HashSet
//hasNext() next()
Set<String> set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("c");
Iterator<String> it = set.iterator();
boolean b1 = it.hasNext();
if(b1){
String s = it.next();
System.out.println(s);
}
boolean b2 = it.hasNext();
if(b2){
String s = it.next();
System.out.println(s);
}
}
}
迭代器的使用
it.hasNext();
还有没有下一个元素,如果这个游标后面有元素就返回true,否则,false;
it.next();
返回游标所指位置的下一个元素,取出,用hasNext()看有没有,next取
优化
public class Test{
public stattic void main(String args[]){
Set<String> set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
set.add("d");
set.add("c");
Iterator<String> it = set.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
}
什么是映射(Map)
每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。
public class Test{
public static void main(String args[]){
HashMap<String,String> hasMap = new HashMap<String,String>();
Map<String,String> map = hasMap;
map.put("1","a");
map.put("2","b");
map.put("3","c");
map.put("4","d");
int i = map.size();
System.out.println(i);
}
}
public class Test{
public static void main(String args[]){
HashMap<String,String> hasMap = new HashMap<String,String>();
Map<String,String> map = hasMap;
map.put("1","a");
map.put("2","b");
map.put("3","c");
map.put("4","d");
map.put("3","e");
int i = map.size();
System.out.println(i);
String s = map.get("3");
System.out.println(ss);
}
}
public class TestCollection {
public static void main(String[] args) {
List<Hero> heros = new ArrayList<Hero>();
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero name " + i));
}
for (int i = 0; i < heros.size(); i++) {
Hero h = heros.get(i);
System.out.println(h);
}
}
}
总结
- 本文讲了详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架,如果您还有更好地理解,欢迎沟通
- 定位:分享
Android&Java知识点,有兴趣可以继续关注
第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架的更多相关文章
- 第五节:详细讲解Java中的接口与继承
前言 大家好,给大家带来详细讲解Java中的接口与继承的概述,希望你们喜欢 什么是接口(interface) 接口中的方法都是抽象方法,public权限,全是抽象函数,不能生成对象 interface ...
- 第四节:详细讲解Java中的类和面向对象思想
前言 大家好,给大家带来详细讲解Java中的类和面向对象思想的概述,希望你们喜欢 类和面向对象 在Java中怎样理解对象,创建对象和引用:什么是引用,对于基础学习的同学,要深入了解引用.示例:Stri ...
- 第九节:详细讲解Java中的泛型,多线程,网络编程
前言 大家好,给大家带来详细讲解Java中的泛型,多线程,网络编程的概述,希望你们喜欢 泛型 泛型格式:ArrayList list= new ArrayList(); ArrayList list= ...
- 第七节:详细讲解Java中的日期,java.util.date
前言 大家好,给大家带来详细讲解Java中的日期,java.util.date的概述,希望你们喜欢 类Date Java.lang.Object->java.util.Date public c ...
- 第六节:详细讲解Java中的装箱与拆箱及其字符串
前言 大家好,给大家带来详细讲解Java中的装箱与拆箱及其字符串的概述,希望你们喜欢 装箱与拆箱 封装类有:Byte , short , Integer , Character , long , Fl ...
- 详细讲解JAVA中的IO流
一.流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. ...
- 【转】详细讲解Java中log4j的使用方法
转载地址:http://www.233.com/Java/zhuangye/20070731/142625631.html 1.Log4j是什么? Log4j可以帮助调试(有时候debug是发挥不了作 ...
- 详细讲解Java中方法的重载和重写
首先讲讲方法的重载: Java的重载就是在类中可以创建多个方法,它们具有相同的名字,但是却有不同的参数. 判断是否重载只有两个条件: 1)相同的方法名 2)不同的参数 具体为: A.方法参数类型不同 ...
- java 中的异常处理
一. 异常的概念和Java异常体系结构 异常是程序运行过程中出现的错误.本文主要讲授的是Java语言的异常处理.Java语言的异常处理框架, 是Java语言健壮性的一个重要体现. Java把 ...
随机推荐
- ReactiveX 学习笔记(23)RxCpp
RxCpp RxCpp 是 ReactiveX 的 C++ 语言实现. 下载 RxCpp $ git clone --recursive https://github.com/ReactiveX/Rx ...
- Sql Server数据库之流程定义变量和流程控制语句
一.局部变量和全局变量 1.声明局部变量 语法:declare @变量名 变量类型 2.给局部变量赋值 语法:set @变量名=值, select @变量名=值 区别:第一种方式用于普 ...
- lastIndexOf() 找出指定元素出现的所有位置(返回的是下标数组)---lastIndexOf() 这个方法是倒叙查找,正序的是indexOf()
var indices = []; var array = ['a', 'b', 'a', 'c', 'a', 'd']; var element = 'a'; var idx = array.las ...
- java.io.Flushable 接口
package java.io; import java.io.IOException; /** * 在什么情况下用呢? 缓冲输出到一个流中的情况下,这个流要刷新. */ public interfa ...
- 云笔记项目-Java反射知识学习
在云笔记项目中,补充了部分反射的知识,反射这一部分基础知识非常重要,前面学习的框架Spring和MyBatis读取xml配置文件创建对象,以及JDBC加载驱动等都用了反射,但只知道有这个东西,具体不知 ...
- poj3273(二分)
题目链接:https://vjudge.net/problem/POJ-3273 题意:给定n个数,将这n个数划分成m块,问所有块最大值的最小是多少. 思路:注意到所求值最大为109,所以可以用二分来 ...
- linux用户和组管理,/etc/passwd 、/etc/shadow和/etc/group --学习
一./etc/passwd 和/etc/shadow解释 与用户相关的系统配置文件主要有/etc/passwd 和/etc/shadow,其中/etc/shadow是用户资讯的加密文件,比如用户的密码 ...
- 49-2015年第6届蓝桥杯Java B组
1.三角形面积 如图1所示.图中的所有小方格面积都是1. 那么,图中的三角形面积应该是多少呢? 请填写三角形的面积.不要填写任何多余内容或说明性文字. image.png 计算方法: 8 * ...
- go语言 http学习
net/http库学习 概念 处理器 处理器:拥有ServeHTTP方法的接口(任何类型) 签名:ServeHTTP(http.ResponseWriter, *http.Request) Respo ...
- php之$_SESSION的理解
1.什么是session? Session的中文译名叫做“会话”,其本来的含义是指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个sessi ...