/** * 3*. 浠庤緭鍏ユ祦涓嫹璐濆唴瀹瑰埌杈撳叆嫻佷腑 * @throws IOException */ public void copyStream(InputStream is, OutputStream os) throws IOException { // 榪欎釜璇昏繃榪囩▼鍙互鍙傞槄 readToBuffer 涓殑娉ㄩ噴 String line; BufferedReader reader = new BufferedReader(new InputStreamReader(is)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(os)); line = reader.readLine(); while (line != null) { writer.println(line); line = reader.readLine(); } writer.flush(); // 鏈鍚庣‘瀹氳鎶婅緭鍑烘祦涓殑涓滆タ閮藉啓鍑哄幓浜?nbsp; // 榪欓噷涓嶅叧闂?writer 鏄洜涓?os 鏄粠澶栭潰浼犺繘鏉ョ殑 // 鏃㈢劧涓嶆槸浠庤繖閲屾墦寮鐨勶紝涔熷氨涓嶄粠榪欓噷鍏抽棴 // 濡傛灉鍏抽棴鐨?writer錛屽皝瑁呭湪閲岄潰鐨?os 涔熷氨琚叧浜?nbsp; }
/** * 3. 璋冪敤 copyStream(InputStream, OutputStream) 鏂規(guī)硶鎷瘋礉鏂囨湰鏂囦歡 */ public void copyTextFile(String inFilename, String outFilename) throws IOException { // 鍏堟牴鎹緭鍏?杈撳嚭鏂囦歡鐢熸垚鐩稿簲鐨勮緭鍏?杈撳嚭嫻?nbsp; InputStream is = new FileInputStream(inFilename); OutputStream os = new FileOutputStream(outFilename); copyStream(is, os); // 鐢?copyStream 鎷瘋礉鍐呭 is.close(); // is 鏄湪榪欓噷鎵撳紑鐨勶紝鎵浠ラ渶瑕佸叧闂?nbsp; os.close(); // os 鏄湪榪欓噷鎵撳紑鐨勶紝鎵浠ラ渶瑕佸叧闂?nbsp; }
public static void main(String[] args) throws IOException { int sw = 1; // 涓夌嫻嬭瘯鐨勯夋嫨寮鍏?nbsp; AccessTextFile test = new AccessTextFile();
switch (sw) { case 1: // 嫻嬭瘯璇?nbsp; { InputStream is = new FileInputStream("E:\\test.txt"); StringBuffer buffer = new StringBuffer(); test.readToBuffer(buffer, is); System.out.println(buffer); // 灝嗚鍒?buffer 涓殑鍐呭鍐欏嚭鏉?nbsp; is.close(); break; } case 2: // 嫻嬭瘯鍐?nbsp; { StringBuffer buffer = new StringBuffer("Only a test\n"); test.writeFromBuffer(buffer, System.out); break; } case 3: // 嫻嬭瘯鎷瘋礉 { test.copyTextFile("E:\\test.txt", "E:\\r.txt"); } break; } }
}
==============
//榪欎釜鏄敤reader鐨勶紝榪欎釜涓嶄細(xì)鍑虹幇寮傚父
import java.io.*; import java.util.*;
public class Test { public static void main(String[] args) throws Exception { File file = new File("Test.java"); BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null;
public class Polymorphism{ public static void main(String[] args) { A b = new B(); b.fb(); } }
class A { public A(){
} public void fa() { System.out.println("CLASS A :Function fa Runing......"); }
public void fb() { System.out.println("CLASS A :Function fb Runing......"); fa(); System.out.println("CLASS A :Function fb Stop......"); } }
class B extends A { public B(){ } public void fa() { System.out.println("CLASS B :Function fa Runing......"); }
public void fb() { System.out.println("CLASS B :Function fb Runing......"); super.fb(); System.out.println("CLASS B :Function fb Stop......"); } }
涓嬮潰鏄畠鐨勮繍琛岀粨鏋滐細(xì)
CLASS B :Function fb Runing...... CLASS A :Function fb Runing...... CLASS B :Function fa Runing...... CLASS A :Function fb Stop...... CLASS B :Function fb Stop......
鎬庝箞鏍鳳紝鐚滃緇撴灉浜嗗悧錛熷鏋滅粨鏋滆窡浣犳兂璞$殑涓妯′竴鏍鳳紝閭d箞鎭枩浣狅紝浣犲澶氭佸凡緇忔湁鍒濇浜嗚В浜嗭紝璧風(fēng)爜鍦ㄨ娉曞眰嬈′笂鏄瘮杈冪啛鎮(zhèn)変簡銆備絾鏄紝鍗冧竾涓嶈“媧嬫磱寰楁剰”錛屼綘鍙惁瑙f瀽緇撴灉涓轟粈涔堜細(xì)鏄繖鏍峰悧錛熸垜浠彲浠ュ厛鏉ユ⒊鐞嗕竴涓嬬▼搴忔祦紼嬶細(xì) 1銆佽繍琛宮ain鍑芥暟錛屽垱寤築瀵硅薄(A b = new B(),瀹屽叏鍙互鏇夸唬B b=new B();)錛岃皟鐢˙鐨勬柟娉昮b錛屼簬鏄墦鍗板嚭"CLASS B :Function fb Runing......"錛岄兘鍦ㄦ儏鐞嗕箣涓?br style="line-height: normal" /> 2銆佹墽琛宻uper.fb()錛岃皟鐢ㄧ埗綾籄鐨勬柟娉昮b錛岄鍏堟墦鍗板嚭"CLASS A :Function fb Runing......"錛岄鏂欎箣涓?br style="line-height: normal" /> 3銆佹墽琛屾柟娉昮a()錛屾墦鍗板嚭"CLASS B :Function fa Runing......"錛屽憙錛熷鎬簡錛屼負(fù)浠涔堜笉鏄墽琛孉鐨勬柟娉昮a(),鑰屾槸瀛愮被B涓殑fa()鍛紵褰撳墠琚墽琛岀殑鏄被A鐨勬柟娉曪紝閭d箞铏氭嫙鏈烘壘鍒扮殑搴旇鏄疉綾葷殑Method Table錛屾壘鍒扮殑搴旇鏄疉綾葷殑鏂規(guī)硶fa()鍟婏紵闅捐В锝?br style="line-height: normal" /> 4銆佹墦鍗?CLASS A :Function fb Stop......"錛岃繑鍥?br style="line-height: normal" /> 5銆佹墦鍗?strong style="line-height: normal">"CLASS A :Function fb Stop....."錛岃繑鍥烇紝紼嬪簭緇撴潫銆?br style="line-height: normal" /> 鐜板湪闂娓呮浜嗭紝灝辨槸铏氭嫙鏈哄湪鎵ц綾籄鏂規(guī)硶鐨勬椂鍊欐煡鎵劇殑Method Table绔熺劧鏄瓙綾籅鐨勩備負(fù)浠涔堝憿錛熷叾瀹烇紝鍙鎴戜滑娓呮java鏂規(guī)硶璋冪敤鐨勬柟寮忥紝榪欎釜闂灝辮繋鍒冭岃В浜嗐傚湪Java铏氭嫙鏈轟腑錛屾瘡鍚姩涓涓柊鐨勭嚎紼嬶紝铏氭嫙鏈洪兘浼?xì)湄?fù)瀹冨垎閰嶄竴涓狫ava鏍堬紝鑰屾瘡褰撶嚎紼嬭皟鐢ㄤ竴涓猨ava鏂規(guī)硶鏃訛紝铏氭嫙鏈哄氨浼?xì)鍦ㄨ绾拷E嬬殑java鏍堜腑鍘嬪叆涓涓柊甯э紝鐢ㄤ互瀛樺偍鍙傛暟錛屽眬閮ㄥ彉閲忕瓑鏁版嵁銆傛垜浠皢榪欎釜姝e湪琚墽琛岀殑鏂規(guī)硶縐頒負(fù)璇ョ嚎紼嬬殑褰撳墠鏂規(guī)硶錛屽叾鐩稿簲鐨勬爤甯т負(fù)褰撳墠甯с?br style="line-height: normal" /> 濂戒簡錛屽綋鎴戜滑璋冪敤涓涓柟娉曟椂錛屾垜浠渶瑕佸線褰撳墠甯т腑鍘嬪叆鍝簺鍙傛暟鍛紵綆鍗曪紝鏂規(guī)硶鐨勫弬鏁板垪琛ㄤ腑涓嶆槸閮借寰楁竻娓呮妤氱殑鍚楋紵鍡紝瀵逛簬C璇█鏉ヨ錛岃繖涓娉曟槸姝g‘鐨勶紝浣嗘槸瀵逛簬璇稿C++錛孞ava錛孭ython絳夐潰鍚戝璞¤璦鏉ヨ錛屽嵈鏄笉瀵圭殑銆?span style="background-color: yellow">澶у榪樿寰楅偅涓?this"鎸囬拡鍚楋紵錛?/span>涓嶉敊錛?strong style="line-height: normal">鍦↗ava涓紝鎵鏈夌殑瀹炰緥鏂規(guī)硶錛圛nstance Method錛夎皟鐢ㄧ殑鏃跺欓兘浼?xì)鎶婂綋鍓嶅璞″帇鍏ュ綋鍓嶅抚涓Q孞ava铏氭嫙鏈烘鏄氳繃榪欎釜鍙傛暟鏉ュ喅瀹氬綋鍓嶆墍浣跨敤鐨勭被錛堥氳繃鍒ゆ柇璇ュ璞$殑綾誨瀷錛?/strong>銆?br style="line-height: normal" /> 鍦ㄤ笂闈㈢殑渚嬪瓙涓紝main涓皟鐢╞.fb()鏃訛紝鍘嬪叆鐨勫綋鍓嶅璞¤嚜鐒舵槸B綾誨璞★紝鎴戜滑璁頒負(fù)b銆傚湪B鐨刦b()涓皟鐢╯uper.fb()鏃訛紝鍘嬪叆鐨勫氨鏄垰鍒氬帇鍏ョ殑瀵硅薄錛屼篃灝辨槸b浜嗐傚悓鏍鳳紝鍦ˋ鐨刦b涓皟鐢╢a()鏃訛紝鍘嬪叆鐨勪篃鏄痓銆傚洜姝わ紝鍦ㄤ嬌鐢?nbsp;invokevirtual鎸囦護(hù)璋冪敤fa()鏃訛紝鎵劇殑灝辨槸B鐨勬柟娉曡〃錛堝綋鍓嶅璞鐨勭被鍨嬩負(fù)B錛夛紝涔熷氨鎵ц浜嗙被B鐨刦a浜嗐?br style="line-height: normal" /> 榪欑鐜拌薄鍦ㄦ瀯閫犲嚱鏁頒腑鐗瑰埆甯歌錛屽洜涓烘瀯閫犲嚱鏁頒腑浼?xì)闅愬惈鋴社敤璋冪敤鐖毒c葷殑鏋勯犲嚱鏁扮殑錛屽鏋滃湪鐖剁被鐨勬瀯閫犲嚱鏁頒腑璋冪敤浜嗗疄渚嬫柟娉曪紙濡?nbsp;A鐨刦a錛夛紝鑰屽湪瀛愮被涓張瑕嗙洊浜嗚繖涓疄渚嬫柟娉曪紙濡?nbsp;B鐨刦a錛夛紝閭d箞寰楀埌鐨勭粨鏋滃線寰涓嶆槸鎴戜滑鎵瑕佺殑銆傚洜姝わ紝鎴戜滑鏈濂戒笉瑕佸湪鏋勯犲嚱鏁頒腑浣跨敤澶氭佹柟娉曪紝涓嶇劧錛孌ebug浼?xì)寰堢棝鑻︾殑锛?xì)錛?/p>
]]>Java涓叏灞鍙橀噺鐨勫疄鐜?/title>http://www.shnenglu.com/luyulaile/archive/2010/12/21/137139.htmlluisluisTue, 21 Dec 2010 12:03:00 GMThttp://www.shnenglu.com/luyulaile/archive/2010/12/21/137139.htmlhttp://www.shnenglu.com/luyulaile/comments/137139.htmlhttp://www.shnenglu.com/luyulaile/archive/2010/12/21/137139.html#Feedback0http://www.shnenglu.com/luyulaile/comments/commentRss/137139.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/137139.htmlimport java.util.*; import java.math.*; publicclass Negotiation { publicstatic final int NUM_MAXTIME=10; /**//**鏈澶ц疆嬈?/span>*/ publicstatic final int MAX_TURN=50; publicstatic final int NUM_ISSUE=3; /**//**鏈澶у鎵嬫暟*/ publicstatic final int MAX_PARTNER=10; /**//**璋堝垽榪囩▼ * */ void negotiationProcess() { } }
]]>[杞澆]Vector閬嶅巻http://www.shnenglu.com/luyulaile/archive/2010/01/26/106444.htmlluisluisTue, 26 Jan 2010 01:08:00 GMThttp://www.shnenglu.com/luyulaile/archive/2010/01/26/106444.htmlhttp://www.shnenglu.com/luyulaile/comments/106444.htmlhttp://www.shnenglu.com/luyulaile/archive/2010/01/26/106444.html#Feedback0http://www.shnenglu.com/luyulaile/comments/commentRss/106444.htmlhttp://www.shnenglu.com/luyulaile/services/trackbacks/106444.htmlimport java.util.Iterator; import java.util.Vector;
public class VectorIterator implements Iterator{
private Vector v;
private int currentIndex=0;
public VectorIterator(){
}
public VectorIterator(Vector v){ this.v=v; }
public boolean hasNext() { if(this.currentIndex<this.v.size()){ System.out.println("current index is : "+this.currentIndex); return true; }else{ System.out.println("out of the bound "); } return false; }
public Object next() { return this.v.get(this.currentIndex++); }
public void remove() { this.v.remove(this.currentIndex); }
public void testMethod(){ v.add("a"); v.add("b"); v.add("c");
System.out.println("\n鍘熸潵鐨剉ecotr"); for(int i=0;i<v.size();i++){ System.out.println("\n"+" index is: "+ i + " element is: " +v.get(i)); }
v.remove(0);
System.out.println("\n鍘繪帀涓涓獀ector涓涓涓厓绱?); for(int i=0;i<v.size();i++){ System.out.println("\n"+" index is: "+ i + " element is: " +v.get(i)); } }
public static void main(String[] args) { test t = new test(); t.testMethod(); System.out.println("Hello World!"); } } -----------------鎺掑簭紼嬪簭------------------------------ import java.util.*;
class MyCompare implements Comparator //瀹炵幇Comparator錛屽畾涔夎嚜宸辯殑姣旇緝鏂規(guī)硶 { public int compare(Object o1, Object o2) { Elem e1=(Elem)o1; Elem e2=(Elem)o2;
public class Vector1 { public static void main(String[] args) { List v = new Vector(); v.add(new Elem(1)); v.add(new Elem(22)); v.add(new Elem(3)); v.add(new Elem(14)); Comparator ct = new MyCompare(); Collections.sort(v, ct); for (int i = 0; i < v.size(); i++) System.out.println(((Elem) v.get(i)).get());