您的位置 首页 java

第八节:详细讲解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

//第一种:输入流输出流//第二种:字节流字符流//第三种:节点流处理流//FileInputStreamclass 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和BufferedReaderclass 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();
 }
} 

集合框架

一组类和接口,位于java.util包,主要用于存储和管理对象,主要分为三大类—集合,列表和映射。

什么是集合(Set)

集合中对象是没有顺序的,并且没有重复对象;

什么是列表(List)

集合中对象可以有重复的对象,可以按照顺序取,也可以指定取。

什么是映射(Map)

每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。

类集框架主体结构

interface
Iterator Collection
ListIterator List Set MapLinkeList 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流的介绍以及类集合框架

文章地址:https://www.zhihuclub.com/181917.shtml

关于作者: 智云科技

热门文章

网站地图