java中常用的工具类(一)

我们java程序员在开发项目的是常常会用到一些工具类。今天我汇总了一下java中常用的工具方法。大家可以在项目中使用。可以收藏!加入IT江湖官方群:383126909 我们一起成长

一、String工具类

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

package com.itjh.javaUtil;

import java.util.ArrayList;

import java.util.List;

/**

*

* String工具类. <br>

*

* @author 宋立君

* @date 2014年06月24日

*/

public class StringUtil {

private static final int INDEX_NOT_FOUND = -1;

private static final String EMPTY = "";

/**

* <p>

* The maximum size to which the padding constant(s) can expand.

* </p>

*/

private static final int PAD_LIMIT = 8192;

/**

* 功能:将半角的符号转换成全角符号.(即英文字符转中文字符)

*

* @author 宋立君

* @param str

*            源字符串

* @return String

* @date 2014年06月24日

*/

public static String changeToFull(String str) {

String source = "[email protected]#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+\\|[];:‘\",<.>/?";

String[] decode = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",

"!", "@", "#", "$", "%", "︿", "&", "*", "(", ")", "a", "b",

"c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",

"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",

"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",

"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",

"Y", "Z", "-", "_", "=", "+", "\", "|", "【", "】", ";", ":",

"‘", "\"", ",", "〈", "。", "〉", "/", "?" };

String result = "";

for (int i = 0; i < str.length(); i++) {

int pos = source.indexOf(str.charAt(i));

if (pos != -1) {

result += decode[pos];

} else {

result += str.charAt(i);

}

}

return result;

}

/**

* 功能:cs串中是否一个都不包含字符数组searchChars中的字符。

*

* @author 宋立君

* @param cs

*            字符串

* @param searchChars

*            字符数组

* @return boolean 都不包含返回true,否则返回false。

* @date 2014年06月24日

*/

public static boolean containsNone(CharSequence cs, char... searchChars) {

if (cs == null || searchChars == null) {

return true;

}

int csLen = cs.length();

int csLast = csLen - 1;

int searchLen = searchChars.length;

int searchLast = searchLen - 1;

for (int i = 0; i < csLen; i++) {

char ch = cs.charAt(i);

for (int j = 0; j < searchLen; j++) {

if (searchChars[j] == ch) {

if (Character.isHighSurrogate(ch)) {

if (j == searchLast) {

// missing low surrogate, fine, like

// String.indexOf(String)

return false;

}

if (i < csLast

&& searchChars[j + 1] == cs.charAt(i + 1)) {

return false;

}

} else {

// ch is in the Basic Multilingual Plane

return false;

}

}

}

}

return true;

}

/**

* <p>

* 编码为Unicode,格式 ‘\u0020‘.

* </p>

*

* @author 宋立君

*

*         <pre>

*   CharUtils.unicodeEscaped(‘ ‘) = "\u0020"

*   CharUtils.unicodeEscaped(‘A‘) = "\u0041"

* </pre>

*

* @param ch

*            源字符串

* @return 转码后的字符串

* @date 2014年06月24日

*/

public static String unicodeEscaped(char ch) {

if (ch < 0x10) {

return "\\u000" + Integer.toHexString(ch);

} else if (ch < 0x100) {

return "\\u00" + Integer.toHexString(ch);

} else if (ch < 0x1000) {

return "\\u0" + Integer.toHexString(ch);

}

return "\\u" + Integer.toHexString(ch);

}

/**

* <p>

* 进行tostring操作,如果传入的是null,返回空字符串。

* </p>

*

* <pre>

* ObjectUtils.toString(null)         = ""

* ObjectUtils.toString("")           = ""

* ObjectUtils.toString("bat")        = "bat"

* ObjectUtils.toString(Boolean.TRUE) = "true"

* </pre>

*

* @param obj

*            源

* @return String

*/

public static String toString(Object obj) {

return obj == null ? "" : obj.toString();

}

/**

* <p>

* 进行tostring操作,如果传入的是null,返回指定的默认值。

* </p>

*

* <pre>

* ObjectUtils.toString(null, null)           = null

* ObjectUtils.toString(null, "null")         = "null"

* ObjectUtils.toString("", "null")           = ""

* ObjectUtils.toString("bat", "null")        = "bat"

* ObjectUtils.toString(Boolean.TRUE, "null") = "true"

* </pre>

*

* @param obj

*            源

* @param nullStr

*            如果obj为null时返回这个指定值

* @return String

*/

public static String toString(Object obj, String nullStr) {

return obj == null ? nullStr : obj.toString();

}

/**

* <p>

* 只从源字符串中移除指定开头子字符串.

* </p>

*

* <pre>

* StringUtil.removeStart(null, *)      = null

* StringUtil.removeStart("", *)        = ""

* StringUtil.removeStart(*, null)      = *

* StringUtil.removeStart("www.domain.com", "www.")   = "domain.com"

* StringUtil.removeStart("domain.com", "www.")       = "domain.com"

* StringUtil.removeStart("www.domain.com", "domain") = "www.domain.com"

* StringUtil.removeStart("abc", "")    = "abc"

* </pre>

*

* @param str

*            源字符串

* @param remove

*            将要被移除的子字符串

* @return String

*/

