一、IO流
1.理解
傳輸數據、做輸入輸出、拷貝、上傳下載
不同流:一連串流動的數據,以先入先出的方式流動,管道
作業:傳輸數據,讀入寫出,上傳下載
2.流的分類
1)按照流向分: 都是大腦爲中心,程序爲中心
輸出流、輸入流
文件(數據源)--輸入流-->程序(目的地) 程序--輸出->文件
2)按照操作數據單元:
字節流:萬能流,能夠傳輸任意類型的數據
字符流:純文本內容
3)按照功能分:
節點流:真實用來傳輸數據,數據從數據源到目的地
功能流:擴展節點流的功能
3.字節流
讀入 以程序爲中心 數據源--讀入-->程序
InputStream 抽象父類 字節輸入流
FileInputStream 文件字節輸入流,從系統文件中讀入數據到程序
構造器 FileInputStream(String name) 內部會先構建File對象,然後調用下面結構File對象的構造器
new FileInputStream(File);
方法
read()->返回值int,讀到的字節數據,如果沒有數據返回-1 讀入一個字節的數據
read(byte[]) 一個字節數組一個字節數組讀入,返回讀入到數組中數據的個數,如果沒有讀到,返回-1。
注意:java中字符串中表示路徑可以使用\或者/,使用\會識別成爲轉義字符
public class IODemo01 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //創建一個文件字節流 FileInputStream file=new FileInputStream("F:\\test.txt"); //System.out.println(file); //讀取文件信息一個字節 // int num=file.read(); // System.out.println((char)num); //用循環來讀取每個字節,循環爲-1的時候結束 int num=-1; while((num=file.read())!=-1){ System.out.print((char)num); } //讀取文件信息以數組輸出 byte[] by=new byte[1024]; //文件有多少個字節 int len=-1; while((len=file.read(by))!=-1){ System.out.println(new String(by,0,len)); } file.close(); } }
字節輸出流:
OutputStream 抽象父類
FileOutputStream 文件字節輸出流
構造器
FileOutputStream(String name) 創建一個文件輸出流寫入文件指定名稱。
默認覆蓋原有內容
FileOutputStream(String name, boolean append)寫出 並且追加。
常用方法
write(int)
write(byte[] b)
write(byte[] b, int off, int len)
flush() 只要是輸出流都要記得關閉之前刷出
close()
寫出的時候,如果文件系統中不存在,系統會自動幫你創建一個,但是如果是目錄不存在,系統不會創建
public class IODemo02 { public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub //文件輸出流,追加blooean ture(append) FileOutputStream os=new FileOutputStream("F://test.txt",true); //寫入write(int) os.write(97); //write(byte[] b) os.write("hhhh".getBytes()); //刷出 os.flush(); //關閉 os.close(); } }
5.文件拷貝字符流
數據源文件----->程序------>目的地文件
文件拷貝:
1.創建流
文件字節輸入流
文件字節輸出流
2.先讀入 後寫出
3.刷出
4.關閉(後打開的先關閉)
public class CopyFile03 { public static void main(String[] args) { //1.創建流 InputStream is=null; OutputStream os=null; try { /*is=new FileInputStream("D:/test.txt"); os=new FileOutputStream("E:/test.txt");*/ //2.先讀入 後寫出 byte[] car=new byte[1024]; int len=-1; while(-1!=(len=is.read(car))){ os.write(car, 0, len); } //3.刷出 os.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ //無論try中是否出現異常代碼,都會執行finally中的關閉功能 //4.關閉(後打開的先關閉) try { if(os!=null){ //預防空指針異常出現 os.close(); } if(is!=null){ //預防空指針異常出現 is.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
6.字符流
字符流:
只能拷貝純文本的數據
輸入流: Reader FileReader read() read(char[]) close()
輸出流: Writer FileWriter write() + flush() close()
實現字符流的文件拷貝,測試拷貝圖片不合適
public class IODemo03 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //字符流 Reader Writer //輸入流 Reader rd=new FileReader("F://test.txt"); //Reader rd=new FileReader("D://1.jpg"); //寫入流 //Writer rt=new FileWriter("D://3.jpg"); Writer rt=new FileWriter("F://hh.txt"); //讀寫 char [] car=new char[1024]; int len=-1; while((len=rd.read(car))!=-1){ rt.write(car, 0, len); } //刷出 rt.flush(); //關閉 rt.close(); rd.close(); } }
7.對象流(功能流)
對象流 (功能流) :讀寫對象|任意類型數據 保留數據+數據類型
使用: 功能流(節點流)
功能流:提高節點流的功能,增強性能
ObjectInputStream 對象|反序列化輸入流 新增方法:readXxx()
ObjectOutputStream 對象|序列化字節輸出流 新增方法:writeXxx()
注意:不能發生多態
序列化:把對象數據轉爲可存儲或者可傳輸的過程,成爲序列化
不是所有的類型都能夠序列化 java.io.Serializable
先序列化後反序列化
讀寫的內容順序保持一致
不是所有的屬性都需要序列化 transient,屬性值爲默認值
靜態的內容不會被序列化
如果父類實現序列化接口,子類的內容也可以序列化,子類實現序列化接口,子類只能序列化子類的內容,父類的內容無法序列化
public class ObjectDemo01 { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { // TODO Auto-generated method stub write("F://test.txt"); read("F://test.txt"); } //序列化輸出流 name:目的地文件的路徑 public static void write(String name) throws FileNotFoundException, IOException{ //定義對象流輸出流 ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(name)); Student s=new Student("xiao",18); //寫出 os.writeBoolean(true); os.writeUTF("UTF-8"); os.writeObject(s); //刷出 os.flush(); //關閉 os.close(); s.age=20; } //反序列化輸入流 public static void read(String name) throws FileNotFoundException, IOException, ClassNotFoundException{ ObjectInputStream is=new ObjectInputStream(new FileInputStream(name)); //讀入 boolean b=is.readBoolean(); String str=is.readUTF(); Object s=is.readObject(); System.out.println(b+"--->"+str+"--->"+s); //關閉 is.close(); } }
二、函數式接口
1.四大函數式接口
消費性接口 Consumer<T> 表示接受單個輸入參數並且不返回結果的操作。 void accept(T t) lambda表達式:T什麼型都可以,有參數沒有返回值 public static void get(String str,Consumer<Stirng>con){ con.accept(str); } //String get("jlk",t->t.方法()); String::equals//類::成員方法 //int get(2,t->{System.out.println(t);}) System.out::println//對象::成員方法 //boolean 供給型接口 Supplier<T> 該供應商提供的結果類型* T get()** get()方法在list集合中使用 public static List<Integer> get(int num,Supplier<List<Integer>>sup){ List ls=new ArrayList(); for(int i=0;i<num;i++){ ls.add(sup.get()); } return list; } //get(1,()->(int)Math.random()(5-2+1)+2); // 函數型接口 Function<T,R> 表示接受一個參數併產生結果的函數。** R apply(T t)** public static String get(String s1,Function<String,String>fun){
return fun.apply(s2);; } get("jkk",t->t.方法) String::trim//類名::成員方法斷定型接口 Predicate<T> 表示一個參數的謂詞(布爾值函數)。** boolean test(T t)** public static boolean get(String s,Predicate<String>pre){ return pre.test(s); } get("",t->t.方法()) String::equals //類名::成員方法
2.方法引用::
簡化Lambda表達式,可以理解爲Lambda表達式的另一種表現形式
前提: 當重寫的抽象方法方法體的實現,其實就是通過調用另外一個已有的方法進行實現,可以通過方法引用進行調用,簡化Lambda的寫法
對象的引用 ::成員方法;
類名::靜態方法名;
類名::成員方法;
要求: 函數型接口的抽象方法的參數列表和返回值要求與內部引用的方法的參數列表和返回值保持一致!!!!
構造器引用:
new Person();
Person::new;
類型::new;
/** * 對象的引用 ::成員方法; * */ public class Dome01 { public static void main(String[] args) { // TODO Auto-generated method stub Fruit f=new Fruit(); A<Fruit> a=()->{f.getName();}; //對象調用了getName() a=f::getName; //System.out.println(); } } class Fruit{ private String name; public Fruit(){ } public void getName(){ System.out.println("名字是:"+this.name); } } //創建接口 interface A<T>{ void get(); } /** * 類名::靜態方法名; * */ public class Demo02 { public static void main(String[] args) { // TODO Auto-generated method stub B<Integer> b=(t)->{Animal.getAge(t);}; b=Animal::getAge; } } class Animal{ int age; public Animal(){ } public static void getAge(int age){ System.out.println("年齡:"+age); } } interface B<T>{ void get(T t); }/** * 構造器 * 類型類名::new; * */ public class Demo03 { public static void main(String[] args) { // TODO Auto-generated method stub Car car=new Car(); C<Car> c=()->{return new Car();}; c=()->new Car(); c=Car::new; } } class Car{ String name="str"; public Car(){ } } interface C<T>{ T get(); } //類名::靜態方法 public class Demo04 { public static void main(String[] args) { // TODO Auto-generated method stub Student s=new Student(); D<Student,String> d=(t)->{return t.get(s);}; d=Student::get; } } class Student{ String name; public Student(){ } public static String get(Student s){ return s.name; } } interface D<T,R>{ R get(T t); }