日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

day22 Java学习 IO流(序列流)

發布時間:2023/12/9 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 day22 Java学习 IO流(序列流) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IO流(序列流)

? ? ?序列流:

? ? ? ? ? ? ? ? ? * 可以把多個字節輸入流整合成一個,從序列流中讀取數據時,將從被整合的第一個流開始讀,讀完一個之后繼續讀第二個。

? ? 整合方式:

? ? ? ? ? ? ? ? ? * Seq uenceInputStream ( InputStream ,InputStream )

?

IO流(序列流整合多個)

public static void main(String[] args) throws IOException {FileInputStream fis1 = new FileInputStream("test1.txt");FileInputStream fis2 = new FileInputStream("test2.txt");FileInputStream fis3 = new FileInputStream("test.txt");Vector<InputStream> v = new Vector<>();v.add(fis1);v.add(fis2);v.add(fis3);Enumeration<InputStream> en = v.elements();SequenceInputStream sis = new SequenceInputStream(en);FileOutputStream fos = new FileOutputStream("list.txt");int b;while ((b = sis.read()) != -1) {fos.write(b);}sis.close();fos.close();} 例子

?

IO流(內存輸出流)

??內存輸出流:

? ? ? ? ? ? ? ?* 該輸出可以向內存中寫數據,把內存當作一個緩沖區,寫出之后可以一次性獲取出所有數據。

public static void main(String[] args) throws IOException {FileInputStream fis1 = new FileInputStream("test1.txt");ByteArrayOutputStream by = new ByteArrayOutputStream(); // 在內存中創建了可以增長的內存數組int b;while ((b = fis1.read()) != -1) { // 將讀到的數據逐個寫到內存中 by.write(b);}System.out.println(by); // 將緩沖區的內容轉換成了字符串 fis1.close();} 例子

?

IO流(隨機訪問流和讀寫數據)

? ?隨機訪問流:

? ? ? ? ? ? ? ? ? ? ? * RandomAccessFile類不屬于流,是Object類的子類,但他融合了InputStream和OutputStream的功能。支持對隨機訪問文件的讀取和寫入。

? ? seek():設置指定位置讀和寫

?

IO流(對象操作流 )

? ?對象操作流?:該流可以將一個對象寫出。或者讀取一個對象到程序中,也就是執行了序列化和反序列化的操作。? ? ? ?

? ?使用方式:

? ? ? ? ? ? ? ?new?ObjectOutputStream(OutputStream),writeObject( )。?

? ? ? ? ? ? ? ?new?ObjectInputStream(OutputStream)? ? 對象輸入流(反序列化)

?

IO流(對象操作流優化)

? ? ?

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {Person p1 = new Person("張三", 14);Person p2 = new Person("李四", 15);Person p3 = new Person("王五", 16);ArrayList<Person> list = new ArrayList<>(); list.add(p1);list.add(p2);list.add(p3);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));oos.writeObject(list);//將整個集合一次寫出 oos.close();ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt"));ArrayList<Person> list1 = (ArrayList<Person>) ois.readObject(); //將整個集合一次讀取for (Person person : list1) {System.out.println(person);}ois.close();} 優化例子

?

IO流(數據輸入輸出流)

? ??數據輸入輸出流:

? ? ? ? ? ? ? *?DataInputStream,DataOutputStream可以按照基本數據類型大小讀寫數據。

? ? 使用方式:

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {DataOutputStream dos=new DataOutputStream(new FileOutputStream("g.txt"));dos.writeInt(997);dos.writeInt(998);dos.writeInt(999);dos.close();DataInputStream dis=new DataInputStream(new FileInputStream("g.txt"));int a=dis.readInt();int b=dis.readInt();int c=dis.readInt();System.out.println(a);System.out.println(b);System.out.println(c);} 例子

?

?IO流(Properties概述和作為Map集合的使用)

? ? ??Properties的概述:

? ? ? ? ? ? ? ? ? ? ?*?Properties是Hashtable的子類。

? ? ? ? ? ? ? ? ? ? ?*?Properties類表示了一個持久的屬性集。

? ? ? ? ? ? ? ? ? ? ?*?Properties可保存在流中或從流中加載。

? ? ? ? ? ? ? ? ? ? ?* 屬性列表中每個鍵及其對象值都是一個字符串

public static void main(String[] args) {Properties p= new Properties();p.put("a", 1);System.out.println(p);System.out.println(p.get("a"));} 例子

?

??IO流(Properties的特殊功能使用)

? ? ? * public Object setProperty( String key ,Sring value) :設置鍵和值

? ? ? * public String? getProperty( String key) :根據鍵來獲取值

? ? ? * public?Enumeration <string>? ?:迭代? ??stringPropertyNames():拿到所有的鍵?

public static void main(String[] args) {Properties p= new Properties();p.setProperty("a", "2");p.setProperty("b", "3");Enumeration<String> en= (Enumeration<String>) p.propertyNames();while (en.hasMoreElements()) {String key=en.nextElement();String value=p.getProperty(key);System.out.println(key+"="+value);}} 例子

?

IO流(Properties的load( )和Store ( ))

? ?

public static void main(String[] args) throws FileNotFoundException, IOException {Properties p= new Properties();System.out.println("讀取前:"+p);p.load(new FileInputStream("a.txt")); //load()讀取文件System.out.println("讀取后:"+p);p.setProperty("name", "lisi");p.store(new FileOutputStream("a.txt"), null); //store()第二個參數是用來描述文件列表的,如果不描述可以傳NULLSystem.out.println("添加后:"+p);} 例子

?

轉載于:https://www.cnblogs.com/feng0001/p/10965452.html

總結

以上是生活随笔為你收集整理的day22 Java学习 IO流(序列流)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。