public static String removeStart(String str, String remove) {

if (isEmpty(str) || isEmpty(remove)) {

return str;

}

if (str.startsWith(remove)) {

return str.substring(remove.length());

}

return str;

}

/**

* <p>

* 只从源字符串中移除指定结尾的子字符串.

* </p>

*

* <pre>

* StringUtil.removeEnd(null, *)      = null

* StringUtil.removeEnd("", *)        = ""

* StringUtil.removeEnd(*, null)      = *

* StringUtil.removeEnd("www.domain.com", ".com.")  = "www.domain.com"

* StringUtil.removeEnd("www.domain.com", ".com")   = "www.domain"

* StringUtil.removeEnd("www.domain.com", "domain") = "www.domain.com"

* StringUtil.removeEnd("abc", "")    = "abc"

* </pre>

*

* @param str

*            源字符串

* @param remove

*            将要被移除的子字符串

* @return String

*/

public static String removeEnd(String str, String remove) {

if (isEmpty(str) || isEmpty(remove)) {

return str;

}

if (str.endsWith(remove)) {

return str.substring(0, str.length() - remove.length());

}

return str;

}

/**

* <p>

* 将一个字符串重复N次

* </p>

*

* <pre>

* StringUtil.repeat(null, 2) = null

* StringUtil.repeat("", 0)   = ""

* StringUtil.repeat("", 2)   = ""

* StringUtil.repeat("a", 3)  = "aaa"

* StringUtil.repeat("ab", 2) = "abab"

* StringUtil.repeat("a", -2) = ""

* </pre>

*

* @param str

*            源字符串

* @param repeat

*            重复的次数

* @return String

*/

public static String repeat(String str, int repeat) {

// Performance tuned for 2.0 (JDK1.4)

if (str == null) {

return null;

}

if (repeat <= 0) {

return EMPTY;

}

int inputLength = str.length();

if (repeat == 1 || inputLength == 0) {

return str;

}

if (inputLength == 1 && repeat <= PAD_LIMIT) {

return repeat(str.charAt(0), repeat);

}

int outputLength = inputLength * repeat;

switch (inputLength) {

case 1:

return repeat(str.charAt(0), repeat);

case 2:

char ch0 = str.charAt(0);

char ch1 = str.charAt(1);

char[] output2 = new char[outputLength];

for (int i = repeat * 2 - 2; i >= 0; i--, i--) {

output2[i] = ch0;

output2[i + 1] = ch1;

}

return new String(output2);

default:

StringBuilder buf = new StringBuilder(outputLength);

for (int i = 0; i < repeat; i++) {

buf.append(str);

}

return buf.toString();

}

}

/**

* <p>

* 将一个字符串重复N次,并且中间加上指定的分隔符

* </p>

*

* <pre>

* StringUtil.repeat(null, null, 2) = null

* StringUtil.repeat(null, "x", 2)  = null

* StringUtil.repeat("", null, 0)   = ""

* StringUtil.repeat("", "", 2)     = ""

* StringUtil.repeat("", "x", 3)    = "xxx"

* StringUtil.repeat("?", ", ", 3)  = "?, ?, ?"

* </pre>

*

* @param str

*            源字符串

* @param separator

*            分隔符

* @param repeat

*            重复次数

* @return String

*/

public static String repeat(String str, String separator, int repeat) {

if (str == null || separator == null) {

return repeat(str, repeat);

} else {

// given that repeat(String, int) is quite optimized, better to rely

// on it than try and splice this into it

String result = repeat(str + separator, repeat);

return removeEnd(result, separator);

}

}

/**

* <p>

* 将某个字符重复N次.

* </p>

*

* @param ch

*            某个字符

* @param repeat

*            重复次数

* @return String

*/

public static String repeat(char ch, int repeat) {

char[] buf = new char[repeat];

for (int i = repeat - 1; i >= 0; i--) {

buf[i] = ch;

}

return new String(buf);

}

/**

* <p>

* 字符串长度达不到指定长度时,在字符串右边补指定的字符.

* </p>

*

* <pre>

* StringUtil.rightPad(null, *, *)     = null

* StringUtil.rightPad("", 3, ‘z‘)     = "zzz"

* StringUtil.rightPad("bat", 3, ‘z‘)  = "bat"

* StringUtil.rightPad("bat", 5, ‘z‘)  = "batzz"

* StringUtil.rightPad("bat", 1, ‘z‘)  = "bat"

* StringUtil.rightPad("bat", -1, ‘z‘) = "bat"

* </pre>

*

* @param str

*            源字符串

* @param size

*            指定的长度

* @param padChar

*            进行补充的字符

* @return String

*/

public static String rightPad(String str, int size, char padChar) {

if (str == null) {

return null;

}

int pads = size - str.length();

if (pads <= 0) {

return str; // returns original String when possible

}

if (pads > PAD_LIMIT) {

return rightPad(str, size, String.valueOf(padChar));

}

return str.concat(repeat(padChar, pads));

}

