Effective Java¶ÁÊé±Ê¼Ç
package chape8;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* µÚ°ËÕ ͨÓóÌÐòÉè¼Æ
* µÚ45Ìõ£º ½«¾Ö²¿±äÁ¿µÄ×÷ÓÃÓò¼õСµ½×îС
* ½«¾Ö²¿±äÁ¿µÄ×÷ÓÃÓò×îС»¯¡£¿ÉÒÔÔöÇ¿´úÂëµÄ¿É¶ÁºÍ¿Éά»¤ÐÔ£¬²¢½µµÍ³ö´íµÄ¿ÉÄÜÐÔ
* Ҫʹ×÷ÓÃÓò×îС£¬×îÓÐÁ¦µÄ·½·¨¾ÍÊÇÔÚµÚÒ»´ÎʹÓÃËüµÄʱºòÉùÃ÷£»Èç¹ûÌáÇ°ÉùÃ÷»áÔì³É
* һƬ»ìÂÒ£»Ò²»á·ÖÉ¢×¢ÒâÁ¦
* ¼¸ºõÿ¸ö¾Ö²¿±äÁ¿µÄÉùÃ÷¶¼Ó¦¸Ã°üÀ¨Ò»¸ö³õʼ»¯µÃ±í´ïʽ£»
* ÀýÈ磺
* for(Element e : c)
* {
* dosomthing(e);
* }
* Õâ¸öÊÇjava1.5ÒÔÉϵİ汾µÄforÑ»·µÄÊ×ÏÈ¿¼ÂǵÄд·¨£»
* ¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£
* ÔÙÀ´¿´£ºÏÂÃæÊÇjava1.5ÒÔÇ°µÄforÑ»·
* List c = new ArrayList ();
Iterator it = c.iterator();
while(it.hasNext())
{
it.next();
}
List c1 = new ArrayList();
Iterator it1 = c1.iterator();
while(it.hasNext())
{
it1.next();
}
* µ«Äã±È½ÏÒ»ÏÂÕâÁ½¸öÑ»·£¬µÚ¶þ¸öÑ»·ÊÇÎÒÒªµÄµÚ¶þ¸öÑ»· ÎÒΪÁË·½±ã½«µÚÒ»¸öÑ»·
* ¸´ÖÆÁËÒ»·Ý £¬È»ºó¸Ä±äÁ˱äÁ¿µÄÃû³ÆÀ´´ïµ½Á½¸öÑ»·µÄÄ¿µÄ£¬這樣寫 ide²»會報錯
* µ«ÊÇÏÔÈ»ÎÒÍüÁËÐÞ¸ÄһЩ±ØÐëÐ޸ĵĶ«Î÷£¬µ«ÊǵÚÒ»¸öÑ»·ÖеÄitÔÚµÚ¶þ¸öµÄ×÷ÓÃÓòÖÐÒÀÈ»ÓÐЧ ÕâÑù»áÔì³ÉÄãÈÏΪ
* ĪÃûÆäÃîµÄÎÊÌ⣻
* Èç¹û這樣寫£º
* List c = new ArrayList ();
for (Iterator it = c.iterator();it.hasNext();)
{
it.next();
}
List c1 = new ArrayList();
while(Iterator it1 = c1.iterator();it.hasNext();)
{
it1.next();
}
µÚÒ»個Ñ環ÖеÄ變Á¿µÄ×÷ÓÃÓò²»會Ó°響µ½µÚ¶þ個 這樣ide¾Í會報錯 ¿ÉÒÔ¼°時¸Ä³É£»
將¾Ö²¿變Á¿µÄ×÷ÓÃÓò×îС»¯ ʹ·½·¨Ð¡¶ø¼¯ÖУ¬將ÄÜ·Ö開µÄ·½·¨¶¼·Ö開£»
* @author lee
*
*/
public class Chape45 {
public static void main(String[] args) {
/*List c = new ArrayList ();
Iterator it = c.iterator();
while(it.hasNext())
{
it.next();
}
List c1 = new ArrayList();
Iterator it1 = c1.iterator();
while(it.hasNext())
{
it1
Ïà¹ØÎĵµ£º
Java NIO APIÏê½â
ÔÚJDK
1.4ÒÔÇ°£¬JavaµÄIO²Ù×÷¼¯ÖÐÔÚjava.ioÕâ¸ö°üÖУ¬ÊÇ»ùÓÚÁ÷µÄ×èÈû£¨blocking£©API¡£¶ÔÓÚ´ó¶àÊýÓ¦ÓÃÀ´Ëµ£¬ÕâÑùµÄAPIʹÓúܷ½
±ã£¬È»¶ø£¬Ò»Ð©¶ÔÐÔÄÜÒªÇó½Ï¸ßµÄÓ¦Óã¬ÓÈÆäÊÇ·þÎñ¶ËÓ¦Óã¬ÍùÍùÐèÒªÒ»¸ö¸üΪÓÐЧµÄ·½Ê½À´´¦ÀíIO¡£´ÓJDK 1.4Æð£¬NIO
API×÷Ϊһ¸ö»ùÓÚ»º³åÇø£¬²¢ÄÜÌṩ·Ç×èÈû(non-blo ......
ÔÚJAVAÖУº
1. º¯ÊýÖØÔØÊÇÒ»¸öÀàÖÐÉùÃ÷Á˶à¸öͬÃûµÄ·½·¨£¬µ«Óв»Í¬µÄ²ÎÊý¸öÊýºÍ²ÎÊýÀàÐÍ¡£
2. º¯ÊýÖع¹ÊÇÖ¸ÔÚ×ÓÀàÖÐÉùÃ÷Ó븸ÀàͬÃûµÄ·½·¨£¬´Ó¶ø¸²¸ÇÁ˸¸ÀàµÄ·½·¨¡£Öع¹½â¾öÁË×ÓÀàÓ븸ÀàµÄ²îÒìÎÊÌâ¡£
Àý×ÓÈçÏ£º
//ÖØÔØ
class A {
public d (){};
public d (q){};
public d (q,w){};
}
//ÖØд ......
×ܽáÒ»£º
Java IOµÄÒ»°ãʹÓÃÔÔò£º
Ò»¡¢°´Êý¾ÝÀ´Ô´£¨È¥Ïò£©·ÖÀࣺ
1¡¢ÊÇÎļþ£º FileInputStream, FileOutputStream, FileReader, FileWriter
2¡¢ÊÇbyte[]£ºByteArrayInputStream, ByteArrayOutputStream
3¡¢ÊÇChar[]: CharArrayReader, CharArrayWriter
4¡¢ÊÇString: StringBufferInputStream, StringReader, StringW ......
Îļþ´ò¿ªÊ±ºòÊÇͨ¹ýÎļþÍ·À´È·¶¨ÎļþµÄ±àÂë
StreamReader sr=new StreamReader(@"F:\cym\temp\1.txt");
sr.CurrentEncoding //·µ»Ø±àÂë
ÊÀ½çÉϵĸ÷µØÇø¶¼Óб¾µØµÄÓïÑÔ¡£µØÇø²îÒìÖ±½Óµ¼ÖÂÁËÓïÑÔ»·¾³µÄ²îÒì¡£ÔÚ¿ª·¢Ò»¸ö¹ú¼Ê»¯³ÌÐòµÄ¹ý³ÌÖУ¬´¦ÀíÓïÑÔÎ ......
public List createArrayList(String FilePath,String FileName){
List list =new ArrayList();
FileName=FilePath+"\\"+FileName;
File file=new File(FileName);
String encoding="GBK";//·ÀÖ¹ÂÒÂë Õâ±àÂëÐèÒª¸ù¾Ý×Ô¼ºµÄ²Ù ......