java genericity_The advance of Java -- Genericity, Exception, IO(Day04)

1. Genericity:

①: only T type

public class Dog {

X str;

int age;

void test(){

System.out.println(str);

}

}public static void main(String[] args) {

Dog s = new Dog<>();

s.str = "pd";

s.test();

}②>Wildcards: any type, no limitation

List l = new ArrayList<>();

l.add("aaa");

l.add("bbb");

l.add("ccc");

test(l);

List l1 = new ArrayList<>();

l1.add(123);

l1.add(456);

l1.add(789);

test(l1);

}

public static void test(List> list){

for (int j = 0; j

System.out.println(list.get(j));

}

}③ extends T>: any class as long as extends  T(included T)

{

List d = new ArrayList<>();

d.add(new Dog("H",12));

test(d);

}

public static void test(List extends Animal> l){

for(int i , i < l.size(), i++)

System.out.println(l.get(i));

}④ super T>:  any class as long as extended by T (included)

List l = new ArrayList<>();

l.add(new Dog("H",12,"red"));

Dog d = new Dog("K",11,"brown");

test(l,d);

System.out.println(l);

test1(l,d);

System.out.println(l);

}

public static void test(List super U> l, U d){

l.add(d);

}2. Exception:

Grammar:

①try{

//normal sentences

}catch(Exception e){

//handle sentences

}finally{

//using to close some resources

}

②throws: Thorwing exception to superclass, and finally handled by Java.

*The Exception of subclass cannot throw over superclass.

③custom Exception:

throw new xxxException();(It must extends Exception, and will call the constructor of superclass)

④The difference of RuntimeException and none-RuntimeException:

(1)RuntimeException do not need to display Exception.

(2)Kinds:

b9dbb907e0391ae7270cefca0a700a03.png

3. IO:

①File:supporting files' or directories' basic information. but not include specific contents.

public static void main(String[] args) throws IOException {

File f = new File("d:/a.properties");

System.out.println(f.exists());

File f1 = new File("D:");

File f2 = new File(f1,"a.properties");

System.out.println(f2.exists());

System.out.println(f.isFile());

System.out.println(f.isDirectory());

System.out.println(f.length());

//System.out.println(f.createNewFile());

//f.delete();

//System.out.println(f.createNewFile());

f.mkdir();

f = new File("d:/b/c");

f.mkdirs();

}

②InputStream & Reader: they are abstract class and the superclass of all the InputStream. Allowing program read external data, like disk. There are three methods:

int read(): read single byte

int read(char[]/byte[]): read byte or char array

int read(char[]/byte[], int off, int len):read

byte or char array since off position.

(1)FileInputStream: (Byte Stream)

public static void main(String[] args) throws IOException {

FileInputStream f = new FileInputStream("src/com/bsr/day1128/Demo01.java");

int len = -1;

byte[] b = new byte[100];

while((len = f.read(b)) != -1){//读位数而不是字符

System.out.print(new String(b));//把数组转换成字符串

}

}(2)FileReader: (Character Stream)

public static void main(String[] args) throws FileNotFoundException {

FileReader f = new FileReader("src/com/bsr/day1128/Demo01.java");

int len = -1;

try {

while((len = f.read()) != -1){

System.out.print((char) len);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

f = new FileReader("src/com/bsr/day1128/Demo01.java");

char[] c = new char[10];

try {

while((len = f.read(c)) != -1){

System.out.print(new String(c,0,len));

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}(3)FileOutputStream & FileWriter:

public static void main(String[] args) {

//try {

//FileOutputStream o = new FileOutputStream("d:/a.properties");

try {

FileWriter w = new FileWriter("d:/a.properties");

w.write("World Hello!");

w.flush();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

/*try {

o.write("hello world!".getBytes());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}*/

}(4)Processing Stream: need a existing stream

public static void main(String[] args) {

try {

FileOutputStream o = new FileOutputStream("e:/a.properties");

PrintStream p = new PrintStream(o);

p.print("Hello");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}(5)The system of IO:

dd065de7a21cf9c9b60bbf63eea08350.png

(6)InputStreamReader & OutputStreamReader: using to converted Byte Stream into Character Stream

public static void main(String[] args) {

InputStreamReader i = new InputStreamReader(System.in);

BufferedReader b = new BufferedReader(i);

try {

System.out.println(b.readLine());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

(7)SetOut(): redirect

public static void main(String[] args) throws FileNotFoundException {

System.setOut(new PrintStream("d:/a.properties"));

System.out.println("hhhhhhhh");

}

(8) Recursive walk of the files or directories in a disk:

package com.pd;

import java.io.File;

public class test2 {

public static void main(String[] args) {

File f = new File("e:/");

test(f);

}

public static void test(File f){

File[] f1 = f.listFiles();

for (int i = 0; i < f1.length; i++) {

if(f1[i].isDirectory() && !f1[i].isHidden()){

System.out.println(f1[i].getName());

test(f1[i]);

}else{

System.out.println(f1[i].getName());

}

}

}

}*We need to consider the hidden files or directories of system, since we have to gain the permission otherwise it will throw NullPointerException.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值