/**

* <p>

* 扩大字符串长度,从左边补充指定字符

* </p>

*

* <pre>

* StringUtil.rightPad(null, *, *)      = null

* StringUtil.rightPad("", 3, "z")      = "zzz"

* StringUtil.rightPad("bat", 3, "yz")  = "bat"

* StringUtil.rightPad("bat", 5, "yz")  = "batyz"

* StringUtil.rightPad("bat", 8, "yz")  = "batyzyzy"

* StringUtil.rightPad("bat", 1, "yz")  = "bat"

* StringUtil.rightPad("bat", -1, "yz") = "bat"

* StringUtil.rightPad("bat", 5, null)  = "bat  "

* StringUtil.rightPad("bat", 5, "")    = "bat  "

* </pre>

*

* @param str

*            源字符串

* @param size

*            扩大后的长度

* @param padStr

*            在右边补充的字符串

* @return String

*/

public static String rightPad(String str, int size, String padStr) {

if (str == null) {

return null;

}

if (isEmpty(padStr)) {

padStr = " ";

}

int padLen = padStr.length();

int strLen = str.length();

int pads = size - strLen;

if (pads <= 0) {

return str; // returns original String when possible

}

if (padLen == 1 && pads <= PAD_LIMIT) {

return rightPad(str, size, padStr.charAt(0));

}

if (pads == padLen) {

return str.concat(padStr);

} else if (pads < padLen) {

return str.concat(padStr.substring(0, pads));

} else {

char[] padding = new char[pads];

char[] padChars = padStr.toCharArray();

for (int i = 0; i < pads; i++) {

padding[i] = padChars[i % padLen];

}

return str.concat(new String(padding));

}

}

/**

* <p>

* 扩大字符串长度,从左边补充空格

* </p>

*

* <pre>

* StringUtil.leftPad(null, *)   = null

* StringUtil.leftPad("", 3)     = "   "

* StringUtil.leftPad("bat", 3)  = "bat"

* StringUtil.leftPad("bat", 5)  = "  bat"

* StringUtil.leftPad("bat", 1)  = "bat"

* StringUtil.leftPad("bat", -1) = "bat"

* </pre>

*

* @param str

*            源字符串

* @param size

*            扩大后的长度

* @return String

*/

public static String leftPad(String str, int size) {

return leftPad(str, size, ‘ ‘);

}

/**

* <p>

* 扩大字符串长度,从左边补充指定的字符

* </p>

*

* <pre>

* StringUtil.leftPad(null, *, *)     = null

* StringUtil.leftPad("", 3, ‘z‘)     = "zzz"

* StringUtil.leftPad("bat", 3, ‘z‘)  = "bat"

* StringUtil.leftPad("bat", 5, ‘z‘)  = "zzbat"

* StringUtil.leftPad("bat", 1, ‘z‘)  = "bat"

* StringUtil.leftPad("bat", -1, ‘z‘) = "bat"

* </pre>

*

* @param str

*            源字符串

* @param size

*            扩大后的长度

* @param padStr

*            补充的字符

* @return String

*/

public static String leftPad(String str, int size, char padChar) {

if (str == null) {

return null;

}

int pads = size - str.length();

if (pads <= 0) {

return str; // returns original String when possible

}

if (pads > PAD_LIMIT) {

return leftPad(str, size, String.valueOf(padChar));

}

return repeat(padChar, pads).concat(str);

}

/**

* <p>

* 扩大字符串长度,从左边补充指定的字符

* </p>

*

* <pre>

* StringUtil.leftPad(null, *, *)      = null

* StringUtil.leftPad("", 3, "z")      = "zzz"

* StringUtil.leftPad("bat", 3, "yz")  = "bat"

* StringUtil.leftPad("bat", 5, "yz")  = "yzbat"

* StringUtil.leftPad("bat", 8, "yz")  = "yzyzybat"

* StringUtil.leftPad("bat", 1, "yz")  = "bat"

* StringUtil.leftPad("bat", -1, "yz") = "bat"

* StringUtil.leftPad("bat", 5, null)  = "  bat"

* StringUtil.leftPad("bat", 5, "")    = "  bat"

* </pre>

*

* @param str

*            源字符串

* @param size

*            扩大后的长度

* @param padStr

*            补充的字符串

* @return String

*/

public static String leftPad(String str, int size, String padStr) {

if (str == null) {

return null;

}

if (isEmpty(padStr)) {

padStr = " ";

}

int padLen = padStr.length();

int strLen = str.length();

int pads = size - strLen;

if (pads <= 0) {

return str; // returns original String when possible

}

if (padLen == 1 && pads <= PAD_LIMIT) {

return leftPad(str, size, padStr.charAt(0));

}

if (pads == padLen) {

return padStr.concat(str);

} else if (pads < padLen) {

return padStr.substring(0, pads).concat(str);

} else {

char[] padding = new char[pads];

char[] padChars = padStr.toCharArray();

for (int i = 0; i < pads; i++) {

padding[i] = padChars[i % padLen];

}

return new String(padding).concat(str);

}

}

