1.闡述BufferedReader和BufferedWriter的工做原理,java
是否緩衝區讀寫器的性能恆大於非緩衝區讀寫器的性能,爲何,請舉例說明?數組
答:
ide
BufferedReader:從字符輸入流讀取文本,緩衝各個字符,從而實現字符、數組和行的高效讀取;性能
BufferedWriter:將文本寫入字符輸出流,緩衝各個字符,從而提供單個字符、駐足和字符串的高效寫入;緩衝區讀寫器的性能恆大於非緩衝區讀寫器的性能。spa
2.闡述HashSet與HashMap的異同。3d
答:orm
HashSet:HashSet集合內部是經過HashMap進行實現的。使用的是HashMap中key部分,實現set接口,set繼承collection接口對象
HashMap;實現Map接口,Map接口與collection接口是同級的。繼承
他們都具備不重複的特色,採用hash機制進行存儲。接口
3.Charset類操做:isSupport()
答:
3.1)經過該類驗證平臺是否支持一下字符集:
gb2312
GB2312
gbk
GBK
utf-8
utf8
iso8859-1
iso-8859-1
3.2)取出平臺默認的字符集
import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; public class CharsetApp { public static void main(String[] args) { /** * 迭代全部字符集名稱 */ for(String key : Charset.availableCharsets().keySet()){ System.out.println(key); } System.out.println(Charset.defaultCharset().name()); Map<String,String> map = new HashMap<String,String>(); map.put("key001", "tom"); map.put("key001", "tomas"); System.out.println(map.get("key001")); } }
4.FileReader.getEncoding();
new String(,,,,,charset);
import java.nio.charset.Charset;
/**
*
*
*/
public class CharsetTest {
public static void main(String[] args) {
// 驗證字符集
System.out.println("gb2312驗證結果:" + Charset.isSupported("gb2312"));
System.out.println("GB2312驗證結果:" + Charset.isSupported("GB2312"));
System.out.println("gbk驗證結果:" + Charset.isSupported("gbk"));
System.out.println("GBK驗證結果:" + Charset.isSupported("GBK"));
System.out.println("utf-8驗證結果:" + Charset.isSupported("utf-8"));
System.out.println("utf8驗證結果:" + Charset.isSupported("utf8"));
System.out.println("iso8859-1驗證結果:" + Charset.isSupported("iso8859-1"));
System.out.println("iso-8859-1驗證結果:" + Charset.isSupported("iso-8859-1"));
// 獲取平臺默認的字符集
Charset defaultCharset = Charset.defaultCharset();
System.out.println("系統默認的字符集:" + defaultCharset.displayName());
}
}
5.使用FileInputStream + FileOutputStream / BufferedInputStream + BufferedOuputStream
實現大文件複製,比較複製效率。
答:
package test_13day做業; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; //使用FileInputStream + FileOutputStream / BufferedInputStream + BufferedOuputStream //實現大文件複製,比較複製效率。 public class teat { public static void main(String[] args) throws IOException { long startTimeMillis = System.currentTimeMillis(); File fsou = new File("D:/w.bibis"); FileInputStreamCopy(startTimeMillis, fsou); BufferedInputStreamCopy(startTimeMillis, fsou); } // 實現文件輸入輸出的時間 private static void FileInputStreamCopy(long startTimeMillis, File fsou) throws IOException { try { if (!fsou.exists()) { fsou.createNewFile(); } File fdec = new File("D:/w.bibis"); FileInputStream fls = new FileInputStream(fsou); FileOutputStream fos = new FileOutputStream(fdec); int readLen = fls.read(); while (readLen != -1) { fos.write(readLen); readLen = fls.read(); } fos.flush(); fls.close(); fos.close(); long endTimeMillis = System.currentTimeMillis(); System.out.println("FileInputStream + FileOutputStream 花費時間:" + (endTimeMillis - startTimeMillis)); } catch (FileNotFoundException e) { e.printStackTrace(); } } private static void BufferedInputStreamCopy(long startTimeMillis, File fsou) { try { if (!fsou.exists()) { fsou.createNewFile(); } File fdec=new File("D:/jiji.txt"); FileInputStream fis=new FileInputStream(fsou); FileOutputStream fos=new FileOutputStream(fdec); BufferedInputStream bis=new BufferedInputStream(fis); BufferedOutputStream bos=new BufferedOutputStream(fos); byte[] buf=new byte[1024]; int readLen=0; while((readLen=bis.read(buf))!=-1){ bos.write(buf, 0, readLen); } bos.close(); bis.close(); fos.close(); fis.close(); long endTimeMillis=System.currentTimeMillis(); System.out.println(" BufferedInputStream + BufferedOuputStream花費時間"+(endTimeMillis - startTimeMillis)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
6.闡述對象回收的前提條件。
答:
1)對象的失去引用
2)對象離開做用域