前言

大家好,给大家带来详细讲解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. LevelDB源码分析-Compact

    Compaction compact由背景线程完成,代码中触发背景线程的函数为: void DBImpl::MaybeScheduleCompaction() { mutex_.AssertHeld( ...

  2. SQL Server 磁盘请求超时的833错误原因分析以及解决

    本文出处:http://www.cnblogs.com/wy123/p/6984885.html 最近遇到一个SQL Server服务器响应极度缓慢,并且出现客户端请求报错的情况,在数据库中的erro ...

  3. Html转成Image

    html转图片,原来用了html2image.jar,但是做不到复杂的css js渲染效果. 在网友推荐下,可以用wkhtmltoimage插件,这是用谷歌浏览器的内核webkit,网上搜一下可以下载 ...

  4. 【ESP8266】、ESP8266通讯使用的AT指令

    一.AT指令介绍 AT(Attention), AT指令一般应用于终端设备和PC应用之间建立连接.通过AT指令来控制. 二.常用AT指令 AT指令主要分为: 基础AT指令,WIFI功能AT指令,TCP ...

  5. Lua中面向对象

    一.Lua中类的简单实现: (1)版本——摘自 Cocos2.0中的: --Create an class. function class(classname, super) local superT ...

  6. react组件回顶部

    在挂载更新里面判断滚动条的距离(滚动条不能overflow: auto 踩坑) componentDidMount(){ window.addEventListener('scroll' , ()=& ...

  7. NET Core小细节杂记

    1.中文编码问题: 01.在net core中,使用中文的编码,要先进行注册: //CodePagesEncodingProvider在包System.Text.Encoding.CodePages中 ...

  8. 46-2016 蓝桥杯 java B 组

    1.煤球数目 有一堆煤球,堆成三角棱锥形.具体: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形), .... 如果一共有100层,共有多少个煤 ...

  9. 以太坊Inner Transaction合约内充值转账

  10. UI 设计的整个工作流程是怎样的?

    作为一个专业UI设计师,不仅仅要了解整个产品在UI界面设计,交互设计中的工作流程,更需要了解整个产品从需求提出到产品上线的整个工作流程. 以下是互联网产品个部门的工作分配及流程: 从图中可以看到,一个 ...