/**

* <p>

* 扩大字符串长度并将现在的字符串居中,被扩大部分用空格填充。

* <p>

*

* <pre>

* StringUtil.center(null, *)   = null

* StringUtil.center("", 4)     = "    "

* StringUtil.center("ab", -1)  = "ab"

* StringUtil.center("ab", 4)   = " ab "

* StringUtil.center("abcd", 2) = "abcd"

* StringUtil.center("a", 4)    = " a  "

* </pre>

*

* @param str

*            源字符串

* @param size

*            扩大后的长度

* @return String

*/

public static String center(String str, int size) {

return center(str, size, ‘ ‘);

}

/**

* <p>

* 将字符串长度修改为指定长度,并进行居中显示。

* </p>

*

* <pre>

* StringUtil.center(null, *, *)     = null

* StringUtil.center("", 4, ‘ ‘)     = "    "

* StringUtil.center("ab", -1, ‘ ‘)  = "ab"

* StringUtil.center("ab", 4, ‘ ‘)   = " ab"

* StringUtil.center("abcd", 2, ‘ ‘) = "abcd"

* StringUtil.center("a", 4, ‘ ‘)    = " a  "

* StringUtil.center("a", 4, ‘y‘)    = "yayy"

* </pre>

*

* @param str

*            源字符串

* @param size

*            指定的长度

* @param padStr

*            长度不够时补充的字符串

* @return String

* @throws IllegalArgumentException

*             如果被补充字符串为 null或者 empty

*/

public static String center(String str, int size, char padChar) {

if (str == null || size <= 0) {

return str;

}

int strLen = str.length();

int pads = size - strLen;

if (pads <= 0) {

return str;

}

str = leftPad(str, strLen + pads / 2, padChar);

str = rightPad(str, size, padChar);

return str;

}

/**

* <p>

* 将字符串长度修改为指定长度,并进行居中显示。

* </p>

*

* <pre>

* StringUtil.center(null, *, *)     = null

* StringUtil.center("", 4, " ")     = "    "

* StringUtil.center("ab", -1, " ")  = "ab"

* StringUtil.center("ab", 4, " ")   = " ab"

* StringUtil.center("abcd", 2, " ") = "abcd"

* StringUtil.center("a", 4, " ")    = " a  "

* StringUtil.center("a", 4, "yz")   = "yayz"

* StringUtil.center("abc", 7, null) = "  abc  "

* StringUtil.center("abc", 7, "")   = "  abc  "

* </pre>

*

* @param str

*            源字符串

* @param size

*            指定的长度

* @param padStr

*            长度不够时补充的字符串

* @return String

* @throws IllegalArgumentException

*             如果被补充字符串为 null或者 empty

*/

public static String center(String str, int size, String padStr) {

if (str == null || size <= 0) {

return str;

}

if (isEmpty(padStr)) {

padStr = " ";

}

int strLen = str.length();

int pads = size - strLen;

if (pads <= 0) {

return str;

}

str = leftPad(str, strLen + pads / 2, padStr);

str = rightPad(str, size, padStr);

return str;

}

/**

* <p>

* 检查字符串是否全部为小写.

* </p>

*

* <pre>

* StringUtil.isAllLowerCase(null)   = false

* StringUtil.isAllLowerCase("")     = false

* StringUtil.isAllLowerCase("  ")   = false

* StringUtil.isAllLowerCase("abc")  = true

* StringUtil.isAllLowerCase("abC") = false

* </pre>

*

* @param cs

*            源字符串

* @return String

*/

public static boolean isAllLowerCase(String cs) {

if (cs == null || isEmpty(cs)) {

return false;

}

int sz = cs.length();

for (int i = 0; i < sz; i++) {

if (Character.isLowerCase(cs.charAt(i)) == false) {

return false;

}

}

return true;

}

/**

* <p>

* 检查是否都是大写.

* </p>

*

* <pre>

* StringUtil.isAllUpperCase(null)   = false

* StringUtil.isAllUpperCase("")     = false

* StringUtil.isAllUpperCase("  ")   = false

* StringUtil.isAllUpperCase("ABC")  = true

* StringUtil.isAllUpperCase("aBC") = false

* </pre>

*

* @param cs

*            源字符串

* @return String

*/

public static boolean isAllUpperCase(String cs) {

if (cs == null || StringUtil.isEmpty(cs)) {

return false;

}

int sz = cs.length();

for (int i = 0; i < sz; i++) {

if (Character.isUpperCase(cs.charAt(i)) == false) {

return false;

}

}

return true;

}

/**

* <p>

* 反转字符串.

* </p>

*

* <pre>

* StringUtil.reverse(null)  = null

* StringUtil.reverse("")    = ""

* StringUtil.reverse("bat") = "tab"

* </pre>

*

* @param str

*            源字符串

* @return String

*/

public static String reverse(String str) {

if (str == null) {

return null;

}

return new StringBuilder(str).reverse().toString();

}

/**

* <p>

* 字符串达不到一定长度时在右边补空白.

* </p>

*

* <pre>

* StringUtil.rightPad(null, *)   = null

* StringUtil.rightPad("", 3)     = "   "

* StringUtil.rightPad("bat", 3)  = "bat"

* StringUtil.rightPad("bat", 5)  = "bat  "

* StringUtil.rightPad("bat", 1)  = "bat"

* StringUtil.rightPad("bat", -1) = "bat"

* </pre>

*

* @param str

*            源字符串

* @param size

*            指定的长度

* @return String

*/

