前言

大家好,给大家带来详细讲解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流的介绍以及类集合框架的更多相关文章

  1. 第五节:详细讲解Java中的接口与继承

    前言 大家好,给大家带来详细讲解Java中的接口与继承的概述,希望你们喜欢 什么是接口(interface) 接口中的方法都是抽象方法,public权限,全是抽象函数,不能生成对象 interface ...

  2. 第四节:详细讲解Java中的类和面向对象思想

    前言 大家好,给大家带来详细讲解Java中的类和面向对象思想的概述,希望你们喜欢 类和面向对象 在Java中怎样理解对象,创建对象和引用:什么是引用,对于基础学习的同学,要深入了解引用.示例:Stri ...

  3. 第九节:详细讲解Java中的泛型,多线程,网络编程

    前言 大家好,给大家带来详细讲解Java中的泛型,多线程,网络编程的概述,希望你们喜欢 泛型 泛型格式:ArrayList list= new ArrayList(); ArrayList list= ...

  4. 第七节:详细讲解Java中的日期,java.util.date

    前言 大家好,给大家带来详细讲解Java中的日期,java.util.date的概述,希望你们喜欢 类Date Java.lang.Object->java.util.Date public c ...

  5. 第六节:详细讲解Java中的装箱与拆箱及其字符串

    前言 大家好,给大家带来详细讲解Java中的装箱与拆箱及其字符串的概述,希望你们喜欢 装箱与拆箱 封装类有:Byte , short , Integer , Character , long , Fl ...

  6. 详细讲解JAVA中的IO流

    一.流的概念        流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等.        ...

  7. 【转】详细讲解Java中log4j的使用方法

    转载地址:http://www.233.com/Java/zhuangye/20070731/142625631.html 1.Log4j是什么? Log4j可以帮助调试(有时候debug是发挥不了作 ...

  8. 详细讲解Java中方法的重载和重写

    首先讲讲方法的重载: Java的重载就是在类中可以创建多个方法,它们具有相同的名字,但是却有不同的参数. 判断是否重载只有两个条件: 1)相同的方法名 2)不同的参数 具体为: A.方法参数类型不同 ...

  9. java 中的异常处理

    一. 异常的概念和Java异常体系结构  异常是程序运行过程中出现的错误.本文主要讲授的是Java语言的异常处理.Java语言的异常处理框架,     是Java语言健壮性的一个重要体现. Java把 ...

随机推荐

  1. springboot中配置文件application.properties的配置详情,数据源配置

    pring Boot使用了一个全局的配置文件application.properties,放在src/main/resources目录下或者类路径的/config下.Sping Boot的全局配置文件 ...

  2. 作为一名程序员,在面试中如何展现你Python的coding能力?

    来源商业新知,原文标题:如何在一场面试中展现你对Python的coding能力? 如果你已经通过了招聘人员的电话面试,那么下面正是该展现你代码能力的时候了.无论是练习,作业,还是现场白板面试,这都是你 ...

  3. win10版office365激活序列码

    win10版office365激活序列码(在别的地方找到一个) : NKGG6-WBPCC-HXWMY-6DQGJ-CPQVG 1.在线安装Office2016预览版后它是不会自动激活的,需在Offi ...

  4. 使用ibatis时 sql中 in 的参数赋值(转)

    转:http://www.cnblogs.com/sunzhenchao/archive/2012/12/03/2799365.html 一.问题描述: 1.在使用ibatis执行下面的sql: up ...

  5. 联想RD450带Read10服务器操作系统密码忘记

    联想RD450带Read10服务器操作系统密码忘记 可以用U盘PE进入重写密码 按F1进入BIOS界面设置如下 进入BOOT选项卡,设置U盘第一启动,也就是图中 Boot Option #1 设置为 ...

  6. swift static func 和 class func

    Swift中static func 相当于class final func.禁止这个方法被重写 clas func  可以被继承重写

  7. [leetcode]28. Implement strStr()实现strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. ES6 proxy(代理拦截) &&Reflect

  9. 数据库mysql之慢查询优化

    今天项目遇到一个问题:就是在公司test环境中执行sql查询语句很快,也就几百毫秒,但是放到sit环境中测试就要延迟至少1分钟左右. 网上找了很多原因,大多数都是说索引问题,我看了索引没问题,又重新建 ...

  10. java多线程系列6 synchronized 加强版 ReentrantLock

    ReentrantLock类是可重入.互斥.实现了Lock接口的锁,它与使用synchronized方法和快具有相同的基本行为和语义,并且扩展了其能力.ReenreantLock类的常用方法有: Re ...