[笔记][java 4 android] [028~034]接口、异常和I/O流

【028】接口

interface定义,方法是抽象方法,权限均为public

*还记得抽象的特性吗?不能生成对象,但是能够被继承,能被复写

接口的实现类,其实就是继承类
如:
usb.java
interface usb{
     public void read();
     public void write();
}
usbPhone.java
class usbPhone implements usb{
     public void read(){
          System.out.println("usbPhone reading");
     }
     public void write(){
          System.out.println("usbPhone write");
     }
}


test.java
class test{
     public static void main(String args []){
          usbPhone usbp1 = new usbPhone();
          //向上转型,将子类对象赋值给父类引用
          usb u1 = usbp1;
          u1.read();
          u1.write();
     }
}


Tips:一个类可以实现多个接口(也就是继承中的特殊情况,子类有多个父类)

如:
wifi.java
interface wifi{
     public void open();
     public void close();
}

usb.java
interface usb{
     public void read();
     public void write();
}

Phone.java
class Phone implements usb,wifi{
     public void read(){
          System.out.println("Phone usb reading");
     }
     public void write(){
          System.out.println("Phone usb write");
     }
     public void open(){
          System.out.println("phone wifi open");
     }
     public void close(){
          System.out.println("phone wifi close");
     }
}

test.java
class Phone implements usb,wifi{
     public void read(){
          System.out.println("Phone usb reading");
     }
     public void write(){
          System.out.println("Phone usb write");
     }
     public void open(){
          System.out.println("phone wifi open");
     }
     public void close(){
          System.out.println("phone wifi close");
     }
}



Tips:一个接口也可以继承多个接口
interface A{}
interface B{}
interface C implements A,B{}
这里C继承了A和B

【029】接口的应用

呃,一个工厂模式的内容,有相关基础的不用看了ORZ。。

【030】

囧,集数混乱了,见笔记【027】

【31】java中的异常

关键字throw和throws


1:throw
User.java
class User{
     private int age;
    
     void setAge(int age){
          if(age < 0){
               //uncheck exception
               RuntimeException r = new RuntimeException("年龄不能是负数");
               throw r;
          }
          this.age = age;
     }
}

test.java
class test{
     public static void main(String args []){
          User user = new User();
          user.setAge(-20);
     }
}



2、throws
User.java
class User{
     private int age;
     
     void setAge(int age) throws Exception{
     //throws Exception 声明一个异常,但是不处理
          if(age < 0){
               //uncheck exception
               Exception r = new Exception("年龄不能是负数");
               throw r;
          }
          this.age = age;
     }
}

test.java
class test{
     public static void main(String args []){
          User user = new User();
          try{
               user.setAge(-20);
          }
          catch(Exception c){
          //对异常进行处理
               System.out.println(c);
          }
     }
}


Tips:
1、RuntimeException是运行时的异常,其他都是编译时的异常,不过不进行声明或者捕捉均会报错
2、对于异常的处理,需要一定的经验积累,现在是分成两种,①是在函数内进行异常处理,②是函数抛出异常,调用函数的方法进行异常处理

【032】java的I/O流

I/O操作的目的:从数据源中读取数据,将数据写入目的数据源中。

I/O核心类:
InputStream          OutputStream   是抽象类,是所有字符流的父类
     ↓                          ↓
FileInputStream     FileOutputStream  字节流

InputStream核心方法:
int read(byte [] b, int off, int len)     //从b字节数组的第off的字节开始,读取len个,返回值为len

OutputStream核心方法
void write(byte [] b, int off, int len)     //基本同上,off从0开始,和数组下标保持一致


如:
import java.io.*;     //1、导入类
class test{
     public static void main(String args []){
          //2、声明输入流引用
          FileInputStream fis = null;
          //声明输出流引用
          FileOutputStream fos = null;
          try{
               //3、生成代表输入和输出流对象
               fis = new FileInputStream("d:/android/src/from.txt");
               fos = new FileOutputStream("d:/android/src/to.txt");
               //4、生成字节数组
               byte [] buffer = new byte[10];
               //5、调用输入流对象的read方法,返回值为读取字节长度
               int temp = fis.read(buffer,0,buffer.length);
               //放入fos
               fos.write(buffer,0,temp);
               for(int i = 0; i < buffer.length; i++){
                    System.out.println(buffer[i]);
               }
          }
          catch(Exception e){
               System.out.println(e);
          }
     }
}
结果:字符转换为ascii码,而from.txt中的内容已经被复写到to.txt中



【033】I/O流继续

1、大文件的读写方法以及关闭输入输出流的方法

import java.io.*;
class test{
     public static void main(String args []){
          //2、声明输入流引用
          FileInputStream fis = null;
          //声明输出流引用
          FileOutputStream fos = null;
          try{
               //3、生成代表输入和输出流对象
               fis = new FileInputStream("d:/android/src/from.txt");
               fos = new FileOutputStream("d:/android/src/to.txt");
               //4、生成字节数组
               byte [] buffer = new byte[5];
               //
               while(true){
                    int temp = fis.read(buffer,0,buffer.length);
                    if(temp == -1){
                         break;
                    }
                    else{
                         fos.write(buffer,0,buffer.length);
                    }
               }
          }
          catch(Exception e){
               System.out.println(e);
          }
          finally{
               try{
                    fis.close();
                    fos.close();
               }
               catch(Exception e){
                    System.out.println(e);
               }
          }
     }
}


2、字符流的使用方法
读写文件时,以字符为基础
Reader             Writer   字符流的父类
     ↓                    ↓
FileReader     FileWriter

FileReader核心方法
int read(char [] c, int off, int len)
FileWriter核心方法
void write(char [] c, int off, int len)
*理解参见字节流

例如:
import java.io.*;

class test{
     public static void main(String args []){
          FileReader fr = null;
          FileWriter fw = null;
          try{
               fr = new FileReader("d:/android/src/from.txt");
               fw = new FileWriter("d:/android/src/to.txt");
               
               char [] buffer= new char[13];
               int tmep = fr.read(buffer,0,buffer.length);
               fw.write(buffer,0,temp);
               for(int i = 0;i<buffer.length;i++){
                    System.out.println(buffer[i]);
               }
          }
          catch(Exception e){
               System.out.println(e);
          }
          finally{
               try{
                    fr.close();
                    fw.close();
               }
               catch(Exception e){
                    System.out.println(e);
               }
          }
     }
}
结果:输出from.txt的内容,以及三个空字符(无内容),from.txt内容复写至to.txt



【034】I/O流继续~

1、处理流实例
BufferedReader(字符流、输入流、处理流)
核心方法:
public String readLine() throwsIOException:以行来处理数据

如:
import java.io.*;
class test{
     public static void main(String args []){
          FileReader fr = null;
          BufferedReader br = null;
          try{
               fr = new FileReader("d:/android/src/from.txt");
               br = new BufferedReader(fr);
               String line = null;
               while(true){
                    line = br.readLine();
                    if(line == null){
                         break;
                    }
                    System.out.println(line);
               }
          }
          catch(Exception e){
               System.out.println(e);
          }
          finally{
               try{
                    fr.close();
                    br.close();
               }
               catch(Exception e){
                    System.out.println(e);
               }
          }
     }
}
结果:


Tips:
使用一个处理流,必须要有一个节点流

2、Decorator模式
装饰者模式,不细说。。。主要在于理解。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值