public static String rightPad(String str, int size) {

return rightPad(str, size, ‘ ‘);

}

/**

* 从右边截取字符串.</p>

*

* <pre>

* StringUtil.right(null, *)    = null

* StringUtil.right(*, -ve)     = ""

* StringUtil.right("", *)      = ""

* StringUtil.right("abc", 0)   = ""

* StringUtil.right("abc", 2)   = "bc"

* StringUtil.right("abc", 4)   = "abc"

* </pre>

*

* @param str

*            源字符串

* @param len

*            长度

* @return String

*/

public static String right(String str, int len) {

if (str == null) {

return null;

}

if (len < 0) {

return EMPTY;

}

if (str.length() <= len) {

return str;

}

return str.substring(str.length() - len);

}

/**

* <p>

* 截取一个字符串的前几个.

* </p>

*

* <pre>

* StringUtil.left(null, *)    = null

* StringUtil.left(*, -ve)     = ""

* StringUtil.left("", *)      = ""

* StringUtil.left("abc", 0)   = ""

* StringUtil.left("abc", 2)   = "ab"

* StringUtil.left("abc", 4)   = "abc"

* </pre>

*

* @param str

*            源字符串

* @param len

*            截取的长度

* @return the String

*/

public static String left(String str, int len) {

if (str == null) {

return null;

}

if (len < 0) {

return EMPTY;

}

if (str.length() <= len) {

return str;

}

return str.substring(0, len);

}

/**

* <p>

* 得到tag字符串中间的子字符串,只返回第一个匹配项。

* </p>

*

* <pre>

* StringUtil.substringBetween(null, *)            = null

* StringUtil.substringBetween("", "")             = ""

* StringUtil.substringBetween("", "tag")          = null

* StringUtil.substringBetween("tagabctag", null)  = null

* StringUtil.substringBetween("tagabctag", "")    = ""

* StringUtil.substringBetween("tagabctag", "tag") = "abc"

* </pre>

*

* @param str

*            源字符串。

* @param tag

*            标识字符串。

* @return String 子字符串, 如果没有符合要求的,返回{@code null}。

*/

public static String substringBetween(String str, String tag) {

return substringBetween(str, tag, tag);

}

/**

* <p>

* 得到两个字符串中间的子字符串,只返回第一个匹配项。

* </p>

*

* <pre>

* StringUtil.substringBetween("wx[b]yz", "[", "]") = "b"

* StringUtil.substringBetween(null, *, *)          = null

* StringUtil.substringBetween(*, null, *)          = null

* StringUtil.substringBetween(*, *, null)          = null

* StringUtil.substringBetween("", "", "")          = ""

* StringUtil.substringBetween("", "", "]")         = null

* StringUtil.substringBetween("", "[", "]")        = null

* StringUtil.substringBetween("yabcz", "", "")     = ""

* StringUtil.substringBetween("yabcz", "y", "z")   = "abc"

* StringUtil.substringBetween("yabczyabcz", "y", "z")   = "abc"

* </pre>

*

* @param str

*            源字符串

* @param open

*            起字符串。

* @param close

*            末字符串。

* @return String 子字符串, 如果没有符合要求的,返回{@code null}。

*/

public static String substringBetween(String str, String open, String close) {

if (str == null || open == null || close == null) {

return null;

}

int start = str.indexOf(open);

if (start != INDEX_NOT_FOUND) {

int end = str.indexOf(close, start + open.length());

if (end != INDEX_NOT_FOUND) {

return str.substring(start + open.length(), end);

}

}

return null;

}

/**

* <p>

* 得到两个字符串中间的子字符串,所有匹配项组合为数组并返回。

* </p>

*

* <pre>

* StringUtil.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"]

* StringUtil.substringsBetween(null, *, *)            = null

* StringUtil.substringsBetween(*, null, *)            = null

* StringUtil.substringsBetween(*, *, null)            = null

* StringUtil.substringsBetween("", "[", "]")          = []

* </pre>

*

* @param str

*            源字符串

* @param open

*            起字符串。

* @param close

*            末字符串。

* @return String 子字符串数组, 如果没有符合要求的,返回{@code null}。

*/

public static String[] substringsBetween(String str, String open,

String close) {

if (str == null || isEmpty(open) || isEmpty(close)) {

return null;

}

int strLen = str.length();

if (strLen == 0) {

return new String[0];

}

int closeLen = close.length();

int openLen = open.length();

List<String> list = new ArrayList<String>();

int pos = 0;

while (pos < strLen - closeLen) {

int start = str.indexOf(open, pos);

if (start < 0) {

break;

}

start += openLen;

int end = str.indexOf(close, start);

if (end < 0) {

break;

}

list.add(str.substring(start, end));

pos = end + closeLen;

}

if (list.isEmpty()) {

return null;

}

return list.toArray(new String[list.size()]);

}

/**

* 功能:切换字符串中的所有字母大小写。<br/>

*

* <pre>

* StringUtil.swapCase(null)                 = null

* StringUtil.swapCase("")                   = ""

* StringUtil.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"

* </pre>

*

*

* @param str

*            源字符串

* @return String

*/

