1. Genericity:
①<T>: only T type
public class Dog <X>{
X str;
int age;
void test(){
System.out.println(str);
}
}
public static void main(String[] args) {
Dog<String> s = new Dog<>();
s.str = "pd";
s.test();
}
②<?>Wildcards: any type, no limitation
List<String> l = new ArrayList<>();
l.add("aaa");
l.add("bbb");
l.add("ccc");
test(l);
List<Integer> 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 <list.size(); j++) {
System.out.println(list.get(j));
}
}
③<? extends T>: any class as long as extends T(included T)
{
List<Dog> 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<Animal> 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 <U> 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)
(1)RuntimeException do not need to display Exception.
(2)Kinds:
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:
(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.