IT十八掌作業_java基礎第十三天_IO

1.闡述BufferedReader和BufferedWriter的工作原理,

  是否緩衝區讀寫器的性能恆大於非緩衝區讀寫器的性能,爲什麼,請舉例說明?


2.闡述HashSet與HashMap的異同。


3.Charset類操作:isSupport()

    3.1)通過該類驗證平臺是否支持一下字符集:

        gb2312

        GB2312

        gbk

        GBK

        utf-8

        utf8

        iso8859-1

        iso-8859-1


    3.2)取出平臺默認的字符集


4.FileReader.getEncoding();

  new String(,,,,,charset);


5.使用FileInputStream + FileOutputStream / BufferedInputStream + BufferedOuputStream

  實現大文件複製,比較複製效率。


6.闡述對象回收的前提條件。


答:

1,

BufferedReader和BufferedWriter底層仍然採用的字符輸入輸出流,只是加入了緩衝技術。

使得在操作字符數據的時候,先將數據輸入輸出到緩衝區中,然後批量輸入輸出到物理文件,從而加快效率,降低磁盤IO壓力。

緩衝區讀寫器的性能一定恆大於非緩衝區讀寫器的性能,因爲批量導入導出避免了流對物理文件的頻繁訪問,從而提高IO性能。

2,

HashSet和HashMap的異同

同:都是集合,可以存儲對象,並且HashSet底層採用的就是HashMap,使用的是HashMap的key部分,此外,兩者都具有不重複的特點,都採用了哈希機制進行存儲。

異:HashSet實現Set接口,Set繼承Collection接口;HashMap實現Map接口,而Collection和Map是同一級的,

3,

package com.it18zhang.day13;


import java.nio.charset.Charset;


public class CharsetDemo {


public static void main(String[] args) {

// //取出支持的字符集

// for(String ch : Charset.availableCharsets().keySet()){

// System.out.println(ch);

// }

// System.out.println("-----------------");

System.out.println("gb2312\t::"+Charset.isSupported("gb2312"));

System.out.println("GB2312\t::"+Charset.isSupported("GB2312"));

System.out.println("gbk\t::"+Charset.isSupported("gbk"));

System.out.println("GBK\t::"+Charset.isSupported("GBK"));

System.out.println("utf-8\t::"+Charset.isSupported("utf-8"));

System.out.println("utf8\t::"+Charset.isSupported("utf8"));

System.out.println("iso8859-1\t::"+Charset.isSupported("iso8859-1"));

System.out.println("iso-8859-1\t::"+Charset.isSupported("iso-8859-1"));

System.out.println("default Charset is\t"+Charset.defaultCharset().displayName());

}



}


4,

package com.it18zhang.day13;


import java.io.FileNotFoundException;

import java.io.FileReader;


public class FileReaderEncodingDemo {


public static void main(String[] args) throws Exception {

FileReader  fr = new FileReader("d:/aa.txt");

System.out.println("文件編碼爲"+fr.getEncoding());

String str = new String("a中國b人".getBytes(),"utf-8");

System.out.println(str);

String str1 = new String("a中國b人".getBytes(),"gbk");

System.out.println(str1);

}


}

5

package com.it18zhang.day13;

import java.io.*;

public class CopyFileDemo {


public static void main(String[] args) throws Exception {

FileInputStream fis = new FileInputStream("d:/Koala.jpg");

FileOutputStream fos = new FileOutputStream("d:/copy_file1.jpg");

BufferedInputStream bufIn = new BufferedInputStream(new FileInputStream("d:/Koala.jpg"));

BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream("d:/copy_file2.jpg"));

Long startTime = System.currentTimeMillis();

int ch = 0;

while((ch=fis.read()) != -1){

fos.write((char)ch);

}

fos.flush();

fis.close();

fos.close();

Long endTime = System.currentTimeMillis(); 

System.out.println("FileInputStream+FileOutputStream花費了\t:"+(endTime-startTime));

startTime = System.currentTimeMillis();

byte[] bytes = new byte[1024];

int len = 0;

while((len = bufIn.read(bytes))!=-1){

bufOut.write(bytes,0,len);

}

bufOut.flush();

bufIn.close();

bufOut.close();

endTime = System.currentTimeMillis();

System.out.println("BufferedInputStream+BufferedOutputStream花費了\t:"+(endTime-startTime));

System.out.println("over");

}


}



6.闡述對象回收的前提條件。

答:當沒有直接或者間接的引用到達對象時,該對象可以被jvm垃圾回收。


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