public static String swapCase(String str) {

if (StringUtil.isEmpty(str)) {

return str;

}

char[] buffer = str.toCharArray();

boolean whitespace = true;

for (int i = 0; i < buffer.length; i++) {

char ch = buffer[i];

if (Character.isUpperCase(ch)) {

buffer[i] = Character.toLowerCase(ch);

whitespace = false;

} else if (Character.isTitleCase(ch)) {

buffer[i] = Character.toLowerCase(ch);

whitespace = false;

} else if (Character.isLowerCase(ch)) {

if (whitespace) {

buffer[i] = Character.toTitleCase(ch);

whitespace = false;

} else {

buffer[i] = Character.toUpperCase(ch);

}

} else {

whitespace = Character.isWhitespace(ch);

}

}

return new String(buffer);

}

/**

* 功能:截取出最后一个标志位之后的字符串.<br/>

* 如果sourceStr为empty或者expr为null,直接返回源字符串。<br/>

* 如果expr长度为0,直接返回sourceStr。<br/>

* 如果expr在sourceStr中不存在,直接返回sourceStr。<br/>

*

* @author 宋立君

* @date 2014年06月24日

* @param sourceStr

*            被截取的字符串

* @param expr

*            分隔符

* @return String

*/

public static String substringAfterLast(String sourceStr, String expr) {

if (isEmpty(sourceStr) || expr == null) {

return sourceStr;

}

if (expr.length() == 0) {

return sourceStr;

}

int pos = sourceStr.lastIndexOf(expr);

if (pos == -1) {

return sourceStr;

}

return sourceStr.substring(pos + expr.length());

}

/**

* 功能:截取出最后一个标志位之前的字符串.<br/>

* 如果sourceStr为empty或者expr为null,直接返回源字符串。<br/>

* 如果expr长度为0,直接返回sourceStr。<br/>

* 如果expr在sourceStr中不存在,直接返回sourceStr。<br/>

*

* @author 宋立君

* @date 2014年06月24日

* @param sourceStr

*            被截取的字符串

* @param expr

*            分隔符

* @return String

*/

public static String substringBeforeLast(String sourceStr, String expr) {

if (isEmpty(sourceStr) || expr == null) {

return sourceStr;

}

if (expr.length() == 0) {

return sourceStr;

}

int pos = sourceStr.lastIndexOf(expr);

if (pos == -1) {

return sourceStr;

}

return sourceStr.substring(0, pos);

}

/**

* 功能:截取出第一个标志位之后的字符串.<br/>

* 如果sourceStr为empty或者expr为null,直接返回源字符串。<br/>

* 如果expr长度为0,直接返回sourceStr。<br/>

* 如果expr在sourceStr中不存在,直接返回sourceStr。<br/>

*

* @author 宋立君

* @date 2014年06月24日

* @param sourceStr

*            被截取的字符串

* @param expr

*            分隔符

* @return String

*/

public static String substringAfter(String sourceStr, String expr) {

if (isEmpty(sourceStr) || expr == null) {

return sourceStr;

}

if (expr.length() == 0) {

return sourceStr;

}

int pos = sourceStr.indexOf(expr);

if (pos == -1) {

return sourceStr;

}

return sourceStr.substring(pos + expr.length());

}

/**

* 功能:截取出第一个标志位之前的字符串.<br/>

* 如果sourceStr为empty或者expr为null,直接返回源字符串。<br/>

* 如果expr长度为0,直接返回sourceStr。<br/>

* 如果expr在sourceStr中不存在,直接返回sourceStr。<br/>

* 如果expr在sourceStr中存在不止一个,以第一个位置为准。

*

* @author 宋立君

* @date 2014年06月24日

* @param sourceStr

*            被截取的字符串

* @param expr

*            分隔符

* @return String

*/

public static String substringBefore(String sourceStr, String expr) {

if (isEmpty(sourceStr) || expr == null) {

return sourceStr;

}

if (expr.length() == 0) {

return sourceStr;

}

int pos = sourceStr.indexOf(expr);

if (pos == -1) {

return sourceStr;

}

return sourceStr.substring(0, pos);

}

/**

* 功能:检查这个字符串是不是空字符串。<br/>

* 如果这个字符串为null或者trim后为空字符串则返回true,否则返回false。

*

* @author 宋立君

* @date 2014年06月24日

* @param chkStr

*            被检查的字符串

* @return boolean

*/

public static boolean isEmpty(String chkStr) {

if (chkStr == null) {

return true;

} else {

return "".equals(chkStr.trim()) ? true : false;

}

}

/**

* 如果字符串没有超过最长显示长度返回原字符串,否则从开头截取指定长度并加...返回。

*

* @param str

*            原字符串

* @param length

*            字符串最长显示的长度

* @return 转换后的字符串

*/

public static String trimString(String str, int length) {

if (str == null) {

return "";

} else if (str.length() > length) {

return str.substring(0, length - 3) + "...";

} else {

return str;

}

}

}

二、MD5

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

package com.itjh.javaUtil;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

