锘??xml version="1.0" encoding="utf-8" standalone="yes"?> XML鏂囦歡: 璇誨彇XML鍗曚釜鑺傜偣錛坕nputs涓嬬殑columns鑺傜偣錛? 璇誨彇XML inputs鑺傜偣涓嬫墍鏈夌殑input鑺傜偣錛?/p> 鑾峰彇褰撳墠鐨勬湀浠藉拰鏃ユ湡 璇曚簡涓涓?鏋滅劧姝g‘ 鍚庢潵鏌ョ湅java doc鏂囨。,MONTH瀛楁瑙i噴濡備笅 Field number for 榪欎釜瀛楁鐨勫煎彧鏄鏄巊et()鐨勫睘鎬у瓧孌靛?鏉ヨ幏鍙杕onth鐨?/p>
浠ヤ笅涓鴻幏鍙栧叾瀹? Calendar CD = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); //褰撳墠騫?br />
int year = cal.get(Calendar.YEAR); java浜х敓闅忔満鏁扮殑鍑犵鏂瑰紡 浜?鍦╦ava.util榪欎釜鍖呴噷闈㈡彁渚涗簡涓涓猂andom鐨勭被錛屾垜浠彲浠ユ柊寤轟竴涓猂andom鐨勫璞℃潵浜х敓闅忔満鏁幫紝浠栧彲浠ヤ駭鐢熼殢鏈烘暣鏁般侀殢鏈篺loat銆侀殢鏈篸ouble錛岄殢鏈簂ong錛岃繖涓篃鏄垜浠湪j2me鐨勭▼搴忛噷緇忓父鐢ㄧ殑涓涓彇闅忔満鏁扮殑鏂規硶銆?/p>
涓?鍦ㄦ垜浠殑System綾諱腑鏈変竴涓猚urrentTimeMillis()鏂規硶錛岃繖涓柟娉曡繑鍥炰竴涓粠1970騫?鏈?鍙?鐐?鍒?縐掑埌鐩墠鐨勪竴涓縐掓暟錛岃繑鍥炵被鍨嬫槸long錛屾垜浠彲浠ユ嬁浠栦綔涓轟竴涓殢鏈烘暟錛屾垜浠彲浠ユ嬁浠栧涓浜涙暟鍙栨ā錛屽氨鍙互鎶婁粬闄愬埗鍦ㄤ竴涓寖鍥翠箣鍐呭暒 鍏跺疄鍦≧andom鐨勯粯璁ゆ瀯閫犳柟娉曢噷涔熸槸浣跨敤涓婇潰絎笁縐嶆柟娉曡繘琛岄殢鏈烘暟鐨勪駭鐢熺殑 java.util.Random綾繪湁涓ょ鏂瑰紡鏋勫緩鏂瑰紡錛氬甫縐嶅瓙鍜屼笉甯︾瀛?/p>
涓嶅甫縐嶅瓙錛? public class RandomTest { } public static void main(String[] args) { 涓ょ鏂瑰紡鐨勫樊鍒湪浜?/p>
(1) 棣栧厛璇鋒墦寮Java Doc錛屾垜浠細鐪嬪埌Random綾葷殑璇存槑錛?/p>
姝ょ被鐨勫疄渚嬬敤浜庣敓鎴愪吉闅忔満鏁版祦錛屾綾諱嬌鐢?48 浣嶇殑縐嶅瓙錛岃縐嶅瓙鍙互浣跨敤綰挎у悓浣欏叕寮忓鍏惰繘琛屼慨鏀癸紙璇峰弬闃?Donald Knuth 鐨勩奣he Art of Computer Programming, Volume 2銆嬶紝絎?3.2.1 鑺傦級銆? 濡傛灉鐢ㄧ浉鍚岀殑縐嶅瓙鍒涘緩涓や釜 Random 瀹炰緥錛屽垯瀵規瘡涓疄渚嬭繘琛岀浉鍚岀殑鏂規硶璋冪敤搴忓垪錛屽畠浠皢鐢熸垚騫惰繑鍥炵浉鍚岀殑鏁板瓧搴忓垪銆備負浜嗕繚璇佸疄鐜拌繖縐嶇壒鎬э紝鎴戜滑涓虹被Random鎸囧畾浜嗙壒瀹氱殑綆楁硶銆備負浜?Java 浠g爜鐨勫畬鍏ㄥ彲縐繪鎬э紝Java 瀹炵幇蹇呴』璁╃被 Random 浣跨敤姝ゅ鎵紺虹殑鎵鏈夌畻娉曘備絾鏄厑璁?Random 綾葷殑瀛愮被浣跨敤鍏朵粬綆楁硶錛屽彧瑕佸叾絎﹀悎鎵鏈夋柟娉曠殑甯歌鍗忓畾鍗沖彲銆? Java Doc瀵筊andom綾誨凡緇忚В閲婂緱闈炲父鏄庣櫧錛屾垜浠殑嫻嬭瘯涔熼獙璇佷簡榪欎竴鐐廣?/p>
(2) 濡傛灉娌℃湁鎻愪緵縐嶅瓙鏁幫紝Random瀹炰緥鐨勭瀛愭暟灝嗘槸褰撳墠鏃墮棿鐨勬縐掓暟錛屽彲浠ラ氳繃System.currentTimeMillis()鏉ヨ幏寰楀綋鍓嶆椂闂寸殑姣鏁般傛墦寮JDK鐨勬簮浠g爜錛屾垜浠彲浠ラ潪甯告槑紜湴鐪嬪埌榪欎竴鐐廣?/p>
/** random瀵硅薄鐨刵extInt(),nextInt(int n)鏂規硶鐨勮鏄庯細 int nextInt() 銆銆闅忔満鏁板湪瀹為檯涓嬌鐢ㄥ緢騫挎硾錛屾瘮濡傝闅忓嵆鐢熸垚涓涓浐瀹氶暱搴︾殑瀛楃涓層佹暟瀛椼傛垨鑰呴殢鍗崇敓鎴愪竴涓笉瀹氶暱搴︾殑鏁板瓧銆佹垨鑰呰繘琛屼竴涓ā鎷熺殑闅忔満閫夋嫨絳夌瓑銆侸ava鎻愪緵浜嗘渶鍩烘湰鐨勫伐鍏鳳紝鍙互甯姪寮鍙戣呮潵瀹炵幇榪欎竴鍒囥?/p>
銆銆涓銆丣ava闅忔満鏁扮殑浜х敓鏂瑰紡 銆銆鍦↗ava涓紝闅忔満鏁扮殑姒傚康浠庡箍涔変笂灝嗭紝鏈変笁縐嶃?/p>
銆銆1銆侀氳繃System.currentTimeMillis()鏉ヨ幏鍙栦竴涓綋鍓嶆椂闂存縐掓暟鐨刲ong鍨嬫暟瀛椼?/p>
銆銆2銆侀氳繃Math.random()榪斿洖涓涓?鍒?涔嬮棿鐨刣ouble鍊箋?/p>
銆銆3銆侀氳繃Random綾繪潵浜х敓涓涓殢鏈烘暟錛岃繖涓槸涓撲笟鐨凴andom宸ュ叿綾伙紝鍔熻兘寮哄ぇ銆?/p>
銆銆浜屻丷andom綾籄PI璇存槑 銆銆1銆丣ava API璇存槑 銆銆Random綾葷殑瀹炰緥鐢ㄤ簬鐢熸垚浼殢鏈烘暟嫻併傛綾諱嬌鐢?48 浣嶇殑縐嶅瓙錛屼嬌鐢ㄧ嚎鎬у悓浣欏叕寮忓鍏惰繘琛屼慨鏀?璇峰弬闃?Donald Knuth 鐨勩奣he Art of Computer Programming錛?Volume 2銆嬶紝絎?3.2.1 鑺?銆?/p>
銆銆濡傛灉鐢ㄧ浉鍚岀殑縐嶅瓙鍒涘緩涓や釜 Random 瀹炰緥錛屽垯瀵規瘡涓疄渚嬭繘琛岀浉鍚岀殑鏂規硶璋冪敤搴忓垪錛屽畠浠皢鐢熸垚騫惰繑鍥炵浉鍚岀殑鏁板瓧搴忓垪銆備負浜嗕繚璇佸睘鎬х殑瀹炵幇錛屼負綾?Random 鎸囧畾浜嗙壒瀹氱殑綆楁硶銆?/p>
銆銆寰堝搴旂敤紼嬪簭浼氬彂鐜?Math 綾諱腑鐨?random 鏂規硶鏇存槗浜庝嬌鐢ㄣ?/p>
銆銆2銆佹柟娉曟憳瑕?/p>
銆銆Random() 銆銆鍒涘緩涓涓柊鐨勯殢鏈烘暟鐢熸垚鍣ㄣ?/p>
銆銆Random(long seed) 銆銆浣跨敤鍗曚釜 long 縐嶅瓙鍒涘緩涓涓柊闅忔満鏁扮敓鎴愬櫒錛?public Random(long seed) { setSeed(seed); } next 鏂規硶浣跨敤瀹冩潵淇濆瓨闅忔満鏁扮敓鎴愬櫒鐨勭姸鎬併?/p>
銆銆protected int next(int bits) 銆銆鐢熸垚涓嬩竴涓吉闅忔満鏁般?/p>
銆銆boolean nextBoolean() 銆銆榪斿洖涓嬩竴涓吉闅忔満鏁幫紝瀹冩槸浠庢闅忔満鏁扮敓鎴愬櫒鐨勫簭鍒椾腑鍙栧嚭鐨勩佸潎鍖鍒嗗竷鐨?boolean 鍊箋?/p>
銆銆void nextBytes(byte[] bytes) 銆銆鐢熸垚闅忔満瀛楄妭騫跺皢鍏剁疆浜庣敤鎴鋒彁渚涚殑瀛楄妭鏁扮粍涓?/p>
銆銆double nextDouble() 銆銆榪斿洖涓嬩竴涓吉闅忔満鏁幫紝瀹冩槸浠庢闅忔満鏁扮敓鎴愬櫒鐨勫簭鍒椾腑鍙栧嚭鐨勩佸湪 0.0 鍜?1.0涔嬮棿鍧囧寑鍒嗗竷鐨?double 鍊箋?/p>
銆銆float nextFloat() 銆銆榪斿洖涓嬩竴涓吉闅忔満鏁幫紝瀹冩槸浠庢闅忔満鏁扮敓鎴愬櫒鐨勫簭鍒椾腑鍙栧嚭鐨勩佸湪 0.0 鍜?1.0 涔嬮棿鍧囧寑鍒嗗竷鐨?float 鍊箋?/p>
銆銆double nextGaussian() 銆銆榪斿洖涓嬩竴涓吉闅忔満鏁幫紝瀹冩槸浠庢闅忔満鏁扮敓鎴愬櫒鐨勫簭鍒椾腑鍙栧嚭鐨勩佸憟楂樻柉(“姝e父鍦?#8221;)鍒嗗竷鐨?double 鍊鹼紝鍏跺鉤鍧囧兼槸 0.0錛屾爣鍑嗗亸宸槸 1.0銆?/p>
銆銆int nextInt() 銆銆榪斿洖涓嬩竴涓吉闅忔満鏁幫紝瀹冩槸姝ら殢鏈烘暟鐢熸垚鍣ㄧ殑搴忓垪涓潎鍖鍒嗗竷鐨?int 鍊箋?/p>
銆銆int nextInt(int n) 銆銆榪斿洖涓涓吉闅忔満鏁幫紝瀹冩槸浠庢闅忔満鏁扮敓鎴愬櫒鐨勫簭鍒椾腑鍙栧嚭鐨勩佸湪 0(鍖呮嫭)鍜屾寚瀹氬?涓嶅寘鎷?涔嬮棿鍧囧寑鍒嗗竷鐨?int鍊箋?/p>
銆銆long nextLong() 銆銆榪斿洖涓嬩竴涓吉闅忔満鏁幫紝瀹冩槸浠庢闅忔満鏁扮敓鎴愬櫒鐨勫簭鍒椾腑鍙栧嚭鐨勩佸潎鍖鍒嗗竷鐨?long 鍊箋?/p>
銆銆void setSeed(long seed) 銆銆浣跨敤鍗曚釜 long 縐嶅瓙璁劇疆姝ら殢鏈烘暟鐢熸垚鍣ㄧ殑縐嶅瓙銆?/p>
銆銆涓夈丷andom綾諱嬌鐢ㄨ鏄?/p>
銆銆1銆佸甫縐嶅瓙涓庝笉甯︾瀛愮殑鍖哄埆Random綾諱嬌鐢ㄧ殑鏍規湰鏄瓥鐣ュ垎甯︾瀛愬拰涓嶅甫縐嶅瓙鐨凴andom鐨勫疄渚嬨?/p>
銆銆閫氫織璇達紝涓よ呯殑鍖哄埆鏄細甯︾瀛愮殑錛屾瘡嬈¤繍琛岀敓鎴愮殑緇撴灉閮芥槸涓鏍風殑銆?/p>
銆銆涓嶅甫縐嶅瓙鐨勶紝姣忔榪愯鐢熸垚鐨勯兘鏄殢鏈虹殑錛屾病鏈夎寰嬪彲璦銆?/p>
銆銆2銆佸垱寤轟笉甯︾瀛愮殑Random瀵硅薄 銆銆Random random = new Random(); 銆銆3銆佸垱寤轟笉甯︾瀛愮殑Random瀵硅薄鏈変袱縐嶆柟娉曪細 銆銆1) Random random = new Random(555L); 銆銆2) Random random = new Random();random.setSeed(555L); 銆銆鍥涖佹祴璇?/p>
銆銆閫氳繃涓涓緥瀛愯鏄庝笂闈㈢殑鐢ㄦ硶 銆銆import java.util.Random; 銆銆/** 銆銆* Java闅忔満鏁版祴璇?/p>
銆銆* User: leizhimin 銆銆* Date: 2008-11-19 17:52:50 銆銆*/ 銆銆public class TestRandomNum { 銆銆public static void main(String[] args) { 銆銆randomTest(); 銆銆testNoSeed(); 銆銆testSeed1(); 銆銆testSeed2(); 銆銆} 銆銆public static void randomTest() { 銆銆System.out.println("--------------test()--------------"); 銆銆//榪斿洖浠ユ縐掍負鍗曚綅鐨勫綋鍓嶆椂闂淬?/p>
銆銆long r1 = System.currentTimeMillis(); 銆銆//榪斿洖甯︽鍙風殑 double 鍊鹼紝澶т簬鎴栫瓑浜?0.0錛屽皬浜?1.0銆?/p>
銆銆double r2 = Math.random(); 銆銆//閫氳繃Random綾繪潵鑾峰彇涓嬩竴涓殢鏈虹殑鏁存暟 銆銆int r3 = new Random().nextInt(); 銆銆System.out.println("r1 = " + r1); 銆銆System.out.println("r3 = " + r2); 銆銆System.out.println("r2 = " + r3); 銆銆} 銆銆public static void testNoSeed() { 銆銆System.out.println("--------------testNoSeed()--------------"); 銆銆//鍒涘緩涓嶅甫縐嶅瓙鐨勬祴璇昍andom瀵硅薄 銆銆Random random = new Random(); 銆銆for (int i = 0; i < 3; i++) { 銆銆System.out.println(random.nextInt()); 銆銆} 銆銆} 銆銆public static void testSeed1() { 銆銆System.out.println("--------------testSeed1()--------------"); 銆銆//鍒涘緩甯︾瀛愮殑嫻嬭瘯Random瀵硅薄 銆銆Random random = new Random(555L); 銆銆for (int i = 0; i < 3; i++) { 銆銆System.out.println(random.nextInt()); 銆銆} 銆銆} 銆銆public static void testSeed2() { 銆銆System.out.println("--------------testSeed2()--------------"); 銆銆//鍒涘緩甯︾瀛愮殑嫻嬭瘯Random瀵硅薄 銆銆Random random = new Random(); 銆銆random.setSeed(555L); 銆銆for (int i = 0; i < 3; i++) { 銆銆System.out.println(random.nextInt()); 銆銆} 銆銆} 銆銆} 銆銆榪愯緇撴灉錛?/p>
銆銆--------------test()-------------- 銆銆r1 = 1227108626582 銆銆r3 = 0.5324887850155043 銆銆r2 = -368083737 銆銆--------------testNoSeed()-------------- 銆銆809503475 銆銆1585541532 銆銆-645134204 銆銆--------------testSeed1()-------------- 銆銆-1367481220 銆銆292886146 銆銆-1462441651 銆銆--------------testSeed2()-------------- 銆銆-1367481220 銆銆292886146 銆銆-1462441651 銆銆Process finished with exit code 0 銆銆閫氳繃testSeed1()涓巘estSeed2()鏂規硶鐨勭粨鏋滃彲浠ョ湅鍒幫紝涓や釜鎵撳嵃緇撴灉鐩稿悓錛屽洜涓轟粬浠瀛愮浉鍚岋紝鍐嶈繍琛屼竴嬈★紝緇撴灉榪樻槸涓鏍風殑錛岃繖灝辨槸甯︾瀛愰殢鏈烘暟鐨勭壒鎬с?/p>
銆銆鑰屼笉甯︾瀛愮殑錛屾瘡嬈¤繍琛岀粨鏋滈兘鏄殢鏈虹殑銆?/p>
銆銆浜斻佺患鍚堝簲鐢?/p>
銆銆涓嬮潰閫氳繃鏈榪戝啓鐨勪竴涓殢鏈烘暟宸ュ叿綾繪潵灞曠ず鐢ㄦ硶錛?/p>
銆銆import java.util.Random; 銆銆/** 銆銆* 闅忔満鏁般侀殢鍗沖瓧絎︿覆宸ュ叿 銆銆* User: leizhimin 銆銆* Date: 2008-11-19 9:43:09 銆銆*/ 銆銆public class RandomUtils { 銆銆public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 銆銆public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 銆銆public static final String numberChar = "0123456789"; 銆銆/** 銆銆* 榪斿洖涓涓畾闀跨殑闅忔満瀛楃涓?鍙寘鍚ぇ灝忓啓瀛楁瘝銆佹暟瀛? 銆銆* 銆銆* @param length 闅忔満瀛楃涓查暱搴?/p>
銆銆* @return 闅忔満瀛楃涓?/p>
銆銆*/ 銆銆public static String generateString(int length) { 銆銆StringBuffer sb = new StringBuffer(); 銆銆Random random = new Random(); 銆銆for (int i = 0; i < length; i++) { 銆銆sb.append(allChar.charAt(random.nextInt(allChar.length()))); 銆銆} 銆銆return sb.toString(); 銆銆} 銆銆/** 銆銆* 榪斿洖涓涓畾闀跨殑闅忔満綰瓧姣嶅瓧絎︿覆(鍙寘鍚ぇ灝忓啓瀛楁瘝) 銆銆* 銆銆* @param length 闅忔満瀛楃涓查暱搴?/p>
銆銆* @return 闅忔満瀛楃涓?/p>
銆銆*/ 銆銆public static String generateMixString(int length) { 銆銆StringBuffer sb = new StringBuffer(); 銆銆Random random = new Random(); 銆銆for (int i = 0; i < length; i++) { 銆銆sb.append(allChar.charAt(random.nextInt(letterChar.length()))); 銆銆} 銆銆return sb.toString(); 銆銆} 銆銆/** 銆銆* 榪斿洖涓涓畾闀跨殑闅忔満綰ぇ鍐欏瓧姣嶅瓧絎︿覆(鍙寘鍚ぇ灝忓啓瀛楁瘝) 銆銆* 銆銆* @param length 闅忔満瀛楃涓查暱搴?/p>
銆銆* @return 闅忔満瀛楃涓?/p>
銆銆*/ 銆銆public static String generateLowerString(int length) { 銆銆return generateMixString(length).toLowerCase(); 銆銆} 銆銆/** 銆銆* 榪斿洖涓涓畾闀跨殑闅忔満綰皬鍐欏瓧姣嶅瓧絎︿覆(鍙寘鍚ぇ灝忓啓瀛楁瘝) 銆銆* 銆銆* @param length 闅忔満瀛楃涓查暱搴?/p>
銆銆* @return 闅忔満瀛楃涓?/p>
銆銆*/ 銆銆public static String generateUpperString(int length) { 銆銆return generateMixString(length).toUpperCase(); 銆銆} 銆銆/** 銆銆* 鐢熸垚涓涓畾闀跨殑綰?瀛楃涓?/p>
銆銆* 銆銆* @param length 瀛楃涓查暱搴?/p>
銆銆* @return 綰?瀛楃涓?/p>
銆銆*/ 銆銆public static String generateZeroString(int length) { 銆銆StringBuffer sb = new StringBuffer(); 銆銆for (int i = 0; i < length; i++) { 銆銆sb.append('0'); 銆銆} 銆銆return sb.toString(); 銆銆} 銆銆/** 銆銆* 鏍規嵁鏁板瓧鐢熸垚涓涓畾闀跨殑瀛楃涓詫紝闀垮害涓嶅鍓嶉潰琛? 銆銆* 銆銆* @param num 鏁板瓧 銆銆* @param fixdlenth 瀛楃涓查暱搴?/p>
銆銆* @return 瀹氶暱鐨勫瓧絎︿覆 銆銆*/ 銆銆public static String toFixdLengthString(long num, int fixdlenth) { 銆銆StringBuffer sb = new StringBuffer(); 銆銆String strNum = String.valueOf(num); 銆銆if (fixdlenth - strNum.length() >= 0) { 銆銆sb.append(generateZeroString(fixdlenth - strNum.length())); 銆銆} else { 銆銆throw new RuntimeException("灝嗘暟瀛? + num + "杞寲涓洪暱搴︿負" + fixdlenth + "鐨勫瓧絎︿覆鍙戠敓寮傚父!"); 銆銆} 銆銆sb.append(strNum); 銆銆return sb.toString(); 銆銆} 銆銆/** 銆銆* 鏍規嵁鏁板瓧鐢熸垚涓涓畾闀跨殑瀛楃涓詫紝闀垮害涓嶅鍓嶉潰琛? 銆銆* 銆銆* @param num 鏁板瓧 銆銆* @param fixdlenth 瀛楃涓查暱搴?/p>
銆銆* @return 瀹氶暱鐨勫瓧絎︿覆 銆銆*/ 銆銆public static String toFixdLengthString(int num, int fixdlenth) { 銆銆StringBuffer sb = new StringBuffer(); 銆銆String strNum = String.valueOf(num); 銆銆if (fixdlenth - strNum.length() >= 0) { 銆銆sb.append(generateZeroString(fixdlenth - strNum.length())); 銆銆} else { 銆銆throw new RuntimeException("灝嗘暟瀛? + num + "杞寲涓洪暱搴︿負" + fixdlenth + "鐨勫瓧絎︿覆鍙戠敓寮傚父!"); 銆銆} 銆銆sb.append(strNum); 銆銆return sb.toString(); 銆銆} 銆銆public static void main(String[] args) { 銆銆System.out.println(generateString(15)); 銆銆System.out.println(generateMixString(15)); 銆銆System.out.println(generateLowerString(15)); 銆銆System.out.println(generateUpperString(15)); 銆銆System.out.println(generateZeroString(15)); 銆銆System.out.println(toFixdLengthString(123, 15)); 銆銆System.out.println(toFixdLengthString(123L, 15)); 銆銆} 銆銆} 銆銆榪愯緇撴灉錛?/p>
銆銆vWMBPiNbzfGCpHG 銆銆23hyraHdJkKPwMv 銆銆tigowetbwkm1nde 銆銆BPZ1KNEJPHB115N 銆銆000000000000000 銆銆000000000000123 銆銆000000000000123 銆銆Process finished with exit code 0 銆銆鍏佹葷粨 銆銆1銆侀殢鏈烘暟寰堝父鐢紝鍦↗ava鏈変笁縐嶄駭鐢熸柟寮忥紝浠andom闅忔満鏁扮殑浣跨敤鏈涓哄鏉傘?/p>
銆銆2銆丷andom綾誨璞℃湁鏄惁甯︾瀛愪箣鍒嗭紝甯︾瀛愮殑鍙縐嶅瓙鐩稿悓錛屽嬈¤繍琛岋紝鐢熸垚闅忔満鏁扮殑緇撴灉鎬繪槸閭f牱銆?/p>
銆銆3銆佸甫縐嶅瓙闅忔満鏁扮殑甯︾瀛愮殑瀵硅薄鍒涘緩鏂瑰紡鏈変袱縐嶏紝鏁堟灉涓鏍楓備絾鏄甫縐嶅瓙鐨勯殢鏈烘暟鐢ㄥ浼間箮涓嶅ぇ銆?/p>
銆銆4銆丷andom鐨勫姛鑳芥兜鐩栦簡Math.random()鐨勫姛鑳姐?/p>
銆銆5銆佸彲浠ラ氳繃闅忔満鏁板幓鍋氬疄鐜伴殢鏈哄瓧絎︿覆絳夊鏉傜殑闅忔満鏁版嵁銆?/p>
銆銆6銆佷笉瑕佺爺絀朵笉閲嶅鐨勯殢鏈烘暟錛屾剰涔変笉澶с?/p>
鍦↗ava 涓垜浠彲浠ヤ嬌鐢╦ava.util.Random綾繪潵浜х敓涓涓殢鏈烘暟鍙戠敓鍣ㄣ傚畠鏈変袱縐嶅艦寮忕殑鏋勯犲嚱鏁幫紝鍒嗗埆鏄疪andom()鍜孯andom(long seed)銆俁andom()浣跨敤褰撳墠鏃墮棿鍗砈ystem.currentTimeMillis()浣滀負鍙戠敓鍣ㄧ殑縐嶅瓙錛孯andom(long seed)浣跨敤鎸囧畾鐨剆eed浣滀負鍙戠敓鍣ㄧ殑縐嶅瓙銆?br />
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
/**
* 璇誨彇鏈夊懡鍚嶇┖闂寸殑XML錛堟敮鎸佽妭鐐硅鍙栦笉瀹屾暣錛?br />
* @author Ai Bo
* @version 1.3.1 Date 2011-12-14
*/
public class Dom4jFactory {
private SAXReader reader = new SAXReader();
private Document document;
private static Element root;
private static Map<String, String> xmlMap = new HashMap<String, String>();
public Dom4jFactory(String path) {
super();
try {
document = reader.read(path);
root = document.getRootElement();
String defaultNamespace = root.getNamespaceURI();
xmlMap.put("default", defaultNamespace);
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static List<?> getSelectNodes(String arg0) {
XPath selector = root.createXPath("//default:" + arg0);
selector.setNamespaceURIs(xmlMap);
return selector.selectNodes(root);
}
@SuppressWarnings("unchecked")
public <T> T getSelectObject(String arg0){
List<?> selectNodes = getSelectNodes(arg0);
if(selectNodes.size()==1){
return (T) selectNodes.get(0);
}else{
return null;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<inputs xmlns="http://input.abio.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://input.abio.com.cn http://www.abio.com.cn/sproinput.xsd">
<columns>2</columns>
<input id="id" datatype="String" minlen="1" maxlen="32" casetype="U" width="40" name_zh="鏁版嵁浠g爜" inputtype="text" editable="2" gridwidth="3"></input>
<input id="organNo" datatype="integer" nullable="false" minlen="1" maxlen="20" casetype="U" width="40" name_zh="鏈烘瀯緙栧彿" inputtype="text" editable="2"></input>
<input id="organName" datatype="String" nullable="false" minlen="1" maxlen="20" casetype="U" width="40" name_zh="鏈烘瀯鍚嶇О" inputtype="text" editable="2"></input>
<input id="pOrganNo" datatype="String" nullable="false" minlen="1" maxlen="20" casetype="U" width="40" name_zh="鐖舵満鏋勭紪鍙? inputtype="text" editable="2"></input>
<input id="effTime" datatype="Date" validator="Date" nullable="false" format="yyyy-MM-dd" minlen="8" maxlen="8" casetype="U" width="40" name_zh="鏈夋晥鏃ユ湡" inputtype="date" editable="2"></input>
<input id="createUser" datatype="String" minlen="1" maxlen="20" casetype="U" width="40" name_zh="鍒涘緩浜? inputtype="text" editable="2"></input>
<input id="createTime" datatype="Date" minlen="10" maxlen="20" casetype="U" width="40" name_zh="鍒涘緩鏃ユ湡" editable="2" inputtype="text"></input>
<input id="updateUser" datatype="String" minlen="1" maxlen="20" casetype="U" width="40" name_zh="鏇存柊浜? inputtype="text" editable="2"></input>
<input id="updateTime" datatype="Date" minlen="10" maxlen="20" casetype="U" width="40" name_zh="鏇存柊鏃ユ湡" editable="2" inputtype="text"></input>
</inputs>
Dom4jFactory df = new Dom4jFactory("c:\\test.xml");
DefaultElement columnsitem = df.getSelectObject("columns");
List<DefaultElement> inputNodes = df.getSelectNodes("input");
]]>
-------------------------
http://java.sun.com/j2se/1.3/download.html
http://java.sun.com/j2se/1.4.2/download.html
http://java.sun.com/javase/downloads/index_jdk5.jsp
http://java.sun.com/javase/downloads/index.jsp
J2EE DK & API涓嬭澆
-------------------------
http://java.sun.com/j2ee/1.3/index.jsp
http://java.sun.com/j2ee/1.3/download.html
http://java.sun.com/j2ee/1.4/index.jsp
http://java.sun.com/j2ee/1.4/download.html
http://java.sun.com/javaee/downloads/index.jsp
JDK1.6API涓枃鐗堬紙鍏級
-------------------------
* HTML 鏍煎紡(鍦ㄧ嚎鑻辨枃) http://java.sun.com/javase/6/docs/
* HTML 鏍煎紡(鍦ㄧ嚎涓枃) http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html/zh_CN/api/index.html
* zip 鏍煎紡(涓枃) http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html_zh_CN.zip
* CHM 鏍煎紡(涓枃) http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/chm/JDK_API_1_6_zh_CN.CHM
JDK1.5API涓枃鐗堬紙鍏級
-------------------------
* HTML 鏍煎紡(鍦ㄧ嚎鑻辨枃) http://java.sun.com/javase/5/docs/
* HTML 鏍煎紡(鍦ㄧ嚎涓枃) http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/index.html
* zip 鏍煎紡(涓枃) http://gceclub.sun.com.cn/Java_Docs/html_zh_CN.zip
* CHM 鏍煎紡(涓枃) http://download.java.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/JDK_API_1_5_zh_CN.CHM
鐩稿叧緗戠珯
-------------------------
http://java.sun.com
http://gceclub.sun.com.cn/
http://developers.sun.com/downloads/
http://java.sun.com/javaee/downloads/
http://java.sun.com/javase/downloads/
http://www.netbeans.info/downloads/
]]>
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipCompressor {
static final int BUFFER = 8192;
private File zipFile;
public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "涓嶅瓨鍦紒");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void compress(File file, ZipOutputStream out, String basedir) {
/* 鍒ゆ柇鏄洰褰曡繕鏄枃浠?nbsp;*/
if (file.isDirectory()) {
System.out.println("鍘嬬緝錛?/span>" + basedir + file.getName());
this.compressDirectory(file, out, basedir);
} else {
System.out.println("鍘嬬緝錛?/span>" + basedir + file.getName());
this.compressFile(file, out, basedir);
}
}
/** 鍘嬬緝涓涓洰褰?nbsp;*/
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 閫掑綊 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}
/** 鍘嬬緝涓涓枃浠?nbsp;*/
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**娓│鏄惁鍙互澹撶府*/
public static void main(String[] args) {
ZipCompressor zc = new ZipCompressor("E:\\szhzip.zip");
zc.compress("E:\\test");
System.out.println("OK.");
}
}
]]>
銆銆public class object {
銆銆public static void main(String[] args) {
銆銆String str1 = new String("abcde");
銆銆String str2 = new String("abcde");
銆銆String str3 = new String("abcde");
銆銆String str4 = new String("abcde");
銆銆String str5 = new String("abcde");
銆銆List list = new ArrayList();
銆銆list.add(str1);
銆銆list.add(str2);
銆銆list.add(str3);
銆銆list.add(str4);
銆銆list.add(str5);
銆銆System.out.println("list.size()=" + list.size());
銆銆for (int i = 0; i < list.size(); i++) {
銆銆if (((String) list.get(i)).startsWith("abcde")) {
銆銆list.remove(i);
銆銆}
銆銆榪愯緇撴灉涓嶆槸錛?br />
銆銆list.size()=5
銆銆after remove:list.size()=0
銆銆灞呯劧鏄細
銆銆list.size()=5
銆銆after remove:list.size()=2
銆銆鍘熷洜錛歀ist姣弐emove鎺変竴涓厓绱犱互鍚庯紝鍚庨潰鐨勫厓绱犻兘浼氬悜鍓嶇Щ鍔紝姝ゆ椂濡傛灉鎵цi=i+1錛屽垯鍒氬垰縐昏繃鏉ョ殑鍏冪礌娌℃湁琚鍙栥?br />
銆銆瑙e喅鏂規硶錛?br />
銆銆1.鍊掕繃鏉ラ亶鍘唋ist
銆銆for (int i = list.size()-1; i > =0; i--) {
銆銆if (((String) list.get(i)).startsWith("abcde")) {
銆銆list.remove(i);
銆銆}
銆銆}
銆銆2.姣忕Щ闄や竴涓厓绱犱互鍚庡啀鎶奿縐誨洖鏉?br />
銆銆for (int i = 0; i < list.size(); i++) {
銆銆if (((String) list.get(i)).startsWith("abcde")) {
銆銆list.remove(i);
銆銆i=i-1;
銆銆}
銆銆}
銆銆3.浣跨敤iterator.remove()鏂規硶鍒犻櫎
銆銆for (Iterator it = list.iterator(); it.hasNext();) {
銆銆String str = (String)it.next();
銆銆if (str.equals("chengang")){
銆銆it.remove();
銆銆}
銆銆}
銆銆}
銆銆System.out.println("after remove:list.size()=" + list.size());
銆銆}
銆銆}
]]>
2.package com;
3.
4.import java.io.FileOutputStream;
5.
6.
7.
8.public class ToImg {
9. public static void main(String[] args) throws Exception {
10. toSmaillImg("untitled.bmp","thumb.bmp");
11. }
12.
13. public static void toSmaillImg(String filePath,String thumbPath) throws Exception{
14. String newurl =thumbPath;
15. java.awt.Image bigJpg = javax.imageio.ImageIO.read(new java.io.File(filePath));
16. float tagsize = 100;
17. int old_w = bigJpg.getWidth(null);
18. int old_h = bigJpg.getHeight(null);
19. int new_w = 0;
20. int new_h = 0;
21. float tempdouble;
22. tempdouble = old_w > old_h ? old_w/tagsize : old_h/tagsize;
23. new_w = Math.round(old_w/tempdouble);
24. new_h = Math.round(old_h/tempdouble);
25. java.awt.image.BufferedImage tag = new java.awt.image.BufferedImage(new_w,new_h,java.awt.image.BufferedImage.TYPE_INT_RGB);
26. tag.getGraphics().drawImage(bigJpg,0,0,new_w,new_h,null);
27. FileOutputStream newimage = new FileOutputStream(newurl);
28. com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(newimage);
29. encoder.encode(tag);
30. newimage.close();
31. }
32.}
]]>
鏈緥瀛愪嬌鐢ㄤ簡jspsmart緇勪歡榪涜涓婁紶錛岃繖閲屽彲浠ュ厤璐逛笅杞借緇勪歡www.jspsmart.com
涓嬭澆瑙e帇鍚庯紝灝唈ar鍖呭鍒跺埌銆\WEB-INF\lib銆鐩綍鍚庨噸鍚湇鍔″櫒錛宩spsmart鍗沖彲姝e父浣跨敤浜?br />
1銆乽ploadimage.jsp
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*,cn.oof.database.*,cn.oof.house.*"%>
<%
SmartUpload mySmartUpload =new SmartUpload();
long file_size_max=4000000;
String fileName2="",ext="",testvar="";
String url="uploadfile/images/"; //搴斾繚璇佸湪鏍圭洰褰曚腑鏈夋鐩綍鐨勫瓨鍦?br />
//鍒濆鍖?/span>
mySmartUpload.initialize(pageContext);
//鍙厑璁鎬笂杞芥綾繪枃浠?/span>
try {
mySmartUpload.setAllowedFilesList("jpg,gif");
//涓婅澆鏂囦歡
mySmartUpload.upload();
} catch (Exception e){
%>
<SCRIPT language=javascript>
alert("鍙厑璁鎬笂浼?jpg鍜?gif綾誨瀷鍥劇墖鏂囦歡");
window.location='upfile.jsp';
</script>
<%
}
try{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (myFile.isMissing()){%>
<SCRIPT language=javascript>
alert("璇峰厛閫夋嫨瑕佷笂浼犵殑鏂囦歡");
window.location='upfile.jsp';
</script>
<%}
else{
//String myFileName=myFile.getFileName(); //鍙栧緱涓婅澆鐨勬枃浠剁殑鏂囦歡鍚?/span>
ext= myFile.getFileExt(); //鍙栧緱鍚庣紑鍚?/span>
int file_size=myFile.getSize(); //鍙栧緱鏂囦歡鐨勫ぇ灝?nbsp;
String saveurl="";
if(file_size<file_size_max){
//鏇存敼鏂囦歡鍚嶏紝鍙栧緱褰撳墠涓婁紶鏃墮棿鐨勬縐掓暟鍊?/span>
Calendar calendar = Calendar.getInstance();
String filename = String.valueOf(calendar.getTimeInMillis());
saveurl=request.getRealPath("/")+url;
saveurl+=filename+"."+ext; //淇濆瓨璺緞
myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
//out.print(filename);
//-----------------------涓婁紶瀹屾垚錛屽紑濮嬬敓鎴愮緝鐣ュ浘-------------------------
java.io.File file = new java.io.File(saveurl); //璇誨叆鍒氭墠涓婁紶鐨勬枃浠?/span>
String newurl=request.getRealPath("/")+url+filename+"_min."+ext; //鏂扮殑緙╃暐鍥句繚瀛樺湴鍧
Image src = javax.imageio.ImageIO.read(file); //鏋勯營mage瀵硅薄
float tagsize=200;
int old_w=src.getWidth(null); //寰楀埌婧愬浘瀹?/span>
int old_h=src.getHeight(null);
int new_w=0;
int new_h=0; //寰楀埌婧愬浘闀?/span>
int tempsize;
float tempdouble;
if(old_w>old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
new_w=Math.round(old_w/tempdouble);
new_h=Math.round(old_h/tempdouble);//璁$畻鏂板浘闀垮
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //緇樺埗緙╁皬鍚庣殑鍥?/span>
FileOutputStream newimage=new FileOutputStream(newurl); //杈撳嚭鍒版枃浠舵祦
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //榪慗PEG緙栫爜
newimage.close();
}
else{
out.print("<SCRIPT language='javascript'>");
out.print("alert('涓婁紶鏂囦歡澶у皬涓嶈兘瓚呰繃"+(file_size_max/1000)+"K');");
out.print("window.location='upfile.jsp;'");
out.print("</SCRIPT>");
}
}
}catch (Exception e){
e.toString();
}
%>
2 upload.htm
<html>
<head>
<title>璇烽夋嫨涓婁紶鐨勫浘鐗?/span></title>
</head>
<body>
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="45" align="center" valign="middle"><form action="uploadimage.jsp" method="post" enctype="multipart/form-data" name="form1">
璇烽夋嫨涓婁紶鐨勫浘鐗?br />
<input type="file" name="file">
<input type="submit" name="Submit" value="涓婁紶">
</form></td>
</tr>
</table>
</body>
</html>
鏉ユ簮:http://www.moon-soft.com/doc/49683.htm
]]>
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//浠婂ぉ鐨勬棩鏈?nbsp;
calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//璁╂棩鏈熷姞1
System.out.println(calendar.get(Calendar.DATE));//鍔?涔嬪悗鐨勬棩鏈烼op
2.鐢╦ava.text.SimpleDateFormat鍜宩ava.util.Date鏉ュ疄鐜?br />
Date d=new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("浠婂ぉ鐨勬棩鏈燂細"+df.format(d));
System.out.println("涓ゅぉ鍓嶇殑鏃ユ湡錛?/span>" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("涓夊ぉ鍚庣殑鏃ユ湡錛?/span>" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));
]]>
java.text.DecimalFormat df = new java.text.DecimalFormat("00.00");//""00.00"灝忔暟鐐瑰悗闈㈢殑0鐨勪釜鏁拌〃紺哄皬鏁扮偣鐨勪釜鏁?/span>
String b = df.format(a);
------鎵撳嵃緇撴灉-----------------
1123454.6789
]]>
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
String dateStr1="2008-3-16";
String dateStr2="2008-3-24";
java.util.Date dateBegin=formater.parse(dateStr1);
java.util.Date dateEnd=formater.parse(dateStr2);
Calendar ca=Calendar.getInstance();
while(dateBegin.compareTo(dateEnd)<=0){
ca.setTime(dateBegin);
ca.add(ca.DATE,1);//鎶奷ateBegin鍔犱笂1澶╃劧鍚庨噸鏂拌祴鍊肩粰date1
dateBegin=ca.getTime();
}
2銆丣ava鑾峰彇褰撴湀鐨勭涓澶╁拰鏈鍚庝竴澶?br />
Calendar cal = Calendar.getInstance();
SimpleDateFormat datef=new SimpleDateFormat("yyyy-MM-dd");
//褰撳墠鏈堢殑鏈鍚庝竴澶?nbsp;
cal.set( Calendar.DATE, 1 );
cal.roll(Calendar.DATE, - 1 );
Date endTime=cal.getTime();
String endTime1=datef.format(endTime)+" 23:59:59";
//褰撳墠鏈堢殑絎竴澶?nbsp;
cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
Date beginTime=cal.getTime();
String beginTime1=datef.format(beginTime)+" 00:00:00";
]]>
]]>
]]>
int day = date.get(Calendar.DAY_OF_MONTH);
get
and set
indicating the month. This is a calendar-specific value. The first month of the year is JANUARY
which is 0; the last depends on the number of months in a year.
int YY = CD.get(Calendar.YEAR) ;
int MM = CD.get(Calendar.MONTH)+1;
int DD = CD.get(Calendar.DATE);
int HH = CD.get(Calendar.HOUR);
int NN = CD.get(Calendar.MINUTE);
int SS = CD.get(Calendar.SECOND);
int MI = CD.get(Calendar.MILLISECOND);
//褰撳墠鏈?br />
int month = (cal.get(Calendar.MONTH))+1;
//褰撳墠鏈堢殑絎嚑澶╋細鍗沖綋鍓嶆棩
int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
//褰撳墠鏃訛細HOUR_OF_DAY-24灝忔椂鍒訛紱HOUR-12灝忔椂鍒?br />
int hour = cal.get(Calendar.HOUR_OF_DAY);
//褰撳墠鍒?br />
int minute = cal.get(Calendar.MINUTE);
//褰撳墠縐?br />
int second = cal.get(Calendar.SECOND);
//0-涓婂崍錛?-涓嬪崍
int ampm = cal.get(Calendar.AM_PM);
//褰撳墠騫寸殑絎嚑鍛?br />
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
//褰撳墠鏈堢殑絎嚑鍛?br />
int week_of_month = cal.get(Calendar.WEEK_OF_MONTH);
//褰撳墠騫寸殑絎嚑澶?br />
int day_of_year = cal.get(Calendar.DAY_OF_YEAR);
]]>
(鏁版嵁綾誨瀷)(鏈灝忓?Math.random()*(鏈澶у?鏈灝忓?1))
渚?
(int)(1+Math.random()*(10-1+1))
浠?鍒?0鐨刬nt鍨嬮殢鏁?/font>
鑾峰緱闅忔満鏁?br />
for (int i=0;i<30;i++)
{System.out.println((int)(1+Math.random()*10));}
(int)(1+Math.random()*10)
閫氳繃java.Math鍖呯殑random鏂規硶寰楀埌1-10鐨刬nt闅忔満鏁?br />
鍏紡鏄?鏈灝忓?--鏈澶у鹼紙鏁存暟錛夌殑闅忔満鏁?br />
錛堢被鍨嬶級鏈灝忓?Math.random()*鏈澶у?/font>
Random ra =new Random();
for (int i=0;i<30;i++)
{System.out.println(ra.nextInt(10)+1);}
閫氳繃java.util鍖呬腑鐨凴andom綾葷殑nextInt鏂規硶鏉ュ緱鍒?-10鐨刬nt闅忔満鏁?/font>
涓.鍦╦2se閲屾垜浠彲浠ヤ嬌鐢∕ath.random()鏂規硶鏉ヤ駭鐢熶竴涓殢鏈烘暟錛岃繖涓駭鐢熺殑闅忔満鏁版槸0-1涔嬮棿鐨勪竴涓猟ouble錛屾垜浠彲浠ユ妸浠栦箻浠ヤ竴瀹氱殑鏁幫紝姣斿璇翠箻浠?00錛屼粬灝辨槸涓?00浠ュ唴鐨勯殢鏈猴紝榪欎釜鍦╦2me涓病鏈夈?/p>
瀵逛簬鏂規硶浜屼腑鐨凴andom綾繪湁浠ヤ笅璇存槑錛?/p>
姝ょ鏂瑰紡灝嗕細榪斿洖闅忔満鐨勬暟瀛楋紝姣忔榪愯緇撴灉涓嶄竴鏍?/p>
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
for(int i=0;i<10;i++){
System.out.println(r.nextInt());
}
甯︾瀛愶細
姝ょ鏂瑰紡錛屾棤璁虹▼搴忚繍琛屽灝戞錛岃繑鍥炵粨鏋滈兘鏄竴鏍風殑
java.util.Random r=new java.util.Random(10);
for(int i=0;i<10;i++){
System.out.println(r.nextInt());
}
}
* Creates a new random number generator. Its seed is initialized to
* a value based on the current time:
* Random() { this(System.currentTimeMillis()); }java.lang.System#currentTimeMillis()
*/
public Random() { this(System.currentTimeMillis()); }
鍙﹀錛?
榪斿洖涓嬩竴涓吉闅忔満鏁幫紝瀹冩槸姝ら殢鏈烘暟鐢熸垚鍣ㄧ殑搴忓垪涓潎鍖鍒嗗竷鐨?int 鍊箋?
int nextInt(int n)
榪斿洖涓涓吉闅忔満鏁幫紝瀹冩槸浠庢闅忔満鏁扮敓鎴愬櫒鐨勫簭鍒椾腑鍙栧嚭鐨勩佸湪 0錛堝寘鎷級鍜屾寚瀹氬鹼紙涓嶅寘鎷級涔嬮棿鍧囧寑鍒嗗竷鐨?int鍊箋?nbsp;
Java闅忔満鏁版葷粨
闅忔満鏁板彂鐢熷櫒(Random)瀵硅薄浜х敓浠ュ悗錛岄氳繃璋冪敤涓嶅悓鐨刴ethod錛歯extInt()銆乶extLong()銆乶extFloat()銆乶extDouble()絳夎幏寰椾笉鍚岀被鍨嬮殢鏈烘暟銆?br />
1>鐢熸垚闅忔満鏁?br />
Random random = new Random();
Random random = new Random(100);//鎸囧畾縐嶅瓙鏁?00
random璋冪敤涓嶅悓鐨勬柟娉曪紝鑾峰緱闅忔満鏁般?br />
濡傛灉2涓猂andom瀵硅薄浣跨敤鐩稿悓鐨勭瀛愶紙姣斿閮芥槸100錛夛紝騫朵笖浠ョ浉鍚岀殑欏哄簭璋冪敤鐩稿悓鐨勫嚱鏁幫紝閭e畠浠繑鍥炲煎畬鍏ㄧ浉鍚屻傚涓嬮潰浠g爜涓袱涓猂andom瀵硅薄鐨勮緭鍑哄畬鍏ㄧ浉鍚?br />
import java.util.*;
class TestRandom {
public static void main(String[] args) {
Random random1 = new Random(100);
System.out.println(random1.nextInt());
System.out.println(random1.nextFloat());
System.out.println(random1.nextBoolean());
Random random2 = new Random(100);
System.out.println(random2.nextInt());
System.out.println(random2.nextFloat());
System.out.println(random2.nextBoolean());
}
}
2>鎸囧畾鑼冨洿鍐呯殑闅忔満鏁?br />
闅忔満鏁版帶鍒跺湪鏌愪釜鑼冨洿鍐?浣跨敤妯℃暟榪愮畻絎?
import java.util.*;
class TestRandom {
public static void main(String[] args) {
Random random = new Random();
for(int i = 0; i < 10;i++) {
System.out.println(Math.abs(random.nextInt())%10);
}
}
}
鑾峰緱鐨勯殢鏈烘暟鏈夋鏈夎礋鐨勶紝鐢∕ath.abs浣胯幏鍙栨暟鎹寖鍥翠負闈炶礋鏁?br />
3>鑾峰彇鎸囧畾鑼冨洿鍐呯殑涓嶉噸澶嶉殢鏈烘暟
import java.util.*;
class TestRandom {
public static void main(String[] args) {
int[] intRet = new int[6];
int intRd = 0; //瀛樻斁闅忔満鏁?br />
int count = 0; //璁板綍鐢熸垚鐨勯殢鏈烘暟涓暟
int flag = 0; //鏄惁宸茬粡鐢熸垚榪囨爣蹇?br />
while(count<6){
Random rdm = new Random(System.currentTimeMillis());
intRd = Math.abs(rdm.nextInt())%32+1;
for(int i=0;i<count;i++){
if(intRet[i]==intRd){
flag = 1;
break;
}else{
flag = 0;
}
}
if(flag==0){
intRet[count] = intRd;
count++;
}
}
for(int t=0;t<6;t++){
System.out.println(t+"->"+intRet[t]);
}
}
}
Java涓殑闅忔満鏁版槸鍚﹀彲浠ラ噸澶嶏紵Java涓駭鐢熺殑闅忔満鏁拌兘鍚﹀彲浠ョ敤鏉ヤ駭鐢熸暟鎹簱涓婚敭錛熷甫鐫榪欎釜闂錛屾垜浠仛浜嗕竴緋誨垪嫻嬭瘯銆?br />
1.嫻嬭瘯涓錛?浣跨敤涓嶅甫鍙傛暟鐨凴andom()鏋勯犲嚱鏁?br />
public class RandomTest {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
for(int i=0;i<10;i++){
System.out.println(r.nextInt());
}
}
}
紼嬪簭榪愯緇撴灉錛?br />
-1761145445
-1070533012
216216989
-910884656
-1408725314
-1091802870
1681403823
-1099867456
347034376
-1277853157
鍐嶆榪愯璇ョ▼搴忥細
-169416241
220377062
-1140589550
-1364404766
-1088116756
2134626361
-546049728
1132916742
-1522319721
1787867608
浠庝笂闈㈢殑嫻嬭瘯鎴戜滑鍙互鐪嬪嚭錛屼嬌鐢ㄤ笉甯﹀弬鏁扮殑Random()鏋勯犲嚱鏁頒駭鐢熺殑闅忔満鏁頒笉浼氶噸澶嶃傞偅涔堬紝浠涔堟儏鍐典笅Java浼氫駭鐢熼噸澶嶇殑闅忔満鏁板憿錛熶笖鐪嬩笅闈㈢殑嫻嬭瘯銆?br />
2. 嫻嬭瘯浜岋細涓篟andom璁劇疆縐嶅瓙鏁?br />
public class RandomTest_Repeat {
/**
* @param args
*/
public static void main(String[] args) {
java.util.Random r=new java.util.Random(10);
for(int i=0;i<10;i++){
System.out.println(r.nextInt());
}
}
}
鏃犺紼嬪簭榪愯澶氬皯嬈★紝鍏剁粨鏋滄繪槸錛?br />
-1157793070
1913984760
1107254586
1773446580
254270492
-1408064384
1048475594
1581279777
-778209333
1532292428
鐢氳嚦鍦ㄤ笉鍚岀殑鏈哄櫒涓婃祴璇曪紝嫻嬭瘯緇撴灉涔熶笉浼氭敼鍙橈紒
3.鍘熷洜鍒嗘瀽錛?br />
(1) 棣栧厛璇鋒墦寮Java Doc錛屾垜浠細鐪嬪埌Random綾葷殑璇存槑錛?br />
姝ょ被鐨勫疄渚嬬敤浜庣敓鎴愪吉闅忔満鏁版祦錛屾綾諱嬌鐢?48 浣嶇殑縐嶅瓙錛岃縐嶅瓙鍙互浣跨敤綰挎у悓浣欏叕寮忓鍏惰繘琛屼慨鏀癸紙璇峰弬闃?Donald Knuth 鐨勩奣he Art of Computer Programming, Volume 2銆嬶紝絎?3.2.1 鑺傦級銆?br />
濡傛灉鐢ㄧ浉鍚岀殑縐嶅瓙鍒涘緩涓や釜 Random 瀹炰緥錛屽垯瀵規瘡涓疄渚嬭繘琛岀浉鍚岀殑鏂規硶璋冪敤搴忓垪錛屽畠浠皢鐢熸垚騫惰繑鍥炵浉鍚岀殑鏁板瓧搴忓垪銆備負浜嗕繚璇佸疄鐜拌繖縐嶇壒鎬э紝鎴戜滑涓虹被Random鎸囧畾浜嗙壒瀹氱殑綆楁硶銆備負浜?Java 浠g爜鐨勫畬鍏ㄥ彲縐繪鎬э紝Java 瀹炵幇蹇呴』璁╃被 Random 浣跨敤姝ゅ鎵紺虹殑鎵鏈夌畻娉曘備絾鏄厑璁?Random 綾葷殑瀛愮被浣跨敤鍏朵粬綆楁硶錛屽彧瑕佸叾絎﹀悎鎵鏈夋柟娉曠殑甯歌鍗忓畾鍗沖彲銆?br />
Java Doc瀵筊andom綾誨凡緇忚В閲婂緱闈炲父鏄庣櫧錛屾垜浠殑嫻嬭瘯涔熼獙璇佷簡榪欎竴鐐廣?br />
(2) 濡傛灉娌℃湁鎻愪緵縐嶅瓙鏁幫紝Random瀹炰緥鐨勭瀛愭暟灝嗘槸褰撳墠鏃墮棿鐨勬縐掓暟錛屽彲浠ラ氳繃System.currentTimeMillis()鏉ヨ幏寰楀綋鍓嶆椂闂寸殑姣鏁般傛墦寮JDK鐨勬簮浠g爜錛屾垜浠彲浠ラ潪甯告槑紜湴鐪嬪埌榪欎竴鐐廣?br />
/**
* Creates a new random number generator. Its seed is initialized to
* a value based on the current time:
* <blockquote><pre>
* public Random() { this(System.currentTimeMillis()); }</pre></blockquote>
*
* @see java.lang.System#currentTimeMillis()
*/
public Random() { this(System.currentTimeMillis()); }
4. 緇撹錛?br />
閫氳繃涓婇潰鐨勬祴璇曞拰鍒嗘瀽錛屾垜浠細瀵筊andom綾繪湁杈冧負娣卞埢鐨勭悊瑙c傚悓鏃訛紝鎴戣寰楋紝閫氳繃闃呰Java Doc鐨凙PI鏂囨。錛屽彲浠ュ緢濂藉湴鎻愰珮鎴戜滑鐨凧ava緙栫▼鑳藉姏錛屽仛鍒?#8220;鐭ュ叾鐒?#8221;錛涗竴鏃﹂亣鍒拌垂瑙g殑闂錛屼笉濡ㄦ墦寮Java鐨勬簮浠g爜錛岃繖鏍鋒垜浠氨鑳藉仛鍒?#8220;鐭ュ叾鎵浠ョ劧”銆?/p>
]]>
TOMCAT鐨勯厤緗?br />
http://download.chinaitlab.com/soft/10791.htm
JAVA閰嶇疆鏂囦歡緙栧啓璇存槑鏂囨。
http://download.chinaitlab.com/soft/10010.htm
涓姝ヤ竴姝ュ浼氶厤緗甂java寮鍙戠幆澧?br />
http://download.chinaitlab.com/soft/9483.htm
Weblogic7寮鍙慐JB鐨勯厤緗?br />
http://download.chinaitlab.com/soft/4938.htm
鍥捐ВJSP鐜瀹夎閰嶇疆
http://download.chinaitlab.com/soft/2157.htm
Tomcat閰嶇疆鏂規硶
http://download.chinaitlab.com/soft/931.htm
鍏ㄧ▼鎸囧Linux涓婮AVA鐜閰嶇疆
http://download.chinaitlab.com/soft/11272.htm
瀛︿範鏂規硶錛?br />
閫氳繃JB4瀛︿範JAVA
http://download.chinaitlab.com/soft/7589.htm
10姝ュ涔?nbsp;JavaScript
http://download.chinaitlab.com/soft/6492.htm
Java Sctipt瀛︿範涓嶆眰浜?br />
http://download.chinaitlab.com/soft/6361.htm
JSP瀛︿範鎸囧崡
http://download.chinaitlab.com/soft/6152.htm
JAVA瀛︿範鏂囨。
http://download.chinaitlab.com/soft/6114.htm
J2EE瀛︿範絎旇
http://download.chinaitlab.com/soft/6048.htm
JavaScript瀛︿範
http://download.chinaitlab.com/soft/3029.htm
Java2 瀛︿範鎸囧崡
http://download.chinaitlab.com/soft/2743.htm
Juniper瀛︿範鎸囧崡
http://download.chinaitlab.com/soft/2227.htm
J2EE瀛︿範璧勬枡
http://download.chinaitlab.com/soft/1618.htm
JSP鐢辨祬鍏ユ繁
http://download.chinaitlab.com/soft/1315.htm
闈㈠悜瀵硅薄緙栫▼錛?br />
Java闈㈠悜瀵硅薄緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/9792.htm
JAVA鐨勬牳蹇冩妧鏈細闈㈠悜瀵硅薄緙栫▼
http://download.chinaitlab.com/soft/9093.htm
Java 涓?nbsp;UML 闈㈠悜瀵硅薄紼嬪簭璁捐
http://download.chinaitlab.com/soft/6053.htm
Java 2 緙栫▼21澶╄嚜瀛﹂?br />
http://download.chinaitlab.com/soft/10507.htm
J2EE緙栫▼璧鋒
http://download.chinaitlab.com/soft/10506.htm
Java闈㈠悜瀵硅薄緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/9792.htm
Java涓撲笟緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/9791.htm
Java鏈嶅姟鍣ㄩ珮綰х紪紼?br />
http://download.chinaitlab.com/soft/9790.htm
J2EE EAI緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/9784.htm
J2MEMIDP鏃犵嚎璁懼緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/9765.htm
JAVA緙栫▼鎬濇兂 涓枃鐗?br />
http://download.chinaitlab.com/soft/9481.htm
Java XML緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/9097.htm
Java 鏁版嵁搴撶紪紼嬪疂鍏?br />
http://download.chinaitlab.com/soft/9095.htm
JAVA鐨勬牳蹇冩妧鏈細闈㈠悜瀵硅薄緙栫▼
http://download.chinaitlab.com/soft/9093.htm
JDBC API鏁版嵁搴撶紪紼嬪疄浣滄暀鏉?br />
http://download.chinaitlab.com/soft/9087.htm
鏍稿績 JSF 緙栫▼
http://download.chinaitlab.com/soft/7946.htm
JAVA 2搴旂敤緙栫▼150渚?br />
http://download.chinaitlab.com/soft/6815.htm
JAVA鏁版嵁搴撶紪紼婮DBC
http://download.chinaitlab.com/soft/6113.htm
娣卞叆鎺屾彙J2EE緙栫▼鎶鏈?br />
http://download.chinaitlab.com/soft/6030.htm
瀹炵敤J2EE璁捐妯″紡緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/5033.htm
Java for Internet緙栫▼鎶鏈?br />
http://download.chinaitlab.com/soft/4198.htm
Java瀹夊叏鎬х紪紼嬫寚鍗?br />
http://download.chinaitlab.com/soft/3773.htm
J2ME鏃犵嚎璁懼緙栫▼
http://download.chinaitlab.com/soft/3669.htm
J2EE EJB緙栫▼瀹炰緥
http://download.chinaitlab.com/soft/3141.htm
Java緙栫▼鎬濇兂 絎笁鐗?br />
http://download.chinaitlab.com/soft/2982.htm
Java 鏋侀檺緙栫▼
http://download.chinaitlab.com/soft/1707.htm
Java2緙栫▼璇﹁В
http://download.chinaitlab.com/soft/1705.htm
緗戠粶緙栫▼錛?br />
J2EE緗戠粶緙栫▼鏍囧噯鏁欑▼
http://download.chinaitlab.com/soft/9100.htm
Java緗戠粶緙栫▼瀹炰緥
http://download.chinaitlab.com/soft/9090.htm
Java P2P緗戠粶緙栫▼鎶鏈?br />
http://download.chinaitlab.com/soft/6333.htm
Java緗戠粶緙栫▼
http://download.chinaitlab.com/soft/3871.htm
緗戠粶緙栫▼鍩虹綃囦箣 Java Script
http://download.chinaitlab.com/soft/3618.htm
Solaris Shell 緙栫▼
http://download.chinaitlab.com/soft/6426.htm
SUN Solaris綆$悊鎵嬪唽
http://download.chinaitlab.com/soft/5732.htm
Solaris鎬ц兘綆$悊
http://download.chinaitlab.com/soft/11247.htm
Solaris9瀹夎鎸囧崡
http://download.chinaitlab.com/soft/5022.htm
SOLARIS楂樼駭緋葷粺綆$悊鍛樻寚鍗?br />
http://download.chinaitlab.com/soft/3726.htm
Solaris鎿嶄綔鐜瀹夊叏
http://download.chinaitlab.com/soft/1500.htm
Solaris GNOME2.0妗岄潰鐢ㄦ埛鎸囧崡
http://download.chinaitlab.com/soft/1488.htm
Solaris 9 12/03 瀹夎鎸囧崡
http://download.chinaitlab.com/soft/1484.htm
Solaris綆$悊鍛樻寚鍗?br />
http://download.chinaitlab.com/soft/1475.htm
涓枃Solaris9 緋葷粺綆$悊鍛樻寚鍗?br />
http://download.chinaitlab.com/soft/1463.htm
Solaris瀹夊叏鎬т笓棰樻寚瀵?br />
http://download.chinaitlab.com/soft/746.htm
XML緋誨垪錛?br />
Java XML緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/9097.htm
Java紼嬪簭璁捐EJB銆乆ML涓庢暟鎹簱
http://download.chinaitlab.com/soft/9094.htm
XML 緇堟瀬鏁欑▼
http://download.chinaitlab.com/soft/9057.htm
Java&XML搴旂敤
http://download.chinaitlab.com/soft/6211.htm
XML_JAVA鎸囧崡
http://download.chinaitlab.com/soft/6163.htm
JDBC錛?br />
JDBC API鏁版嵁搴撶紪紼嬪疄浣滄暀鏉?br />
http://download.chinaitlab.com/soft/9087.htm
JAVA鏁版嵁搴撶紪紼婮DBC
http://download.chinaitlab.com/soft/6113.htm
JDBC API 鍙傝冩暀紼嬬涓夌増
http://download.chinaitlab.com/soft/6057.htm
JDBC涓嶫ava鏁版嵁搴撶▼搴忚璁?br />
http://download.chinaitlab.com/soft/6018.htm
Java璇█SQL鎺ュ彛 JDBCprogram
http://download.chinaitlab.com/soft/5938.htm
JSP搴旂敤紼嬪簭寮鍙戞寚鍗?br />
http://download.chinaitlab.com/soft/1546.htm
鐢?nbsp;JDBC 綆$悊鏁版嵁搴撹繛鎺?br />
http://download.chinaitlab.com/soft/935.htm
JDO錛?br />
鍏ㄩ潰浜嗚ВJDO鏁版嵁搴撶紪紼?br />
http://download.chinaitlab.com/soft/7992.htm
Struts錛?br />
Struts涓枃鎵嬪唽
http://download.chinaitlab.com/soft/10516.htm
Struts鏋舵瀯鎸囧
http://download.chinaitlab.com/soft/7529.htm
綺鵑歴truts鎶鏈?br />
http://download.chinaitlab.com/soft/6801.htm
Struts 瀛︿範璧鋒闂瓟
http://download.chinaitlab.com/soft/6156.htm
Hibernate錛?br />
Hibernate2.1.2鍙傝冩墜鍐屼腑鏂囩増
http://download.chinaitlab.com/soft/8919.htm
JAVA涓庢ā寮廫J2EE妯″紡錛?br />
J2EE 鏍稿績妯″紡
http://download.chinaitlab.com/soft/9785.htm
Java 浼佷笟璁捐妯″紡
http://download.chinaitlab.com/soft/9096.htm
Java綆鍗曞伐鍘傚垱绔嬫фā寮忎粙緇?br />
http://download.chinaitlab.com/soft/7398.htm
EJB璁捐妯″紡
http://download.chinaitlab.com/soft/6135.htm
JAVA璁捐妯″紡
http://download.chinaitlab.com/soft/6112.htm
瀹炵敤J2EE璁捐妯″紡緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/5033.htm
Java涓庢ā寮?br />
http://download.chinaitlab.com/soft/3073.htm
璁捐妯″紡Java鐗?br />
http://download.chinaitlab.com/soft/1723.htm
JBuilder寮鍙慡ervlet鍙奐SP錛?br />
綺鵑欽Builder
http://download.chinaitlab.com/soft/10565.htm
JBuilder閫熸垚璧勬枡
http://download.chinaitlab.com/soft/9714.htm
Jbuilder7鍜寃eblogic7鏁村悎寮鍙戞墜
http://download.chinaitlab.com/soft/9664.htm
JBUILDER9 杞歡寮鍙戦」鐩疄璺?br />
http://download.chinaitlab.com/soft/9089.htm
JbuilderX寮鍙戞寚鍗?br />
http://download.chinaitlab.com/soft/9088.htm
Jbuilder x 鎸囧崡
http://download.chinaitlab.com/soft/7984.htm
JBuilder4寮鍙戜漢鍛樻寚鍗?br />
http://download.chinaitlab.com/soft/5939.htm
JBuilder7 Weblogic7鏁村拰寮鍙戝煿璁墜鍐?br />
http://download.chinaitlab.com/soft/4727.htm
JBuilder寮鍙戞暟鎹簱搴旂敤紼嬪簭
http://download.chinaitlab.com/soft/1701.htm
Java寮鍙戞寚鍗?-Servlets鍜孞SP綃?br />
http://download.chinaitlab.com/soft/9793.htm
Java Servlets 緙栫▼鎸囧崡
http://download.chinaitlab.com/soft/9098.htm
Oreilly Java Servlet
http://download.chinaitlab.com/soft/6522.htm
Java Servlet寮鍙戜笌瀹炰緥
http://download.chinaitlab.com/soft/6029.htm
娣卞叆Java Servlet 緗戠粶緙栫▼
http://download.chinaitlab.com/soft/9783.htm
鐢↗SP_Servlet鏋勫緩涓夊眰寮忕鐞嗕俊鎭郴緇?br />
http://download.chinaitlab.com/soft/6034.htm
Java Servlet甯姪鏂囨。
http://download.chinaitlab.com/soft/2981.htm
JSP緗戠珯緙栫▼鏁欑▼
http://download.chinaitlab.com/soft/11256.htm
JSP璇硶鍒嗘瀽
http://download.chinaitlab.com/soft/11257.htm
JSP瀹炵敤鏁欑▼
http://download.chinaitlab.com/soft/10792.htm
JSP璇硶錛?錛?-HTML娉ㄩ噴
http://download.chinaitlab.com/soft/10790.htm
JSP搴旂敤寮鍙戣瑙?br />
http://download.chinaitlab.com/soft/10025.htm
JSP鎶鏈彮縐?br />
http://download.chinaitlab.com/soft/9387.htm
JSP鎶鏈ぇ鍏?br />
http://download.chinaitlab.com/soft/9388.htm
JSP緗戜笂涔﹀簵瀹炰緥璇﹁В
http://download.chinaitlab.com/soft/9386.htm
JSP鍔ㄦ佺綉欏墊柊鎶鏈?br />
http://download.chinaitlab.com/soft/8920.htm
JSP 鎶鏈ぇ鍏?br />
http://download.chinaitlab.com/soft/7782.htm
JSP楂樼駭寮鍙戜笌搴旂敤
http://download.chinaitlab.com/soft/7633.htm
JSP 瀹屽叏鎺㈢儲
http://download.chinaitlab.com/soft/7546.htm
JSP 楂樼駭寮鍙戜笌搴旂敤
http://download.chinaitlab.com/soft/7116.htm
JSP緙栫▼鎶宸?br />
http://download.chinaitlab.com/soft/7114.htm
JSP閫熸垚鏁欑▼
http://download.chinaitlab.com/soft/6882.htm
JSP緗戠粶緙栫▼鎶鏈?br />
http://download.chinaitlab.com/soft/6880.htm
JSP紼嬪簭璁捐鎸囧崡
http://download.chinaitlab.com/soft/6690.htm
鏈鏂癑SP鍏ラ棬涓庡簲鐢?br />
http://download.chinaitlab.com/soft/6697.htm
JSP蹇熷叆闂?br />
http://download.chinaitlab.com/soft/6636.htm
JSP緗戦〉緙栫▼
http://download.chinaitlab.com/soft/6527.htm
JSP 瀹炵敤鏁欑▼
http://download.chinaitlab.com/soft/6334.htm
JSP鍏ラ棬涓庢彁楂?br />
http://download.chinaitlab.com/soft/6326.htm
JSP璇硶
http://download.chinaitlab.com/soft/6116.htm
鎺屾彙鑷畾涔塉SP鏍囩
http://download.chinaitlab.com/soft/6096.htm
JSP 鍔ㄦ佺綉绔欐妧鏈叆闂ㄤ笌鎻愰珮
http://download.chinaitlab.com/soft/6019.htm
JSP瀹炰緥鍏ラ棬
http://download.chinaitlab.com/soft/4377.htm
JSP鏁欑▼涔嬩笌鏁版嵁搴撻氫俊
http://download.chinaitlab.com/soft/3673.htm
濡備綍鎴愪負浼樼鐨凧SP 紼嬪簭鍛?br />
http://download.chinaitlab.com/soft/3002.htm
JSP鏁版嵁搴撶紪紼嬫寚鍗?br />
http://download.chinaitlab.com/soft/2946.htm
JSP 楂樼駭緙栫▼
http://download.chinaitlab.com/soft/2635.htm
JSP瀹炵敤緙栫▼瀹炰緥闆嗛敠
http://download.chinaitlab.com/soft/2154.htm
JSP紼嬪簭璁捐綺懼僵瀹炰緥
http://download.chinaitlab.com/soft/2151.htm
JSP鍗蟲椂搴旂敤
http://download.chinaitlab.com/soft/1547.htm
JSP紼嬪簭璁捐綺懼僵瀹炰緥
http://download.chinaitlab.com/soft/1543.htm
JSP瀹炵敤緙栫▼瀹炰緥闆嗛敠
http://download.chinaitlab.com/soft/1537.htm
JSP鍩虹
http://download.chinaitlab.com/soft/894.htm
Eclipse寮鍙慡ervlet鍙奐SP錛?br />
Eclipse+Tomcat闆嗘垚寮鍙憇ervle
http://download.chinaitlab.com/soft/6134.htm
J2EE Jboss Ejb With Eclipse 2003
http://download.chinaitlab.com/soft/6045.htm
]]>
log4j.addivity.org.apache=true
#ConsoleAppender錛屾帶鍒跺彴杈撳嚭
#FileAppender錛屾枃浠舵棩蹇楄緭鍑?br />
#SMTPAppender錛屽彂閭歡杈撳嚭鏃ュ織
#SocketAppender錛孲ocket鏃ュ織
#NTEventLogAppender錛學indow NT鏃ュ織
#SyslogAppender錛?br />
#JMSAppender錛?br />
#AsyncAppender錛?br />
#NullAppender
#鏂囦歡杈撳嚭錛歊ollingFileAppender
#log4j.rootLogger = INFO,logfile
log4j.appender.logfile = org.apache.log4j.RollingFileAppender
log4j.appender.logfile.Threshold = INFO
# 杈撳嚭浠ヤ笂鐨処NFO淇℃伅
log4j.appender.logfile.File = INFO_log.html
#淇濆瓨log鏂囦歡璺緞
log4j.appender.logfile.Append = true
# 榛樿涓簍rue錛屾坊鍔犲埌鏈熬錛宖alse鍦ㄦ瘡嬈″惎鍔ㄦ椂榪涜瑕嗙洊
log4j.appender.logfile.MaxFileSize = 1MB
# 涓涓猯og鏂囦歡鐨勫ぇ灝忥紝瓚呰繃榪欎釜澶у皬灝卞張浼氱敓鎴?涓棩蹇?# KB 錛孧B錛孏B
log4j.appender.logfile.MaxBackupIndex = 3
# 鏈澶氫繚瀛?涓枃浠跺浠?
log4j.appender.logfile.layout = org.apache.log4j.HTMLLayout
# 杈撳嚭鏂囦歡鐨勬牸寮?
log4j.appender.logfile.layout.LocationInfo = true
#鏄惁鏄劇ず綾誨悕鍜岃鏁?br />
log4j.appender.logfile.layout.Title =title:\u63d0\u9192\u60a8\uff1a\u7cfb\u7edf\u53d1\u751f\u4e86\u4e25\u91cd\u9519\u8bef
#html欏甸潰鐨?< title >
############################## SampleLayout ####################################
# log4j.appender.logfile.layout = org.apache.log4j.SampleLayout
############################## PatternLayout ###################################
# log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
# log4j.appender.logfile.layout.ConversionPattern =% d % p [ % c] - % m % n % d
############################## XMLLayout #######################################
# log4j.appender.logfile.layout = org.apache.log4j.XMLLayout
# log4j.appender.logfile.layout.LocationInfo = true #鏄惁鏄劇ず綾誨悕鍜岃鏁?br />
############################## TTCCLayout ######################################
# log4j.appender.logfile.layout = org.apache.log4j.TTCCLayout
# log4j.appender.logfile.layout.DateFormat = ISO8601
#NULL, RELATIVE, ABSOLUTE, DATE or ISO8601.
# log4j.appender.logfile.layout.TimeZoneID = GMT - 8 : 00
# log4j.appender.logfile.layout.CategoryPrefixing = false ##榛樿涓簍rue 鎵撳嵃綾誨埆鍚?br />
# log4j.appender.logfile.layout.ContextPrinting = false ##榛樿涓簍rue 鎵撳嵃涓婁笅鏂囦俊鎭?br />
# log4j.appender.logfile.layout.ThreadPrinting = false ##榛樿涓簍rue 鎵撳嵃綰跨▼鍚?br />
# 鎵撳嵃淇℃伅濡備笅錛?br />
#2007 - 09 - 13 14 : 45 : 39 , 765 [http - 8080 - 1 ] ERROR com.poxool.test.test - error鎴愬姛鍏抽棴閾炬帴
###############################################################################
#姣忓ぉ鏂囦歡鐨勮緭鍑猴細DailyRollingFileAppender
#log4j.rootLogger = INFO,errorlogfile
log4j.appender.errorlogfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorlogfile.Threshold = ERROR
log4j.appender.errorlogfile.File = ../logs/ERROR_log
log4j.appender.errorlogfile.Append = true
#榛樿涓簍rue錛屾坊鍔犲埌鏈熬錛宖alse鍦ㄦ瘡嬈″惎鍔ㄦ椂榪涜瑕嗙洊
log4j.appender.errorlogfile.ImmediateFlush = true
#鐩存帴杈撳嚭錛屼笉榪涜緙撳瓨
# ' . ' yyyy - MM: 姣忎釜鏈堟洿鏂頒竴涓猯og鏃ュ織
# ' . ' yyyy - ww: 姣忎釜鏄熸湡鏇存柊涓涓猯og鏃ュ織
# ' . ' yyyy - MM - dd: 姣忓ぉ鏇存柊涓涓猯og鏃ュ織
# ' . ' yyyy - MM - dd - a: 姣忓ぉ鐨勫崍澶滃拰姝e崍鏇存柊涓涓猯og鏃ュ織
# ' . ' yyyy - MM - dd - HH: 姣忓皬鏃舵洿鏂頒竴涓猯og鏃ュ織
# ' . ' yyyy - MM - dd - HH - mm: 姣忓垎閽熸洿鏂頒竴涓猯og鏃ュ織
log4j.appender.errorlogfile.DatePattern = ' . ' yyyy - MM - dd ' .log '
#鏂囦歡鍚嶇О鐨勬牸寮?/span>
log4j.appender.errorlogfile.layout = org.apache.log4j.PatternLayout
log4j.appender.errorlogfile.layout.ConversionPattern =%d %p [ %c] - %m %n %d
#鎺у埗鍙拌緭鍑猴細
#log4j.rootLogger = INFO,consoleAppender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.Threshold = ERROR
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern =%d %-5p %m %n
log4j.appender.consoleAppender.ImmediateFlush = true
# 鐩存帴杈撳嚭錛屼笉榪涜緙撳瓨
log4j.appender.consoleAppender.Target = System.err
# 榛樿鏄疭ystem.out鏂瑰紡杈撳嚭
#鍙戦侀偖浠訛細SMTPAppender
#log4j.rootLogger = INFO,MAIL
log4j.appender.MAIL = org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold = INFO
log4j.appender.MAIL.BufferSize = 10
log4j.appender.MAIL.From = yourmail@gmail.com
log4j.appender.MAIL.SMTPHost = smtp.gmail.com
log4j.appender.MAIL.Subject = Log4J Message
log4j.appender.MAIL.To = yourmail@gmail.com
log4j.appender.MAIL.layout = org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern =%d - %c -%-4r [%t] %-5p %c %x - %m %n
#鏁版嵁搴擄細JDBCAppender
log4j.appender.DATABASE = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL = jdbc:oracle:thin:@ 210.51 . 173.94 : 1521 :YDB
log4j.appender.DATABASE.driver = oracle.jdbc.driver.OracleDriver
log4j.appender.DATABASE.user = ydbuser
log4j.appender.DATABASE.password = ydbuser
log4j.appender.DATABASE.sql = INSERT INTO A1 (TITLE3) VALUES ( ' %d - %c %-5p %c %x - %m%n ' )
log4j.appender.DATABASE.layout = org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern =% d - % c -%- 4r [ % t] %- 5p % c % x - % m % n
JNI涔婥++璋冪敤Java綾?鈥斺?java.lang.String
涓轟粈涔堣鐢–++璋冪敤Java綾伙紵寰堥毦鍥炵瓟錛屽啓鐫鏂囩珷鍙槸瑙夊緱JNI寰堟湁鎰忔濄備簬鏄紑濮嬬紪鍐欎竴孌典嬌鐢╒C++鍦╓indows緋葷粺閲岃皟鐢╦ava鐨凷tring綾伙紝鍦–++閲岃皟鐢⊿tring綾誨唴鐨勪竴浜涙柟娉曘?/p>
JNI宸茬粡琚紑鍙戜簡寰堝騫達紝鑰屽湪鎴?騫村鐨凧ava緙栫▼鏃墮棿閲屼粠鏉ユ病鏈夋帴瑙﹁繃銆傜洿鍒版渶榪戠爺絀禞VM瀹炵幇鍘熺悊鎵嶆敞鎰忓埌JNI銆?JNI鏃ava Native Interface錛孨ative榪欎釜璇嶆垜瑙佽繃鎴戣涓烘渶鎭板綋鐨勭炕璇戝氨鏄師鐢熴傚師鐢熺殑鎰忔濆氨鏄潵鑷郴緇熻嚜宸辯殑錛屽師姹佸師鍛崇殑涓滆タ錛屼緥濡俉in32 API銆侸ava綾婚渶瑕佸湪铏氭嫙鏈轟笂榪愯錛屼篃灝變笉鏄師鐢熺殑錛屽悓鏍?NET Framework涔熶笉鏄師鐢熺殑銆侸NI涔熷氨鏄疛ava鍘熺敓鎺ュ彛銆傚叧浜嶫NI鐨勮鑼冿紝浠ュ強涓轟粈涔堣浣跨敤瀹冿紝瀹冭兘鍋氫簺浠涔堬紝閮藉湪http://java.sun.com/j2se/1.4.2/docs/guide/jni/spec/jniTOC.html閲岃榪扮潃銆?/p>
JNI鏄鑼冿紝瀹冭瀹氫簡铏氭嫙鏈虹殑鎺ュ彛錛岃屾妸鍏蜂綋鐨勫疄鐜扮暀緇欏紑鍙戣呫?/p>
JVM鐨勫疄鐜頒笉鏄敮涓鐨勶紝鐩墠瀛樺湪寰堝縐岼ava铏氭嫙鏈猴紝Sun Hotspot錛孖BM JDK錛岃繕鏈塇P鐨勶紝Kaffe絳夌瓑銆傛渶嫻佽鐨勫氨鏄疭un鐨凥otspot錛屾渶澶嶆潅鐨勫氨鏄疘BM JDK錛岃繖鏄疘BM鐨勪竴璐綔椋庛傛湰鏂囦笉璁ㄨJVM鐨勫疄鐜幫紝鍙叧娉↗NI銆傚鏋滄偍瀹夎浜哠un鐨凧DK錛屾偍灝辮兘鍦╗JAVA_HOME]\include鐩綍涓嬫壘鍒癹ni.h銆傝繖涓ご鏂囦歡灝辨槸铏氭嫙鏈虹殑鍞竴鎺ュ彛錛屼綘鍙互璋冪敤瀹冨0鏄庣殑鍑芥暟鍒涘緩涓涓狫VM銆?/p>
鍦ㄨ鏄嶤++璋冪敤Java綾諱箣鍓嶏紝鎴戞兂鍏堟紨紺轟竴涓嬪鏋滅紪鍐橨ava Native Method銆?/p>
1錛庣紪鍐欏甫鏈塏ative鏂規硶鐨凧ava綾?/p>
package org.colimas.jni.test;
public class JniTest {
static { System.loadLibrary("JniTestImpl"); } //JVM璋冪敤JniTestImpl.dll
public JniTest(){
}
//鍘熺敓鏂規硶
public native void print(String str);
/**
* @param args
*/
public static void main(String[] args) {
JniTest test=new JniTest();
test.print("hello JVM"); //璋冪敤鍘熺敓鏂規硶
}
}
2錛庝嬌鐢╦avah鐢熸垚c璇█澶存枃浠躲?/p>
javah -jni org.colimas.jni.test.JniTest
鐩綍閲屽浜嗕竴涓猳rg_colimas_jni_test_JniTest.h鏂囦歡錛屾墦寮鏂囦歡錛屽唴瀹瑰涓?
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_colimas_jni_test_JniTest */
#ifndef _Included_org_colimas_jni_test_JniTest
#define _Included_org_colimas_jni_test_JniTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_colimas_jni_test_JniTest
* Method: print
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_colimas_jni_test_JniTest_print
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
鍏朵腑鐨凧ava_org_colimas_jni_test_JniTest_print灝辨槸JniTest綾婚噷闈㈢殑print鍘熺敓鏂規硶鐨凜璇█澹版槑銆?/p>
3錛庣紪鍐機浠g爜瀹炵幇鍘熺敓鏂規硶print
#include <jni.h>
#include "org_colimas_jni_test_JniTest.h" //javah鐢熸垚鐨勫ご鏂囦歡
#include <stdio.h>
JNIEXPORT void JNICALL Java_org_colimas_jni_test_JniTest_print
(JNIEnv *env, jobject object,jstring str)
{
//鑾峰緱瀛楃涓?br />
const char * txt=(*env)->GetStringUTFChars(env,str,0);
printf("%s\n",txt); //鎵撳嵃鍒版帶鍒跺彴
return;
}
鍙傛暟JNIEnv *env錛屾槸JNI閲屾渶閲嶈鐨勫彉閲忋侸ava.exe鍒涘緩JVM錛屼箣鍚嶫VM鐢熸垚涓涓猠nv錛岃env鐩稿綋浜嶫VM鍐呯殑Session錛屽彲浠ュ畬鎴愬垱寤篔ava瀵硅薄錛岃皟鐢ㄧ被鏂規硶錛岃幏寰楃被鐨勫睘鎬х瓑絳夈?/p>
鍦ㄨ繖閲宔nv灝嗘柟娉曠殑鍙傛暟Str浠嶫NI鐨刯string綾誨瀷杞崲涓哄父鏁癱har鏁扮粍銆?/p>
4錛庣紪璇?/p>
cl /Ic:\j2sdk1.4.2_10\include /Ic:\j2sdk1.4.2_10\include\win32 /c JniTestImpl.c
5錛庤繛鎺ヤ負DLL
link /dll JniTestImpl.obj
6錛庤緗甈ATH
set PATH=C:\MyProject\Colimas\CD\JNI\MyJNI;%PATH%
7錛庤繍琛?/p>
java org.colimas.jni.test.JniTest
榪斿洖緇撴灉
hello JVM
緇撴潫
浠ヤ笂鏄疄鐜癑ava鍘熺敓鏂規硶鐨勫紑鍙戣繃紼嬶紝涓嬮潰榪涘叆姝i錛屼嬌鐢–++璋冪敤Java鐨刯ava.lang.String綾匯?/p>
1. Object綾誨嚭鍒涘緩JVM銆?/p>
浣跨敤Java綾諱箣鍓嶅繀欏昏鍒涘緩JVM鐜銆侸DK鐢眏ava.exe鏉ュ畬鎴愩傛湰鏂囨湁Object綾葷殑闈欐佹柟娉旴eginJVM鏉ュ垱寤猴紝鐢‥ndJVM鏉ュ叧闂?/p>
鍒涘緩JVM涔嬪悗浼氬湪鍒涘緩2涓彉閲忥紝鍒嗗埆鏄疛NIEnv* env鍜孞avaVM* jvm錛孞NIEnv涓婃枃宸茬粡璇存槑錛孞avaVM錛岄【鍚嶆濅箟錛屼唬琛↗ava铏氭嫙鏈猴紝鐢ㄥ畠鏉ュ叧闂璊VM銆?/p>
Object綾葷殑澶存枃浠?/p>
#include "jni.h"
class Object
{
public:
static bool BeginJVM();
static bool EndJVM();
Object();
virtual ~Object();
protected:
static JNIEnv* env;
static JavaVM* jvm;
};
object.cpp浠g爜
#include "stdafx.h"
#include "JavaClasses.h"
#include "Object.h"
Object::Object()
{}
Object::~Object()
{}
JNIEnv* Object::env=NULL;
JavaVM* Object::jvm=NULL;
//鍒涘緩JVM
bool Object::BeginJVM()
{
JavaVMOption options[3];
JavaVMInitArgs vm_args;
//鍚勭鍙傛暟
options[0].optionString="-Xmx128m";
options[1].optionString="-Verbose:gc";
options[2].optionString="-Djava.class.path=.";
vm_args.version=JNI_VERSION_1_2;
vm_args.options=options;
vm_args.nOptions=3;
//鍒涘緩JVM錛岃幏寰梛vm鍜宔nv
int res = JNI_CreateJavaVM(&jvm,(void **)&env, &vm_args);
return true;
}
bool Object::EndJVM()
{
//鍏抽棴JVM
jvm->DestroyJavaVM();
return true;
}
2. C++鐨凷tring綾昏皟鐢╦ava.lang.String綾繪柟娉?/p>
緙栧啓C++鐗堢殑String綾伙紝璋冪敤java String綾繪柟娉曘傝皟鐢ㄧ殑鏂規硶濡備笅錛?/p>
String replaceAll(String regex, String replacement);
boolean endsWith(String str);
int indexOf(String str);
int compareTo(String anotherString);
char charAt(int i);
String鐨勫ご鏂囦歡:
class String :public Object
{
public:
//涓庤璋冪敤鐨凧ava鏂規硶鍚嶄竴鑷淬?br />
const char * replaceAll(char *regex,char *replacement);
bool endsWith(char * str);
int indexOf(char * str);
int compareTo(char *anotherString);
char charAt(int i);
String(char *str);
virtual ~String();
};
瀹炵幇錛?/p>
#include "stdafx.h"
#include "String.h"
#include "jni.h"
using namespace std;
jclass clazz; //鍏ㄥ眬鍙橀噺錛岀敤鏉ヤ紶閫抍lass
jobject object; //鍏ㄥ眬鍙橀噺錛岀敤鏉ヤ紶閫抩bject
String::String(char *str)
{
jstring jstr;
if (Object::env ==NULL){
cout << "JVM is not created" << endl;
exit(-1);
}
//鑾峰緱java.lang.String綾?br /> clazz=Object::env->FindClass("java/lang/String");
if (clazz ==0 ){
cout << "Class is not found" << endl;
exit(-1);
}
//鑾峰緱String(String str)鏋勯犱綋
jmethodID mid= Object::env->GetMethodID(clazz,"<init>", "(Ljava/lang/String;)V");
if (mid==0){
cerr<< "GetMethodID Error for class" << endl;
exit(-1);
}
//灝嗗瓧絎︿覆灝佽涓簀string銆?br /> jstr = Object::env->NewStringUTF(str);
if (jstr == 0) {
cerr << "Out of memory" <<endl;
exit(-1);
}
cout << "invoking method" << endl;
//鍒涘緩涓涓猨ava.lang.String瀵硅薄銆?br />
object=Object::env->NewObject(clazz,mid,jstr);
}
String::~String()
{}
char String::charAt(int i)
{
if (Object::env ==NULL){
cout << "JVM is not created" << endl;
exit(-1);
}
if (clazz ==0 ){
cout << "Class is not found" << endl;
exit(-1);
}
if (object ==0 ){
cout << "String object is not created" << endl;
exit(-1);
}
jmethodID mid;
//鑾峰緱charAt鏂規硶錛岋紙I錛塁琛ㄧず 鍙傛暟涓篿nt鍨嬶紝榪斿洖char鍨嬨傝緇嗗弬瑙丣NI瑙勮寖
mid = Object::env->GetMethodID(clazz,"charAt", "(I)C");
if (mid==0){
cerr<< "GetMethodID Error for class" << endl;
exit(-1);
}
jint ji=i;
cout << "invoking method" << endl;
//璋冪敤charAt
jchar z = Object::env->CallCharMethod(object,mid,i);
//榪斿洖緇撴灉銆?br />
return z;
}
int String::compareTo(char *anotherString)
{
if (Object::env ==NULL){
cout << "JVM is not created" << endl;
exit(-1);
}
if (clazz ==0 ){
cout << "Class is not found" << endl;
exit(-1);
}
if (object ==0 ){
cout << "String object is not created" << endl;
exit(-1);
}
jmethodID mid;
//(Ljava/lang/String;)I琛ㄧず鍙傛暟涓簀ava.lang.String錛岃繑鍥瀒nt
mid= Object::env->GetMethodID(clazz,"compareTo", "(Ljava/lang/String;)I");
if (mid==0){
cerr<< "GetMethodID Error for class" << endl;
exit(-1);
}
jstring jstr = Object::env->NewStringUTF(anotherString);
cout << "invoking method" << endl;
//璋冪敤鏂規硶
jint z=Object::env->CallIntMethod(object,mid,jstr);
//榪斿洖緇撴灉
return z;
}
int String::indexOf(char *str)
{
if (Object::env ==NULL){
cout << "JVM is not created" << endl;
exit(-1);
}
if (clazz ==0 ){
cout << "Class is not found" << endl;
exit(-1);
}
if (object ==0 ){
cout << "String object is not created" << endl;
exit(-1);
}
jmethodID mid;
mid= Object::env->GetMethodID(clazz,"indexOf", "(Ljava/lang/String;)I");
if (mid==0){
cerr<< "GetMethodID Error for class" << endl;
exit(-1);
}
jstring jstr = Object::env->NewStringUTF(str);
cout << "invoking method" << endl;
jint z=Object::env->CallIntMethod(object,mid,jstr);
return z;
}
bool String::endsWith(char *str)
{
if (Object::env ==NULL){
cout << "JVM is not created" << endl;
exit(-1);
}
if (clazz ==0 ){
cout << "Class is not found" << endl;
exit(-1);
}
if (object ==0 ){
cout << "String object is not created" << endl;
exit(-1);
}
jmethodID mid;
mid= Object::env->GetMethodID(clazz,"endsWith", "(Ljava/lang/String;)Z");
if (mid==0){
cerr<< "GetMethodID Error for class" << endl;
exit(-1);
}
jstring jstr = Object::env->NewStringUTF(str);
cout << "invoking method" << endl;
bool z = Object::env->CallBooleanMethod(object,mid,jstr);
return z;
}
const char * String::replaceAll(char *regex, char *replacement)
{
if (Object::env ==NULL){
cout << "JVM is not created" << endl;
exit(-1);
}
if (clazz ==0 ){
cout << "Class is not found" << endl;
exit(-1);
}
if (object ==0 ){
cout << "String object is not created" << endl;
exit(-1);
}
jmethodID mid;
mid= Object::env->GetMethodID(clazz,"replaceAll", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
if (mid==0){
cerr<< "GetMethodID Error for class" << endl;
exit(-1);
}
jvalue array[2];
jstring jreg = Object::env->NewStringUTF(regex);
jstring jstr = Object::env->NewStringUTF(replacement);
array[0].l=jreg;
array[1].l=jstr;
cout << "invoking method" << endl;
//浼犲叆鍙傛暟錛岃皟鐢╮eplaceAll鏂規硶
jobject z=Object::env->CallObjectMethodA(object,mid,array);
const char *result=Object::env->GetStringUTFChars((jstring)z, 0);
return (const char *)result;
}
3錛庢祴璇?/p>
緙栧啓嫻嬭瘯浠g爜
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)){
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
} else{
//鍒涘緩JVM
Object::BeginJVM();
String test("hello");
//璋冪敤replaceAll
const char *result = test.replaceAll("l","z");
//榪斿洖緇撴灉
cout<< result <<endl;
//鍏抽棴JVM
Object::EndJVM();
}
return nRetCode;
}
4錛庤繍琛?/p>
緙栬瘧闇瑕?jni.h鍜宩vm.lib鏂囦歡銆?/p>
jni.h鍦╗JAVA_HOME]\include
jvm.lib鍦╗JAVA_HOME]\lib
榪愯闇瑕乯vm.dll
jvm.dll鍦╗JAVA_HOME]\ jre\bin\client
榪愯緇撴灉濡備笅錛?/p>
invoking method
invoking method
hezzo
Press any key to continue
灝界鏈枃鐨勪唬鐮佸緢鏈夋剰鎬濓紝浣嗘垜榪樻病鏈夋兂鍒版湁浠涔堜環鍊鹼紝浠ュ強搴旂敤鍒板疄闄呴」鐩腑鐨勭悊鐢便?/p>
鏈枃鏉ヨ嚜CSDN鍗氬錛岃漿杞借鏍囨槑鍑哄錛歨ttp://blog.csdn.net/onlymilan/archive/2006/07/20/947652.aspx