【基礎】-----IO流的理解

 1.文件讀寫

       (1) java 文件模型
             在硬盤上的文件是byte byte byte存儲的,是數據的集合
     (2)打開文件
            有兩種模式“rw”讀寫  “r”只讀
            RandomAssessFile raf=new RandomAssessFile(file,"rw");
             文件指針,打開文件時,指針在開頭  pointer=0
       (3)寫方法  
             raf.write('s')--->只寫一個字節(後8位),同時指針移向下一個位置,準備再次寫入    
        (4)讀方法
              int b = raf.read()---->只讀一個字節
     (5)文件讀寫完成後一定要關閉

2.IO流

1.字節流
 1)InputStream,OutPutStream
        InputStream抽象了應用程序讀取數據的方式
        OutputStream抽象了應用程序寫數據的方式

 2)EOF=End  讀到-1就是到了結尾
 3)輸入流的基本方法
      int b =in.read();讀取一個字節無符號填充到int低八位
      in.read(byte[] buf)  讀取數據填充到字節數組
      in.read(byte[]buf,int start,int size)讀取數據到字節數組buf。從buf的start位置開始,存放size長度

   4)輸出流基本方法
       out.write(int b)    寫出一個byte到流,b的低八位
       out.write(byte[] buf)    將buf 字節數組都寫入到流
       out.write(byte[] buf,int start,int size)    字節數組buf從start位置開始寫size長度法的字節到流

   5)FileInputStream---->具體實現了在文件上讀取數據

package com.wisdomtraffic.common.MyTest;

import static org.junit.Assert.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.Test;

public class IOUtil {
	/**
	 * 讀取指定文件類容,按照16進制輸出到控制檯
	 * 並每輸出10個byte換行
	 * @param FileName
	 * @throws Exception 
	 */
	private void printHex(String fileName) throws Exception {
		//把文件作爲字節流進行讀操作
		FileInputStream in=new FileInputStream(fileName);
        int b;
        int i=1;
        while ((b=in.read())!=-1) {
//        	System.out.println(b);
			System.out.print(Integer.toHexString(b)+"  ");
			if (i++%10==0) {
				System.out.println();
			}
		}
	}
	/**
	 * 從in中批量讀取字節,放入到buf這個字節數組中,
	 * 從第0個位置開始放,最多放buf.length個
	 * 返回的是讀到的字節的個數。
	 * @param fileName
	 * @throws IOException
	 */
	private void printHexByByteArray(String fileName) throws IOException {
		FileInputStream in =new FileInputStream(fileName);
		byte[] bytes =new byte[1024*3];
		int b=0;
		while ((b=in.read(bytes, 0, bytes.length))!=-1) {
			for (int i = 0; i < b; i++) {    //b:讀到的有效字節的個數
				System.out.println(Integer.toHexString(bytes[i]&0xff)+"   ");//int 32位,byte 8位,爲避免數據轉換出問題,通過按位與將高24位清零
			}
		}
//		int 
	}
	
	@Test
	public void testName() throws Exception {
		printHexByByteArray("c:\\hyl\\hhh.txt");
	}
}

6)FileOutputStream--->實現了在文件上寫出byte數據的方法

	//文件copy
	private void copyFile(File srcFile,File dFile) throws Exception {
		if (!srcFile.exists()) {
			throw new IllegalArgumentException("文件"+srcFile+"不存在");
		}
		FileInputStream in=new FileInputStream(srcFile);
		FileOutputStream out =new FileOutputStream(dFile);//若該文件不存在則創建
		byte[] bytes=new byte[1024];
		int b;    //將字節流讀到bytes數組中
		while((b=in.read(bytes, 0, bytes.length))!=-1){
			out.write(bytes, 0, bytes.length);   //將字節流從bytes數據組寫入新的文件中
			out.flush();
		}
	}

 7)DateOutputStream/DateInputStream----->對“流”功能的擴展,可以更方便的讀取int,long,字符等類型的數據
      DateOutputStream    writeInt()/writeDouble()/writeUtf()......

8)BufferedInputStream&BufferedOutputStream   關閉流後要刷新緩衝區  bos.flush();
   這兩個流類爲IO提供了帶緩衝區的操作,一般打開文件進行寫入或讀取操作時,都會加上緩衝,這種流模式提高了IO的性能
   從應用程序中把數據放入文件,相當於把一缸水倒入另一缸中
   FileOutputStream---->write()方法相當於把水一滴一滴的轉移
   DataUotputStream---->WriteXxx()方法更方便些,相當於一瓢一瓢轉移
   BufferedOutputStream---->write()相當於一瓢水先放入桶中,再從桶倒入另一缸中,性能提高了

2.字符流

1)編碼問題
2)認識文本和文本文件
     java的文本(char)是16位無符號整數,是字符的unicode編碼(雙字節編碼)
     文本是byte byte byte。。的數據數列
      文本文件是文本(char)序列按照某種編碼方案utf-8,gbk等序列化爲byte的存儲結果

3)字符流(Reader,Writer)
     字符的處理:一次處理一個字符,字符的底層仍然是基本的字節序列
      InputStreamReader完成byte流解析爲char流。按照編碼解析
      OutputStreamWriter提供char流到byte流,按照編碼處理

      如需要改變編碼,new InputStream(in,"utf-8")

 

FileWriter/FileReader

參數爲TRUE 追加,不會刪除文件中原有的內容

 

 

寫入文件時不能換行,需要調用newLine()方法換行

對象的序列化與反序列化
1)序列化:將Object轉換成byte序列,反之叫對象的反序列化。
2)序列化流:(ObjectOutputStream)是過濾流---writeObject
      反序列化流:(ObjectInputStream)readObject
3)序列化接口(Serializable)
      對象必須實現序列化接口才能實現序列化,

transient關鍵字:該元素不會進行jvm默認的序列化,也可以自己進行這個元素的序列化

 

   

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章