/**

*

* String工具类. <br>

*

* @author 宋立君

* @date 2014年06月24日

*/

public class MD5Util {

protected static char hexDigits[] = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘,

‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ };

protected static MessageDigest messagedigest = null;

static {

try {

messagedigest = MessageDigest.getInstance("MD5");

} catch (NoSuchAlgorithmException nsaex) {

System.err.println(MD5Util.class.getName()

+ "初始化失败,MessageDigest不支持MD5Util。");

nsaex.printStackTrace();

}

}

/**

* 功能:加盐版的MD5.返回格式为MD5(密码+{盐值})

*

* @author 宋立君

* @date 2014年06月24日

* @param password

*            密码

* @param salt

*            盐值

* @return String

*/

public static String getMD5StringWithSalt(String password, String salt) {

if (password == null) {

throw new IllegalArgumentException("password不能为null");

}

if (StringUtil.isEmpty(salt)) {

throw new IllegalArgumentException("salt不能为空");

}

if ((salt.toString().lastIndexOf("{") != -1)

|| (salt.toString().lastIndexOf("}") != -1)) {

throw new IllegalArgumentException("salt中不能包含 { 或者 }");

}

return getMD5String(password + "{" + salt.toString() + "}");

}

/**

* 功能:得到文件的md5值。

*

* @author 宋立君

* @date 2014年06月24日

* @param file

*            文件。

* @return String

* @throws IOException

*             读取文件IO异常时。

*/

public static String getFileMD5String(File file) throws IOException {

FileInputStream in = new FileInputStream(file);

FileChannel ch = in.getChannel();

MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,

file.length());

messagedigest.update(byteBuffer);

return bufferToHex(messagedigest.digest());

}

/**

* 功能:得到一个字符串的MD5值。

*

* @author 宋立君

* @date 2014年06月24日

* @param str

*            字符串

* @return String

*/

public static String getMD5String(String str) {

return getMD5String(str.getBytes());

}

private static String getMD5String(byte[] bytes) {

messagedigest.update(bytes);

return bufferToHex(messagedigest.digest());

}

private static String bufferToHex(byte bytes[]) {

return bufferToHex(bytes, 0, bytes.length);

}

private static String bufferToHex(byte bytes[], int m, int n) {

StringBuffer stringbuffer = new StringBuffer(2 * n);

int k = m + n;

for (int l = m; l < k; l++) {

appendHexPair(bytes[l], stringbuffer);

}

return stringbuffer.toString();

}

private static void appendHexPair(byte bt, StringBuffer stringbuffer) {

char c0 = hexDigits[(bt & 0xf0) >> 4];

char c1 = hexDigits[bt & 0xf];

stringbuffer.append(c0);

stringbuffer.append(c1);

}

}

三、File工具类

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

package com.itjh.javaUtil;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

/**

* 文件相关操作辅助类。

*

* @author 宋立君

* @date 2014年06月24日

*/

public class FileUtil {

private static final String FOLDER_SEPARATOR = "/";

private static final char EXTENSION_SEPARATOR = ‘.‘;

/**

* 功能:复制文件或者文件夹。

*

* @author 宋立君

* @date 2014年06月24日

* @param inputFile

*            源文件

* @param outputFile

*            目的文件

* @param isOverWrite

*            是否覆盖(只针对文件)

* @throws IOException

*/

public static void copy(File inputFile, File outputFile, boolean isOverWrite)

throws IOException {

if (!inputFile.exists()) {

throw new RuntimeException(inputFile.getPath() + "源目录不存在!");

}

copyPri(inputFile, outputFile, isOverWrite);

}

/**

* 功能:为copy 做递归使用。

*

* @author 宋立君

* @date 2014年06月24日

* @param inputFile

* @param outputFile

* @param isOverWrite

* @throws IOException

*/

private static void copyPri(File inputFile, File outputFile,

boolean isOverWrite) throws IOException {

// 是个文件。

if (inputFile.isFile()) {

copySimpleFile(inputFile, outputFile, isOverWrite);

} else {

// 文件夹

if (!outputFile.exists()) {

outputFile.mkdir();

}

// 循环子文件夹

for (File child : inputFile.listFiles()) {

copy(child,

new File(outputFile.getPath() + "/" + child.getName()),

isOverWrite);

}

}

}

/**

* 功能:copy单个文件

*

* @author 宋立君

* @date 2014年06月24日

* @param inputFile

*            源文件

* @param outputFile

*            目标文件

* @param isOverWrite

*            是否允许覆盖

* @throws IOException

*/

private static void copySimpleFile(File inputFile, File outputFile,

boolean isOverWrite) throws IOException {

// 目标文件已经存在

if (outputFile.exists()) {

if (isOverWrite) {

if (!outputFile.delete()) {

throw new RuntimeException(outputFile.getPath() + "无法覆盖!");

}

} else {

// 不允许覆盖

return;

}

}

InputStream in = new FileInputStream(inputFile);

OutputStream out = new FileOutputStream(outputFile);

byte[] buffer = new byte[1024];

int read = 0;

while ((read = in.read(buffer)) != -1) {

out.write(buffer, 0, read);

}

in.close();

out.close();

}

/**

* 功能:删除文件

*

* @author 宋立君

* @date 2014年06月24日

* @param file

*            文件

*/

public static void delete(File file) {

deleteFile(file);

}

/**

* 功能:删除文件,内部递归使用

*

* @author 宋立君

* @date 2014年06月24日

* @param file

*            文件

* @return boolean true 删除成功,false 删除失败。

*/

private static void deleteFile(File file) {

if (file == null || !file.exists()) {

return;

}

// 单文件

if (!file.isDirectory()) {

boolean delFlag = file.delete();

if (!delFlag) {

throw new RuntimeException(file.getPath() + "删除失败!");

} else {

return;

}

}

// 删除子目录

for (File child : file.listFiles()) {

deleteFile(child);

}

// 删除自己

file.delete();

}

/**

* 从文件路径中抽取文件的扩展名, 例如. "mypath/myfile.txt" -> "txt". * @author 宋立君

*

* @date 2014年06月24日

* @param 文件路径

* @return 如果path为null,直接返回null。

*/

public static String getFilenameExtension(String path) {

if (path == null) {

return null;

}

int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR);

if (extIndex == -1) {

return null;

}

int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);

