IO流的常用方法(三)

對象序列化

對象序列化:將對象轉換爲二進制字節序列並儲存在存儲介質中的過程。
注意: 對象序列化和反序列化的前提是本類或其超類實現Serializable接口。

ObjectOutputStream

       //創建ObjectOutputStream對象
		FileOutputStream fos = new FileOutputStream("src/a.txt");
	
	    ObjectOutputStream oos = new ObjectOutputStream(fos);
	    
	    //寫入磁盤中
	    Student student = new Student(1,"浩哥","男");
	    oos.writeObject(student);
	    //先開的後關
	    oos.close();
	    fos.close();

反對象序列化

反對象序列化讀取存儲介質中的二進制字節序列並轉換成對象的過程。
注意: 對象序列化和反序列化的前提是本類或其超類實現Serializable接口。

ObjectInputStream

         //創建ObjectInputStream對象
	     FileInputStream fileInputStream = new FileInputStream("src/a.txt");
		//寫入磁盤中
         ObjectInputStream ois = new ObjectInputStream(fileInputStream);
		
         Student readObject = (Student) ois.readObject();
         System.out.println(readObject);
         //先開的後關
	    ois.close();
	    fileInputStream.close();

Properties類

向配置文件中寫入配置信息的步驟

        //創建FileOutputStream對象
        FileOutputStream fileOutputStream = new FileOutputStream("src/user.properties");
		//創建Properties對象
        Properties properties = new Properties();
		//通過Properties對象的setProperty方法設置鍵值對信息
        properties.setProperty("username", "lihao");
        properties.setProperty("password", "123456");
		//通過Properties對象的store方法將配置信息寫入磁盤文件
        properties.store(fileOutputStream, "first");
		//關閉流
		fileOutputStream.close();

從配置文件中讀取配置信息的步驟

        //創建FileInputStream對象
        FileInputStream fis = new FileInputStream("src/user.properties");
        //創建Properties對象
        Properties properties = new Properties();
        //通過Properties對象的load方法加載配置文件中的內容
        properties.load(fis);
        //通過Properties對象的get方法獲得配置信息
        String str ="username";
        String value = properties.getProperty(str);
        System.out.println(str+"--------------"+value);
        //關流
        fis.close();

RandomAccessFile類

RandomAccessFile類:是可以訪問文件中任意位置內容的類

RandomAccessFile類讀取文件內容並打印輸出的操作步驟

       //以讀模式創建RandomAccessFile對象
        File file = new File("src/a.txt");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        //移動文件指針到文件內容的中間位置
        randomAccessFile.seek(file.length()/2);
		//通過read方法讀取文件內容
        byte[] b= new byte[1024];
		int i=0;
        while((i=randomAccessFile.read(b))!=-1) {
        	System.out.println(new String(b,0,i));
        }
		//關閉流
        randomAccessFile.close();

IO的分類

阻塞IO是程序發起IO請求便只能等待直到獲得響應結果的IO
非阻塞IO是程序發起IO請求便可進行其他操作的IO
同步IO是程序依賴IO操作返回的結果才能得以繼續執行的IO
異步IO是程序並不依賴IO操作返回的結果便可以繼續執行並會得到IO反饋通知的IO
在這裏插入圖片描述
在這裏插入圖片描述

Charset類

作用: 操作字符集
常用方法
在這裏插入圖片描述

FileChannel類和Buffer類

FileChannel類 是實現了Channel接口,並用於文件讀寫的類
Buffer類 是存儲數據並與通道Channel進行數據交互的類
Buffer類的常用方法
在這裏插入圖片描述
FileChannel類的常用方法
在這裏插入圖片描述
注意:
用NIO進行文件操作時,需要先獲取FileChannel。(NIO是面向緩衝區,非阻塞,同步的IO。)
只能通過FileInputStream,FileOutputStream,RandomAccessFile 來獲取FileChannel。

使用FileChannel+CharBuffer寫入磁盤文件

       //獲取FileChannel對象
		FileOutputStream fos = new FileOutputStream("src/a.txt");
		FileChannel fc = fos.getChannel();
		//爲Buffer緩衝區分配空間,向Buffer中寫入數據,並切換Buffer爲讀模式
		CharBuffer buffer = CharBuffer.allocate(1024);//爲緩衝區分配空間 
		buffer.put("你好 Java");//向緩衝區中寫入字符串
		buffer.flip();//將緩衝區指針移動到首位
		//設置字符集編碼格式,對CharBuffer對象進行編碼
		Charset charset = Charset.forName("UTF-8");//設置字符集編碼格式
		ByteBuffer buffer2 = charset.encode(buffer);//對CharBuffer對象進行編碼
		fc.write(buffer2);//將編碼後的緩衝區內容通過文件通道寫入磁盤文件
        //關閉流
		fc.close();
		fos.close();

使用FileChannel+CharBuffer讀取磁盤文件

        //獲取FileChannel對象
		File file=new File("src/a.txt");
		FileInputStream fis = new FileInputStream("src/a.txt");
		FileChannel fc = fis.getChannel();
        //通過fc的map方法映射文件內容到內存中
		ByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0,  file.length());
        //設置字符集編碼格式,對ByteBuffer緩衝區內容進行解碼
		Charset charset = Charset.forName("UTF-8");
		CharBuffer cb = charset.decode(buffer);
		System.out.println(cb.toString());
        //關閉流
		fc.close();
		fis.close();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章