if (folderIndex > extIndex) {

return null;

}

return path.substring(extIndex + 1);

}

/**

* 从文件路径中抽取文件名, 例如: "mypath/myfile.txt" -> "myfile.txt"。 * @author 宋立君

*

* @date 2014年06月24日

* @param path

*            文件路径。

* @return 抽取出来的文件名, 如果path为null,直接返回null。

*/

public static String getFilename(String path) {

if (path == null) {

return null;

}

int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);

return (separatorIndex != -1 ? path.substring(separatorIndex + 1)

: path);

}

/**

* 功能:保存文件。

*

* @author 宋立君

* @date 2014年06月24日

* @param content

*            字节

* @param file

*            保存到的文件

* @throws IOException

*/

public static void save(byte[] content, File file) throws IOException {

if (file == null) {

throw new RuntimeException("保存文件不能为空");

}

if (content == null) {

throw new RuntimeException("文件流不能为空");

}

InputStream is = new ByteArrayInputStream(content);

save(is, file);

}

/**

* 功能:保存文件

*

* @author 宋立君

* @date 2014年06月24日

* @param streamIn

*            文件流

* @param file

*            保存到的文件

* @throws IOException

*/

public static void save(InputStream streamIn, File file) throws IOException {

if (file == null) {

throw new RuntimeException("保存文件不能为空");

}

if (streamIn == null) {

throw new RuntimeException("文件流不能为空");

}

// 输出流

OutputStream streamOut = null;

// 文件夹不存在就创建。

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

streamOut = new FileOutputStream(file);

int bytesRead = 0;

byte[] buffer = new byte[8192];

while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {

streamOut.write(buffer, 0, bytesRead);

}

streamOut.close();

streamIn.close();

}

}

这些工具很不错的,都运行通过,会继续更新的!!

java中常用的工具类(一)

时间: 2024-08-01 10:44:14

java中常用的工具类(一)的相关文章

java中常用的工具类(二)

下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

java中常用的工具类(三)

继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

java中常用的工具类

一.String工具类 package com.itjh.javaUtil; import java.util.ArrayList; import java.util.List; /** * * String工具类. <br> * * @author 宋立君 * @date 2014年06月24日 */ public class StringUtil { private static final int INDEX_NOT_FOUND = -1; private static final St

Android中常用的工具类01

1.图片和视频缩略图工具类 import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ThumbnailUtils; /** * 缩略图生成工具类 * @author * */ public class ThumbnailGenerateUtils { private ThumbnailGenerateUtils(){}; /** * 根据指定的图像路径和大小来获取缩略图

Android中常用的工具类02

1.读取手机联系人信息 一般用在读取手机通讯录上传,这一块比较多. import java.util.ArrayList; import java.util.List; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract.CommonDataKinds.Phon

JAVA中封装JSONUtils工具类及使用

在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. 封装后的JSON工具类JSONUtils.java代码如下: JSONUtils代码,点击展开 import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Itera

java并发编程中常用的工具类 Executor

/***************************************************  * TODO: description .  * @author: gao_chun  * @since:  2015-4-17  * @version: 1.0.0  * @remark: 转载请注明出处  **************************************************/ java.util.concurrent.Executor 使用 Execut

第八章 Java中的并发工具类

等待多线程完成的CountDownLatch countDownLatch允许一个或多个线程等待其他线程完成操作. public class CountDownLatchTest { static CountDownLatch countDownLatch = new CountDownLatch(2); public static void main(String[] args) throws InterruptedException{ new Thread(new Runnable() {

Java中的并发工具类

1.等待多线程完成的CountDownLatch         CountDownLatch允许一个或多个线程等待其他线程完成操作.join用于让当前执行线程等待join线程执行结束.其实现原理是不停检查join线程是否存活,如果join线程存活则让当前线程永远等待.直到join线程中止后,线程的this.notifyAll()方法会被调用,调用notifyAll()方法是在JVM里实现的. 2.同步屏障CyclicBarrier                             Cyl