├── src
└── main
│ ├── resources
│ ├── thread.jpg
│ ├── thread3.jpg
│ ├── config.properties
│ ├── config_1.properties
│ ├── db.properties
│ └── config.xml
│ └── java
│ └── com
│ └── andieguo
│ ├── databasejdbc
│ ├── UserDao.java
│ ├── DBHelper.java
│ ├── User.java
│ └── DBConnection.java
│ ├── jsondemo
│ ├── book.json
│ ├── GsonDemo.java
│ └── Book.java
│ ├── xmldemo
│ ├── ZAgricultrue.xml
│ ├── createbooks.xml
│ ├── books.xml
│ ├── Book.java
│ ├── LoopXMLFile.java
│ ├── ReadXMLFile.java
│ └── CreateXMLFile.java
│ ├── iodemo
│ └── PrintSteamTester.java
│ ├── databasedemo
│ ├── config.properties
│ ├── DB.java
│ └── DBHelper.java
│ ├── generics
│ ├── Cat.java
│ ├── Magpie.java
│ ├── Bird.java
│ ├── ListFactory.java
│ ├── Animal.java
│ └── AnimalTest.java
│ ├── thread
│ ├── TimerDemo.java
│ ├── product
│ │ ├── ProducerConsumer.java
│ │ ├── Consumer.java
│ │ ├── Producer.java
│ │ ├── Product.java
│ │ └── ProductStack.java
│ ├── TimerTaskDemo.java
│ ├── ThreadSecurityDemo.java
│ ├── TimerTest.java
│ ├── VolatileTest.java
│ ├── RunTimeDemo.java
│ └── ThreadTest2.java
│ ├── reflectiondemo
│ ├── Person.java
│ └── DumpMethods.java
│ ├── utildemo
│ ├── HexTest.java
│ ├── DecimalFormatTest.java
│ └── EnumerationTester.java
│ ├── lang3
│ ├── TeacherBean.java
│ ├── SchoolBean.java
│ ├── SimpleJavaTester.java
│ └── UtilsTester.java
│ ├── collectionsdemo
│ ├── StackDemo.java
│ ├── IteratorTester.java
│ └── VectorTester.java
│ ├── stringdemo
│ └── StringTester.java
│ ├── trycatch
│ └── TryCatchTester.java
│ ├── arraydemo
│ └── ArrayTester.java
│ ├── saxparserdemo
│ ├── saxbooks.xml
│ ├── books.xml
│ ├── Book.java
│ ├── XMLParserSAX.java
│ ├── SaxHandler.java
│ └── CreateXMLFile.java
│ ├── enumdemo
│ └── EnumTester.java
│ ├── propertiesutil
│ ├── PropertyParseExceptionTest.java
│ ├── PropertyParseException.java
│ ├── PropertyRetrieverTest.java
│ └── PropertyRetriever.java
│ └── propertiesdemo
│ ├── PropertiesHelperTest.java
│ ├── PropertiesHelper.java
│ └── PropertiesTester.java
├── .gitignore
└── pom.xml
/src/main/resources/thread.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andieguo/JavaDemo/HEAD/src/main/resources/thread.jpg
--------------------------------------------------------------------------------
/src/main/resources/thread3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andieguo/JavaDemo/HEAD/src/main/resources/thread3.jpg
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/databasejdbc/UserDao.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.databasejdbc;
2 |
3 | public class UserDao {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/jsondemo/book.json:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andieguo/JavaDemo/HEAD/src/main/java/com/andieguo/jsondemo/book.json
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/xmldemo/ZAgricultrue.xml:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andieguo/JavaDemo/HEAD/src/main/java/com/andieguo/xmldemo/ZAgricultrue.xml
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/databasejdbc/DBHelper.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andieguo/JavaDemo/HEAD/src/main/java/com/andieguo/databasejdbc/DBHelper.java
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/iodemo/PrintSteamTester.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andieguo/JavaDemo/HEAD/src/main/java/com/andieguo/iodemo/PrintSteamTester.java
--------------------------------------------------------------------------------
/src/main/resources/config.properties:
--------------------------------------------------------------------------------
1 | #modifyThu May 08 16:23:31 CST 2014
2 | #Thu May 08 16:23:31 CST 2014
3 | url=jdbc:mysql://localhost:3306/
4 | username=root
5 | password=root
6 | database=bbs
7 |
--------------------------------------------------------------------------------
/src/main/resources/config_1.properties:
--------------------------------------------------------------------------------
1 | #andieguo modifyFri May 09 21:14:20 CST 2014
2 | #Fri May 09 21:14:20 CST 2014
3 | password=root
4 | url=jdbc\:mysql\://localhost\:3306/
5 | database=bbs
6 | username=root
7 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/databasedemo/config.properties:
--------------------------------------------------------------------------------
1 | #modifyThu May 08 16:23:31 CST 2014
2 | #Thu May 08 16:23:31 CST 2014
3 | url=jdbc:mysql://localhost:3306/
4 | username=root
5 | password=root
6 | database=bbs
7 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/generics/Cat.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.generics;
2 |
3 | public class Cat extends Animal {
4 |
5 | public Cat(String name) {
6 | super(name);
7 | }
8 |
9 | public void jump(){
10 | System.out.println(this.getName() + " can jump.");
11 | }
12 |
13 |
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | # Package Files #
7 | *.jar
8 | *.war
9 | *.ear
10 |
11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12 | hs_err_pid*
13 |
14 | bin/
15 | .settings/
16 | .classpath
17 | .project
18 | /target/
19 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/TimerDemo.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread;
2 |
3 | import java.util.Timer;
4 |
5 | public class TimerDemo {
6 | public static void main(String args[]){
7 | Timer timer = new Timer();
8 | System.out.println("-----------");
9 | timer.schedule(new TimerTaskDemo(), 2000, 1000);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/generics/Magpie.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.generics;
2 |
3 | public class Magpie extends Bird {
4 |
5 | public Magpie(String name) {
6 | super(name);
7 | // TODO Auto-generated constructor stub
8 | }
9 |
10 | public void sing(){
11 | System.out.println(getName() + " can not only eat,but sing");
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/resources/db.properties:
--------------------------------------------------------------------------------
1 | #mysql DB properties
2 | DB_DRIVER_CLASS=com.mysql.jdbc.Driver
3 | DB_URL=jdbc:mysql://localhost:3306/bbs
4 | DB_USERNAME=root
5 | DB_PASSWORD=root
6 |
7 | #Oracle DB Properties
8 | #DB_DRIVER_CLASS=oracle.jdbc.driver.OracleDriver
9 | #DB_URL=jdbc:oracle:thin:@localhost:1571:MyDBSID
10 | #DB_USERNAME=scott
11 | #DB_PASSWORD=tiger
12 |
--------------------------------------------------------------------------------
/src/main/resources/config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | modifyThu May 08 22:40:06 CST 2014
5 | andieguo
6 | 123456
7 | andy
8 | 3.14
9 |
10 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/generics/Bird.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.generics;
2 |
3 | public class Bird extends Animal {
4 |
5 | public Bird(String name) {
6 | super(name);
7 | // TODO Auto-generated constructor stub
8 | }
9 |
10 | public Bird(Integer size,Integer weight){
11 | super(size, weight);
12 | }
13 |
14 | public void fly(){
15 | System.out.println(this.getName() + " can fly.");
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/product/ProducerConsumer.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread.product;
2 |
3 | public class ProducerConsumer {
4 | public static void main(String[] args) {
5 | ProductStack ps = new ProductStack();
6 | Producer p = new Producer(ps, "11");
7 | Consumer c = new Consumer(ps, "12");
8 | new Thread(p).start();
9 | new Thread(c).start();
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/reflectiondemo/Person.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.reflectiondemo;
2 |
3 | public class Person {
4 |
5 | private String name;
6 | private String password;
7 | public String getName() {
8 | return name;
9 | }
10 | public void setName(String name) {
11 | this.name = name;
12 | }
13 | public String getPassword() {
14 | return password;
15 | }
16 | public void setPassword(String password) {
17 | this.password = password;
18 | }
19 |
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/utildemo/HexTest.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.utildemo;
2 |
3 | public class HexTest {
4 | static int[] blooddata = new int[4];
5 | public static void main(String[] args) {
6 | blooddata[0] = (0x3D & 0xff) | ((0x00 << 8) & 0xff00);
7 | blooddata[1] = (0x6A & 0xff) | ((0x00 << 8) & 0xff00);
8 | blooddata[2] = (blooddata[0]+blooddata[1])/2;
9 | blooddata[3] = (0x3e & 0xff);
10 | for(int i : blooddata){
11 | System.out.println(i);
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/TimerTaskDemo.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 | import java.util.TimerTask;
6 |
7 | public class TimerTaskDemo extends TimerTask{
8 |
9 | @Override
10 | public void run() {
11 | // TODO Auto-generated method stub
12 | SimpleDateFormat simpleDateFormat=null;
13 | simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
14 | System.out.println(""+simpleDateFormat.format(new Date()));
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/utildemo/DecimalFormatTest.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.utildemo;
2 |
3 | import java.text.DecimalFormat;
4 |
5 | public class DecimalFormatTest {
6 | public static void main(String[] args) {
7 | DecimalFormat df2= new DecimalFormat("0.0");
8 | System.out.println(df2.format(11111.111111));//1230.10
9 |
10 | DecimalFormat df4 = new DecimalFormat("#.00");
11 | System.out.println(df4.format(1230.1));//1230.10
12 |
13 | DecimalFormat df3 = new DecimalFormat("#.##");
14 | System.out.println(df3.format(1230.1));//1230.10
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/product/Consumer.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread.product;
2 |
3 | public class Consumer implements Runnable {
4 | String name;
5 |
6 | ProductStack ps = null;
7 |
8 | Consumer(ProductStack ps, String name) {
9 | this.ps = ps;
10 | this.name = name;
11 | }
12 |
13 | public void run() {
14 | for (int i = 0; i < 20; i++) {
15 | Product product = ps.pop(name);
16 | try {
17 | Thread.sleep((int) (Math.random() * 1000));
18 | } catch (InterruptedException e) {
19 | e.printStackTrace();
20 | }
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/product/Producer.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread.product;
2 |
3 | public class Producer implements Runnable {
4 | String name;
5 |
6 | ProductStack ps = null;
7 |
8 | Producer(ProductStack ps, String name) {
9 | this.ps = ps;
10 | this.name = name;
11 | }
12 |
13 | public void run() {
14 | for (int i = 0; i < 20; i++) {
15 | Product product = new Product(i, name);
16 | ps.push(product);
17 | try {
18 | Thread.sleep((int) (Math.random() * 200));
19 | } catch (InterruptedException e) {
20 | e.printStackTrace();
21 | }
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/utildemo/EnumerationTester.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.utildemo;
2 |
3 | import java.util.Enumeration;
4 | import java.util.Vector;
5 |
6 | public class EnumerationTester {
7 | public static void main(String args[]) {
8 | Enumeration days;
9 | Vector dayNames = new Vector();
10 | dayNames.add("Sunday");
11 | dayNames.add("Monday");
12 | dayNames.add("Tuesday");
13 | dayNames.add("Wednesday");
14 | dayNames.add("Thursday");
15 | dayNames.add("Friday");
16 | dayNames.add("Saturday");
17 | days = dayNames.elements();
18 | while (days.hasMoreElements()) {
19 | System.out.println(days.nextElement());
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/ThreadSecurityDemo.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread;
2 |
3 | public class ThreadSecurityDemo {
4 | private static int sum = 10;
5 |
6 | public static void main(String[] args){
7 |
8 | Runnable runnable1 = new Runnable() {
9 | public void run() {
10 | sum = sum + 1;
11 | }
12 | };
13 |
14 | Runnable runnable2 = new Runnable() {
15 | public void run() {
16 | sum = sum - 1;
17 | }
18 | };
19 |
20 | Thread thread2 = new Thread(runnable2);
21 | Thread thread1 = new Thread(runnable1);
22 | thread1.start();
23 | thread2.start();
24 | System.out.println(sum);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/databasejdbc/User.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.databasejdbc;
2 |
3 | public class User {
4 | private Integer id;
5 | private String username;
6 | private String password;
7 | private Boolean gender;
8 | public Integer getId() {
9 | return id;
10 | }
11 | public void setId(Integer id) {
12 | this.id = id;
13 | }
14 | public String getUsername() {
15 | return username;
16 | }
17 | public void setUsername(String username) {
18 | this.username = username;
19 | }
20 | public String getPassword() {
21 | return password;
22 | }
23 | public void setPassword(String password) {
24 | this.password = password;
25 | }
26 | public Boolean getGender() {
27 | return gender;
28 | }
29 | public void setGender(Boolean gender) {
30 | this.gender = gender;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/generics/ListFactory.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.generics;
2 |
3 | import java.util.ArrayList;
4 | import java.util.LinkedList;
5 | import java.util.List;
6 |
7 | public class ListFactory{
8 |
9 | private T t;
10 |
11 | public T getT() {
12 | return t;
13 | }
14 |
15 | public void setT(T t) {
16 | this.t = t;
17 | }
18 | /**
19 | * 限制泛型可用类型
20 | * 在定义泛型类别时,预设可以使用任何的类型来实例化泛型类型中的类型。
21 | * 但是如果想限制使用泛型类别时,只能用某个特定类型或者是其子类型才能实例化该类型时,可以在定义类型时,使用extends关键字指定这个类型必须是继承某个类,或者实现某个接口,也可以是这个类或接口本身。
22 | * 此处注意,虽然List是一个接口,但是关键字仍然是extends而不是implements。
23 | * @param args
24 | */
25 | public static void main(String[] args) {
26 | ListFactory listFactory = new ListFactory();
27 | ListFactory listFactory2 = new ListFactory();
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/databasedemo/DB.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.databasedemo;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.ResultSet;
6 | import java.sql.SQLException;
7 | import java.sql.Statement;
8 |
9 | public class DB {
10 |
11 | private Connection conn = null;
12 | public DB(String url,String username,String password){
13 | try {
14 | Class.forName("com.mysql.jdbc.Driver");
15 | conn = DriverManager.getConnection(url,username,password);
16 | System.out.println(conn);
17 | } catch (ClassNotFoundException e) {
18 | e.printStackTrace();
19 | } catch (SQLException e) {
20 | e.printStackTrace();
21 | }
22 | }
23 |
24 | public ResultSet runSql(String sql) throws SQLException{
25 | Statement statement= conn.createStatement();
26 | return statement.executeQuery(sql);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/lang3/TeacherBean.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.lang3;
2 |
3 | import java.io.Serializable;
4 |
5 | import org.apache.commons.lang3.builder.ToStringBuilder;
6 | import org.apache.commons.lang3.builder.ToStringStyle;
7 |
8 | public class TeacherBean implements Serializable{
9 |
10 | /**
11 | *
12 | */
13 | private static final long serialVersionUID = -7109407035176922569L;
14 | private String name;
15 |
16 | public TeacherBean() {
17 | super();
18 | }
19 |
20 | public TeacherBean(String name) {
21 | super();
22 | this.name = name;
23 | }
24 |
25 | public String getName() {
26 | return name;
27 | }
28 |
29 | public void setName(String name) {
30 | this.name = name;
31 | }
32 |
33 | @Override
34 | public String toString() {
35 | return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/TimerTest.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread;
2 |
3 | import java.util.Date;
4 | import java.util.Timer;
5 | import java.util.TimerTask;
6 | public class TimerTest {
7 | static class MyTimerTask1 extends TimerTask {
8 | public void run() {
9 | new Timer().schedule(new MyTimerTask2(), 2000);
10 | }
11 | }
12 | static class MyTimerTask2 extends TimerTask {
13 | public void run() {
14 | new Timer().schedule(new MyTimerTask1(), 3000);
15 | }
16 | }
17 | public static void main(String[] args) {
18 | Timer timer = new Timer();
19 | timer.schedule(new MyTimerTask2(), 2000);
20 | while(true) {
21 | System.out.println(new Date().getSeconds());
22 | try {
23 | Thread.sleep(1000);
24 | } catch (InterruptedException e) {
25 | // TODO Auto-generated catch block
26 | e.printStackTrace();
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/collectionsdemo/StackDemo.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.collectionsdemo;
2 |
3 | import java.util.Stack;
4 |
5 | import junit.framework.TestCase;
6 |
7 | public class StackDemo extends TestCase {
8 |
9 | public void push(Stack st, int a) {
10 | st.push(new Integer(a));
11 | }
12 |
13 | public Integer pop(Stack st) {
14 | if (!st.isEmpty()) {
15 | Integer a = st.pop();
16 | return a;
17 | }
18 | return null;
19 |
20 | }
21 |
22 | public void pushTest() {
23 | Stack st = new Stack();
24 | push(st, 1);
25 | push(st, 2);
26 | push(st, 3);
27 | push(st, 4);
28 | push(st, 5);
29 | System.out.println(st);
30 | pop(st);
31 | System.out.println(st);
32 | pop(st);
33 | pop(st);
34 | System.out.println(st);
35 | pop(st);
36 | pop(st);
37 | pop(st);
38 | System.out.println(st);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/stringdemo/StringTester.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.stringdemo;
2 |
3 | import junit.framework.TestCase;
4 |
5 | public class StringTester extends TestCase{
6 |
7 | public void subStringTest(){
8 | String channel = "00:12:4B:00:02:60:E5:3D_A0";
9 | String mac = channel.substring(0, channel.length()-3);
10 | System.out.println(mac);//00:12:4B:00:02:60:E5:3D
11 | }
12 |
13 | public void splitTest(){
14 | String channel = "00:12:4B:00:02:60:E5:3D_A0";
15 | String[] dat = channel.split("_");
16 | String mac = dat[0];// 00:12:4B:00:02:60:E5:3D
17 | String command = dat[1];//A0
18 | System.out.println("mac:"+mac+",command:"+command);
19 | }
20 |
21 | public String getChannel(){
22 | String command = "{A0=?}";
23 | String[] array = command.split("=");
24 | System.out.println(array[0].substring(1));
25 | return array[0].substring(0);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/VolatileTest.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread;
2 |
3 | public class VolatileTest {
4 | public static volatile int a = -1;
5 |
6 | public static void main(String[] args) {
7 | Thread thread1 = new Thread(){
8 |
9 | @Override
10 | public void run() {
11 | a = 10;
12 | try {
13 | Thread.sleep(10);
14 | } catch (InterruptedException e) {
15 | // TODO Auto-generated catch block
16 | e.printStackTrace();
17 | }
18 | a = a + 1000;
19 | }
20 | };
21 | thread1.start();
22 |
23 | Thread thread2 = new Thread(){
24 |
25 | @Override
26 | public void run() {
27 | a = 100;
28 | try {
29 | Thread.sleep(10);
30 | } catch (InterruptedException e) {
31 | // TODO Auto-generated catch block
32 | e.printStackTrace();
33 | }
34 | a = a + 1000;
35 | }
36 | };
37 | thread2.start();
38 |
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/trycatch/TryCatchTester.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.trycatch;
2 |
3 | import junit.framework.TestCase;
4 |
5 | public class TryCatchTester extends TestCase {
6 |
7 | public int add(int a,int b){
8 | System.out.println("执行add方法");
9 | throw new NullPointerException();
10 | // return a+b;
11 | }
12 | //除非调用system.exit()让程序退出或断电等因素致使程序中止,否则,无论任何因素,finally块都一定会执行!!
13 | public int add1(){
14 | try {
15 | //finally在return语句之后,跳转到上一级程序之前执行。
16 | System.out.println("执行test1的try方法");
17 | int result = add(1, 2);
18 | System.out.println("执行完add方法");
19 | // return result;
20 | } catch (Exception e) {
21 | System.out.println("执行catch方法");
22 | return -2;
23 | } finally {//finally一定会执行,在return之前。(准确说,应该是return ;语句)
24 | System.out.println("执行test1的finally方法");
25 | }
26 | return -1;
27 | }
28 |
29 | public void test1(){
30 | System.out.println(add1());
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/product/Product.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread.product;
2 |
3 | public class Product {
4 | int id;
5 | private String producedBy = "N/A";
6 | private String consumedBy = "N/A";
7 |
8 | Product(int id, String producedBy) {
9 | this.id = id;
10 | this.producedBy = producedBy;
11 | }
12 |
13 | public void consume(String consumedBy) {
14 | this.consumedBy = consumedBy;
15 | }
16 |
17 | public String toString() {
18 | return "Product : " + id + ", produced by " + producedBy
19 | + ", consumed by " + consumedBy;
20 | }
21 |
22 | public String getProducedBy() {
23 | return producedBy;
24 | }
25 |
26 | public void setProducedBy(String producedBy) {
27 | this.producedBy = producedBy;
28 | }
29 |
30 | public String getConsumedBy() {
31 | return consumedBy;
32 | }
33 |
34 | public void setConsumedBy(String consumedBy) {
35 | this.consumedBy = consumedBy;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/arraydemo/ArrayTester.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.arraydemo;
2 |
3 | import junit.framework.TestCase;
4 |
5 | /**
6 | *
7 | * 类ArrayTester.java的实现描述:java数组遍历
8 | * @author andyguo.gd 2016年8月6日 下午1:12:36
9 | */
10 | public class ArrayTester extends TestCase {
11 |
12 | public void test() {
13 | int[][] numbers = new int[][] { { 2, 3, 4, 5, 7, 8 }, { 20, 30, 40 }, { 200, 300 }, { 2000, 3000 } };
14 | //使用增强for循环
15 | for (int i[] : numbers) {
16 | for (int j : i) {
17 | System.out.print(j + " ");
18 | }
19 | System.out.println();
20 | }
21 | }
22 |
23 | public void test2() {
24 | int[][] numbers = new int[][] { { 2, 3, 4, 5, 7, 8 }, { 20, 30, 40 }, { 200, 300 }, { 2000, 3000 } };
25 | for (int i = 0; i < numbers.length; i++) {
26 | for (int j = 0; j < numbers[i].length; j++) {
27 | System.out.print(numbers[i][j] + " ");
28 | }
29 | System.out.println();
30 | }
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/databasedemo/DBHelper.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.databasedemo;
2 |
3 | import java.sql.ResultSet;
4 | import java.sql.SQLException;
5 |
6 | import com.andieguo.propertiesdemo.PropertiesHelper;
7 |
8 | public class DBHelper {
9 |
10 | public static DB getDB() {
11 | String url = PropertiesHelper.getProperty("url");
12 | String username = PropertiesHelper.getProperty("username");
13 | String password = PropertiesHelper.getProperty("password");
14 | String database = PropertiesHelper.getProperty("database");
15 | return new DB(url + database, username, password);
16 | }
17 |
18 | public static void main(String[] args) {
19 | DB db = getDB();
20 | try {
21 | ResultSet rs = db.runSql("select * from admin");
22 | while (rs.next()) {
23 | System.out.println(rs.getString("name"));
24 | System.out.println(rs.getString("password"));
25 | }
26 | } catch (SQLException e) {
27 | e.printStackTrace();
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/saxparserdemo/saxbooks.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Everyday Italian
5 | Giada De Laurentiis
6 | 2005
7 | 30.0
8 |
9 |
10 | Harry Potter
11 | J K. Rowling
12 | 2005
13 | 29.99
14 |
15 |
16 | XQuery Kick Start
17 | James McGovern
18 | Per Bothner
19 | Kurt Cagle
20 | James Linn
21 | Vaidyanathan Nagarajan
22 | 2003
23 | 49.99
24 |
25 |
26 | Learning XML
27 | Erik T. Ray
28 | 2003
29 | 39.95
30 |
31 |
32 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/xmldemo/createbooks.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Everyday Italian
5 | Giada De Laurentiis
6 | 2005
7 | 30.0
8 |
9 |
10 | Harry Potter
11 | J K. Rowling
12 | 2005
13 | 29.99
14 |
15 |
16 | XQuery Kick Start
17 | James McGovern
18 | Per Bothner
19 | Kurt Cagle
20 | James Linn
21 | Vaidyanathan Nagarajan
22 | 2003
23 | 49.99
24 |
25 |
26 | Learning XML
27 | Erik T. Ray
28 | 2003
29 | 39.95
30 |
31 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/enumdemo/EnumTester.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.enumdemo;
2 |
3 | import junit.framework.TestCase;
4 |
5 | public class EnumTester extends TestCase {
6 |
7 | public enum SensorKey {
8 |
9 | KEY_Information("Information", "key_Information"),
10 | KEY_Light("Light", "key_Light"),
11 | KEY_Appliance("Appliance", "key_Appliance");
12 |
13 | private String activity;
14 | private String key;
15 |
16 | private SensorKey(String activity, String key) {// ˽�еĻ��Ѻõ�
17 | this.key = key;
18 | this.activity = activity;
19 | }
20 |
21 | public String getKey() {
22 | return key;
23 | }
24 |
25 | public String getActivity() {
26 | return activity;
27 | }
28 | }
29 |
30 | public void test() {
31 | System.out.println(SensorKey.KEY_Appliance.getKey());
32 | }
33 |
34 | public void test2() {
35 | for (SensorKey sensorKey : SensorKey.values()) {// ����ö��
36 | System.out.println(sensorKey.getKey() + ":" + sensorKey.getActivity());
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/xmldemo/books.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Everyday Italian
6 | Giada De Laurentiis
7 | 2005
8 | 30.00
9 |
10 |
11 | Harry Potter
12 | J K. Rowling
13 | 2005
14 | 29.99
15 |
16 |
17 | XQuery Kick Start
18 | James McGovern
19 | Per Bothner
20 | Kurt Cagle
21 | James Linn
22 | Vaidyanathan Nagarajan
23 | 2003
24 | 49.99
25 |
26 |
27 | Learning XML
28 | Erik T. Ray
29 | 2003
30 | 39.95
31 |
32 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/saxparserdemo/books.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Everyday Italian
6 | Giada De Laurentiis
7 | 2005
8 | 30.00
9 |
10 |
11 | Harry Potter
12 | J K. Rowling
13 | 2005
14 | 29.99
15 |
16 |
17 | XQuery Kick Start
18 | James McGovern
19 | Per Bothner
20 | Kurt Cagle
21 | James Linn
22 | Vaidyanathan Nagarajan
23 | 2003
24 | 49.99
25 |
26 |
27 | Learning XML
28 | Erik T. Ray
29 | 2003
30 | 39.95
31 |
32 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/generics/Animal.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.generics;
2 |
3 | public class Animal {
4 |
5 | private String name;
6 |
7 | private Integer size;
8 |
9 | private Integer weight;
10 |
11 | public Animal(String name){
12 | this.name = name;
13 | }
14 |
15 | public Animal(Integer size, Integer weight) {
16 | super();
17 | this.size = size;
18 | this.weight = weight;
19 | }
20 |
21 | public String getName() {
22 | return name;
23 | }
24 |
25 | public void setName(String name) {
26 | this.name = name;
27 | }
28 |
29 | public Integer getSize() {
30 | return size;
31 | }
32 |
33 | public void setSize(Integer size) {
34 | this.size = size;
35 | }
36 |
37 | public Integer getWeight() {
38 | return weight;
39 | }
40 |
41 | public void setWeight(Integer weight) {
42 | this.weight = weight;
43 | }
44 |
45 | public void eat(){
46 | System.out.println(this.getName() + " cat eat.");
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "Animal [name=" + name + ", size=" + size + ", weight=" + weight + "]";
52 | }
53 |
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/lang3/SchoolBean.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.lang3;
2 |
3 | import java.io.Serializable;
4 |
5 | import org.apache.commons.lang3.builder.ToStringBuilder;
6 | import org.apache.commons.lang3.builder.ToStringStyle;
7 |
8 | public class SchoolBean implements Serializable{
9 |
10 | /**
11 | *
12 | */
13 | private static final long serialVersionUID = -2618428079233870928L;
14 |
15 | private String name;
16 |
17 | private TeacherBean teacher;
18 |
19 | public String getName() {
20 | return name;
21 | }
22 |
23 | public void setName(String name) {
24 | this.name = name;
25 | }
26 |
27 | public TeacherBean getTeacher() {
28 | return teacher;
29 | }
30 |
31 | public void setTeacher(TeacherBean teacher) {
32 | this.teacher = teacher;
33 | }
34 |
35 | public SchoolBean(String name, TeacherBean teacher) {
36 | super();
37 | this.name = name;
38 | this.teacher = teacher;
39 | }
40 |
41 | public SchoolBean() {
42 | super();
43 | }
44 |
45 | @Override
46 | public String toString() {
47 | return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
48 | }
49 |
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/RunTimeDemo.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 |
7 | public class RunTimeDemo {
8 |
9 | public static void main(String[] args){
10 | //runCommand();
11 | runCommand2();
12 | }
13 | private static void runCommand2() {
14 | try {
15 | String [] cmd={"cmd","/C","start F:\\hello.txt"};
16 | Process proc =Runtime.getRuntime().exec(cmd);
17 | } catch (IOException e) {
18 | e.printStackTrace();
19 | }
20 | }
21 | private static void runCommand() {
22 | String command = "ipconfig";
23 | Runtime run = Runtime.getRuntime();
24 | try {
25 | Process p = run.exec(command);
26 | BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
27 | String strline;
28 | while((strline = in.readLine())!=null){
29 | System.out.println(strline);
30 | }
31 | if (p.waitFor() != 0) {
32 | if (p.exitValue() == 1)
33 | System.err.println("hello");
34 | }
35 | in.close();
36 | } catch (Exception e) {
37 | e.printStackTrace();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/collectionsdemo/IteratorTester.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.collectionsdemo;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.Iterator;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | public class IteratorTester {
10 | public static void main(String args[]) {
11 | List myList = new ArrayList();
12 |
13 | myList.add("1");
14 | myList.add("2");
15 | myList.add("3");
16 | myList.add("4");
17 | myList.add("5");
18 |
19 | Iterator it = myList.iterator();
20 | while (it.hasNext()) {
21 | String value = it.next();
22 | System.out.println("List Value:" + value);
23 | if (value.equals("3"));
24 | //myList.remove(value);
25 | }
26 |
27 | Map myMap = new HashMap();
28 | myMap.put("1", "1");
29 | myMap.put("2", "2");
30 | myMap.put("3", "3");
31 |
32 | Iterator it1 = myMap.keySet().iterator();
33 | while (it1.hasNext()) {
34 | String key = it1.next();
35 | System.out.println("Map Value:" + myMap.get(key));
36 | if (key.equals("2")) {
37 | myMap.put("1", "4");
38 | // myMap.put("4", "4");
39 | }
40 | }
41 |
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/jsondemo/GsonDemo.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.jsondemo;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.FileReader;
5 | import java.io.FileWriter;
6 | import java.io.IOException;
7 |
8 | import com.google.gson.Gson;
9 |
10 | public class GsonDemo {
11 | public static void main(String[] args) {
12 | jsonToObject();
13 | }
14 |
15 | @SuppressWarnings("unused")
16 | private static void objectToJson() {
17 | Book book = new Book("story","en"," The ordinary world","andy",2013,45.0);
18 | Gson gson = new Gson();
19 |
20 | String json = gson.toJson(book);
21 | try {
22 | FileWriter writer = new FileWriter("src/com/andieguo/jsondemo/book.json");
23 | writer.write(json);
24 | writer.close();
25 | } catch (IOException e) {
26 | e.printStackTrace();
27 | }
28 | System.out.println(json);
29 | }
30 |
31 | private static void jsonToObject() {
32 | Gson gson = new Gson();
33 | try {
34 | BufferedReader br = new BufferedReader(new FileReader("src/com/andieguo/jsondemo/book.json"));
35 | Book book = gson.fromJson(br, Book.class);
36 | System.out.println(book.toString());
37 | } catch (IOException e) {
38 | e.printStackTrace();
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/product/ProductStack.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread.product;
2 | public class ProductStack {
3 | int index = 0;
4 |
5 | Product[] arrProduct = new Product[6];
6 |
7 | public synchronized void push(Product product) {
8 | while (index == arrProduct.length)
9 | {
10 | try {
11 |
12 | System.out.println(product.getProducedBy() + " is waiting.");
13 | wait();
14 | } catch (InterruptedException e) {
15 | e.printStackTrace();
16 | }
17 | }
18 | System.out.println(product.getProducedBy() + " sent a notifyAll().");
19 |
20 | notifyAll();
21 | arrProduct[index] = product;
22 | index++;
23 | System.out.println(product.getProducedBy() + " ������: " + product);
24 | }
25 |
26 | public synchronized Product pop(String consumerName) {
27 | while (index == 0) {
28 | try {
29 | System.out.println(consumerName + " is waiting.");
30 | wait();
31 | } catch (InterruptedException e) {
32 | e.printStackTrace();
33 | }
34 | }
35 |
36 | System.out.println(consumerName + " sent a notifyAll().");
37 | notifyAll();
38 | index--;
39 | Product product = arrProduct[index];
40 | product.consume(consumerName);
41 | System.out.println(product.getConsumedBy() + " : " + product);
42 | return product;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/xmldemo/Book.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.xmldemo;
2 |
3 | public class Book {
4 | private String category;
5 | private String titleLang;
6 | private String title;
7 | private String author;
8 | private Integer year;
9 | private Double price;
10 |
11 |
12 | @Override
13 | public String toString() {
14 | return "Book [category=" + category + ", titleLang=" + titleLang + ", title=" + title + ", author=" + author + ", year=" + year + ", price=" + price + "]";
15 | }
16 | public String getCategory() {
17 | return category;
18 | }
19 | public void setCategory(String category) {
20 | this.category = category;
21 | }
22 | public String getTitleLang() {
23 | return titleLang;
24 | }
25 | public void setTitleLang(String titleLang) {
26 | this.titleLang = titleLang;
27 | }
28 | public String getTitle() {
29 | return title;
30 | }
31 | public void setTitle(String title) {
32 | this.title = title;
33 | }
34 | public String getAuthor() {
35 | return author;
36 | }
37 | public void setAuthor(String author) {
38 | this.author = author;
39 | }
40 | public Integer getYear() {
41 | return year;
42 | }
43 | public void setYear(Integer year) {
44 | this.year = year;
45 | }
46 | public Double getPrice() {
47 | return price;
48 | }
49 | public void setPrice(Double price) {
50 | this.price = price;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/saxparserdemo/Book.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.saxparserdemo;
2 |
3 | public class Book {
4 | private String category;
5 | private String titleLang;
6 | private String title;
7 | private String author;
8 | private Integer year;
9 | private Double price;
10 |
11 |
12 | @Override
13 | public String toString() {
14 | return "Book [category=" + category + ", titleLang=" + titleLang + ", title=" + title + ", author=" + author + ", year=" + year + ", price=" + price + "]";
15 | }
16 | public String getCategory() {
17 | return category;
18 | }
19 | public void setCategory(String category) {
20 | this.category = category;
21 | }
22 | public String getTitleLang() {
23 | return titleLang;
24 | }
25 | public void setTitleLang(String titleLang) {
26 | this.titleLang = titleLang;
27 | }
28 | public String getTitle() {
29 | return title;
30 | }
31 | public void setTitle(String title) {
32 | this.title = title;
33 | }
34 | public String getAuthor() {
35 | return author;
36 | }
37 | public void setAuthor(String author) {
38 | this.author = author;
39 | }
40 | public Integer getYear() {
41 | return year;
42 | }
43 | public void setYear(Integer year) {
44 | this.year = year;
45 | }
46 | public Double getPrice() {
47 | return price;
48 | }
49 | public void setPrice(Double price) {
50 | this.price = price;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/propertiesutil/PropertyParseExceptionTest.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.propertiesutil;
2 |
3 |
4 | import junit.framework.TestCase;
5 |
6 |
7 | /**
8 | * Tests the property exception class.
9 | */
10 | public class PropertyParseExceptionTest extends TestCase {
11 |
12 |
13 | public void testMinimalConstructor() {
14 |
15 | PropertyParseException e = new PropertyParseException("message");
16 | assertEquals("message", e.getMessage());
17 | assertNull(e.getPropertyKey());
18 | assertNull(e.getPropertyValue());
19 | assertNull(e.getCause());
20 | }
21 |
22 |
23 | public void testPropertyKeyConstructor() {
24 |
25 | PropertyParseException e = new PropertyParseException("message", "key");
26 | assertEquals("message", e.getMessage());
27 | assertEquals("key", e.getPropertyKey());
28 | assertNull(e.getPropertyValue());
29 | assertNull(e.getCause());
30 | }
31 |
32 |
33 | public void testFullConstructor() {
34 |
35 | PropertyParseException e = new PropertyParseException("message", "key", "value");
36 | assertEquals("message", e.getMessage());
37 | assertEquals("key", e.getPropertyKey());
38 | assertEquals("value", e.getPropertyValue());
39 | assertNull(e.getCause());
40 | }
41 |
42 |
43 |
44 | public void testFullConstructorWithNulls() {
45 |
46 | PropertyParseException e = new PropertyParseException(null, null, null);
47 | assertNull(e.getMessage());
48 | assertNull(e.getPropertyKey());
49 | assertNull(e.getPropertyValue());
50 | assertNull(e.getCause());
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/jsondemo/Book.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.jsondemo;
2 |
3 | public class Book {
4 | private String category;
5 | private String titleLang;
6 | private String title;
7 | private String author;
8 | private Integer year;
9 | private Double price;
10 |
11 |
12 | public Book(String category, String titleLang, String title, String author, Integer year, Double price) {
13 | super();
14 | this.category = category;
15 | this.titleLang = titleLang;
16 | this.title = title;
17 | this.author = author;
18 | this.year = year;
19 | this.price = price;
20 | }
21 | @Override
22 | public String toString() {
23 | return "Book [category=" + category + ", titleLang=" + titleLang + ", title=" + title + ", author=" + author + ", year=" + year + ", price=" + price + "]";
24 | }
25 | public String getCategory() {
26 | return category;
27 | }
28 | public void setCategory(String category) {
29 | this.category = category;
30 | }
31 | public String getTitleLang() {
32 | return titleLang;
33 | }
34 | public void setTitleLang(String titleLang) {
35 | this.titleLang = titleLang;
36 | }
37 | public String getTitle() {
38 | return title;
39 | }
40 | public void setTitle(String title) {
41 | this.title = title;
42 | }
43 | public String getAuthor() {
44 | return author;
45 | }
46 | public void setAuthor(String author) {
47 | this.author = author;
48 | }
49 | public Integer getYear() {
50 | return year;
51 | }
52 | public void setYear(Integer year) {
53 | this.year = year;
54 | }
55 | public Double getPrice() {
56 | return price;
57 | }
58 | public void setPrice(Double price) {
59 | this.price = price;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/xmldemo/LoopXMLFile.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.xmldemo;
2 |
3 | import java.io.File;
4 |
5 | import javax.xml.parsers.DocumentBuilder;
6 | import javax.xml.parsers.DocumentBuilderFactory;
7 |
8 | import org.w3c.dom.Document;
9 | import org.w3c.dom.NamedNodeMap;
10 | import org.w3c.dom.Node;
11 | import org.w3c.dom.NodeList;
12 |
13 | public class LoopXMLFile {
14 | public static void main(String[] args) {
15 | loopXMLFile();
16 | }
17 |
18 | private static void loopXMLFile(){
19 | try {
20 | File file = new File("src/com/andieguo/xmldemo/books.xml");
21 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
22 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
23 | Document doc = dBuilder.parse(file);
24 | if(doc.hasChildNodes()){
25 | printNote(doc.getChildNodes());
26 | }
27 | } catch (Exception e) {
28 | e.printStackTrace();
29 | }
30 | }
31 |
32 | private static void printNote(NodeList nodeList){
33 | for(int i=0; i");
37 | System.out.println(node.getTextContent());
38 | if(node.hasAttributes()){
39 | NamedNodeMap nodeMap = node.getAttributes();
40 | for(int j=0;j");
49 | }
50 |
51 |
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/saxparserdemo/XMLParserSAX.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.saxparserdemo;
2 |
3 | import java.io.File;
4 | import java.io.FileInputStream;
5 | import java.util.List;
6 |
7 | import javax.xml.parsers.SAXParser;
8 | import javax.xml.parsers.SAXParserFactory;
9 |
10 | import org.xml.sax.InputSource;
11 | import org.xml.sax.XMLReader;
12 |
13 | public class XMLParserSAX {
14 |
15 | public static void main(String[] args) {
16 | //List books = xmlReader(new File("src/com/andieguo/saxparserdemo/books.xml"));
17 | List books = saxParser(new File("src/com/andieguo/saxparserdemo/books.xml"));
18 | for (Book book : books) {
19 | System.out.println(book.toString());
20 | }
21 | }
22 | public static List saxParser(File file) {
23 | try {
24 | SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
25 | SAXParser saxParser = saxParserFactory.newSAXParser();
26 | SaxHandler saxHandler = new SaxHandler();
27 | saxParser.parse(file, saxHandler);
28 | List books = saxHandler.getBookList();
29 | return books;
30 | } catch (Exception e) {
31 | e.printStackTrace();
32 | }
33 | return null;
34 | }
35 | public static List xmlReader(File file) {
36 | try {
37 | SAXParserFactory factory = SAXParserFactory.newInstance();
38 | SAXParser parser = factory.newSAXParser();
39 | XMLReader reader = parser.getXMLReader();
40 | SaxHandler saxHandler = new SaxHandler();
41 | reader.setContentHandler(saxHandler);
42 | reader.parse(new InputSource(new FileInputStream(file)));
43 | List books = saxHandler.getBookList();
44 | return books;
45 | } catch (Exception e) {
46 | e.printStackTrace();
47 | }
48 | return null;
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/propertiesdemo/PropertiesHelperTest.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.propertiesdemo;
2 |
3 | import java.io.FileNotFoundException;
4 | import java.io.FileOutputStream;
5 | import java.io.OutputStream;
6 | import java.util.Date;
7 |
8 | import junit.framework.TestCase;
9 |
10 | public class PropertiesHelperTest extends TestCase {
11 |
12 | public void getProperty() {
13 | System.out.println(PropertiesHelper.getProperty("KEY"));
14 | }
15 |
16 | public void store() {
17 | PropertiesHelper.setProperty("database", "bbs");
18 | try {
19 | OutputStream out = new FileOutputStream("config.properties");
20 | PropertiesHelper.store(out, "modify" + new Date());
21 | } catch (FileNotFoundException e) {
22 | e.printStackTrace();
23 | }
24 | }
25 |
26 | public void storeXML(){
27 | try {
28 | OutputStream out = new FileOutputStream("config.xml");
29 | PropertiesHelper.storeXML(out, "modify" + new Date());
30 | } catch (FileNotFoundException e) {
31 | e.printStackTrace();
32 | }
33 | }
34 |
35 | public void setProperties(){
36 | PropertiesHelper.setProperty("Weight", "70");
37 | PropertiesHelper.printEntry();
38 | }
39 |
40 | public void put(){
41 | PropertiesHelper.put("Height", 20);
42 | PropertiesHelper.put("Scannable", true);
43 | PropertiesHelper.put("Weight", 70.0);
44 | PropertiesHelper.put("Weight", 80.0);
45 | PropertiesHelper.printEntry();
46 | }
47 |
48 | public void list(){
49 | PropertiesHelper.list(System.out);
50 | }
51 |
52 | public void printEntry() {
53 | PropertiesHelper.printEntry();
54 | }
55 |
56 | public void printKey(){
57 | PropertiesHelper.printKey();
58 | }
59 |
60 | public void printEnumeration(){
61 | PropertiesHelper.printEnumeration();
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/reflectiondemo/DumpMethods.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.reflectiondemo;
2 |
3 | import java.lang.reflect.Method;
4 | import java.util.Stack;
5 |
6 | import junit.framework.TestCase;
7 |
8 | public class DumpMethods extends TestCase {
9 | public static void main(String[] args) {
10 | getDeclaredMethods("java.util.Stack");
11 | }
12 |
13 | private static void getDeclaredMethods(String className) {
14 | try {
15 | Class> c = Class.forName(className);
16 | Method m[] = c.getDeclaredMethods();
17 | for (int i = 0; i < m.length; i++) {
18 | System.out.println(m[i].toString());
19 | }
20 | } catch (ClassNotFoundException e) {
21 | e.printStackTrace();
22 | }
23 | }
24 |
25 | public void getClassTester() {
26 | try {
27 | Class> stringClass = Class.forName("java.lang.String");
28 | Class> intClass = int.class;
29 | Class> integerClass = Integer.class;
30 |
31 | System.out.println(integerClass.getDeclaredMethods()[0].toString());
32 | System.out.println(Stack.class.getDeclaredMethods()[0].toString());
33 | } catch (ClassNotFoundException e) {
34 | e.printStackTrace();
35 | }
36 | }
37 |
38 | public void isInstanceTest() {
39 | Class> c = DumpMethods.class;
40 | boolean b = c.isInstance(new DumpMethods());
41 | System.out.println(b);
42 | }
43 |
44 | public void invokeTest(){
45 | Class> c = Person.class;
46 | try{
47 | Method setNameMethod = c.getMethod("setName", String.class);
48 | Method getNameMethod = c.getMethod("getName", null);
49 | Person p = (Person) c.newInstance();
50 | setNameMethod.invoke(p, "andy");
51 | String name = (String) getNameMethod.invoke(p, null);
52 | System.out.println(name);
53 | }catch(Exception e){
54 | System.out.println(e.toString());
55 | }
56 |
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/thread/ThreadTest2.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.thread;
2 |
3 | public class ThreadTest2 {
4 | public static void main(String[] args) {
5 | final Business business = new Business();
6 | new Thread(new Runnable() {
7 | public void run() {
8 | threadExecute(business, "sub");
9 | }
10 | }).start();
11 | threadExecute(business, "main");
12 |
13 | }
14 | public static void threadExecute(Business business, String threadType) {
15 | for(int i = 0; i < 10; i++) {
16 | try {
17 | if("main".equals(threadType)) {
18 | business.main(i);
19 | } else {
20 | business.sub(i);
21 | }
22 | } catch (InterruptedException e) {
23 | e.printStackTrace();
24 | }
25 | }
26 | }
27 | }
28 | /**
29 | sub thread seq of 0, loop of 0
30 | sub thread seq of 1, loop of 0
31 | main thread seq of 0, loop of 0
32 | main thread seq of 1, loop of 0
33 | main thread seq of 2, loop of 0
34 | main thread seq of 3, loop of 0
35 |
36 | sub thread seq of 0, loop of 1
37 | sub thread seq of 1, loop of 1
38 | main thread seq of 0, loop of 1
39 | main thread seq of 1, loop of 1
40 | main thread seq of 2, loop of 1
41 | main thread seq of 3, loop of 1
42 | * @author Administrator
43 | *
44 | */
45 | class Business {
46 | private boolean bool = true;
47 | public synchronized void main(int loop) throws InterruptedException {
48 | while(bool) {
49 | this.wait();
50 | }
51 | for(int i = 0; i < 4; i++) {
52 | System.out.println("main thread seq of " + i + ", loop of " + loop);
53 | }
54 | bool = true;
55 | this.notify();
56 | }
57 | public synchronized void sub(int loop) throws InterruptedException {
58 | while(!bool) {
59 | this.wait();
60 | }
61 | for(int i = 0; i < 2; i++) {
62 | System.out.println("sub thread seq of " + i + ", loop of " + loop);
63 | }
64 | bool = false;
65 | this.notify();
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.andieguo.demo
6 | JavaDemo
7 | 0.0.1-SNAPSHOT
8 | jar
9 |
10 | JavaDemo
11 | http://maven.apache.org
12 |
13 |
14 | UTF-8
15 |
16 |
17 |
18 |
19 | junit
20 | junit
21 | 3.8.1
22 | test
23 |
24 |
25 |
26 | com.google.code.gson
27 | gson
28 | 2.2.4
29 |
30 |
31 | org.apache.commons
32 | commons-lang3
33 | 3.4
34 |
35 |
36 |
37 | org.apache.commons
38 | commons-collections4
39 | 4.1
40 |
41 |
42 |
43 | commons-beanutils
44 | commons-beanutils
45 | 1.8.3
46 |
47 |
48 |
49 | org.springframework
50 | spring-beans
51 | 4.3.3.RELEASE
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/collectionsdemo/VectorTester.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.collectionsdemo;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Enumeration;
5 | import java.util.Iterator;
6 | import java.util.List;
7 | import java.util.Vector;
8 |
9 | public class VectorTester {
10 |
11 |
12 | public static void main(String args[]) {
13 |
14 | vectortest();
15 | }
16 | @SuppressWarnings({ "unchecked", "rawtypes" })
17 | private static void vectortest() {
18 | // initial size is 3, increment is 2
19 | Vector v = new Vector(3, 2);
20 | System.out.println("Initial size: " + v.size());
21 | System.out.println("Initial capacity: " + v.capacity());
22 | v.addElement(new Integer(1));
23 | v.addElement(new Integer(2));
24 | v.addElement(new Integer(3));
25 | v.addElement(new Integer(4));
26 | System.out.println("Capacity after four additions: " + v.capacity());
27 |
28 | v.addElement(new Double(5.45));
29 | System.out.println("Current capacity: " + v.capacity());
30 | v.addElement(new Double(6.08));
31 | v.addElement(new Integer(7));
32 | System.out.println("Current capacity: " + v.capacity());
33 | v.addElement(new Float(9.4));
34 | v.addElement(new Integer(10));
35 | System.out.println("Current capacity: " + v.capacity());
36 | v.addElement(new Integer(11));
37 | v.addElement(new Integer(12));
38 | System.out.println("First element: " + (Integer) v.firstElement());
39 | System.out.println("Last element: " + (Integer) v.lastElement());
40 | if (v.contains(new Integer(3)))
41 | System.out.println("Vector contains 3.");
42 | // enumerate the elements in the vector.
43 | Enumeration vEnum = v.elements();
44 | System.out.println("\nElements in vector:");
45 | while (vEnum.hasMoreElements())
46 | System.out.print(vEnum.nextElement() + " ");
47 | System.out.println();
48 | }
49 |
50 | @SuppressWarnings({ "unchecked", "rawtypes" })
51 | public void listTest(){
52 | List list = new ArrayList();
53 | list.add(new Integer(11));
54 | list.add(new Double(12.0));
55 | Iterator it = list.iterator();
56 | while(it.hasNext()){
57 | System.out.println(it.next());
58 | }
59 | }
60 |
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/xmldemo/ReadXMLFile.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.xmldemo;
2 |
3 | import java.io.File;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | import javax.xml.parsers.DocumentBuilder;
8 | import javax.xml.parsers.DocumentBuilderFactory;
9 |
10 | import org.w3c.dom.Document;
11 | import org.w3c.dom.Element;
12 | import org.w3c.dom.Node;
13 | import org.w3c.dom.NodeList;
14 |
15 | public class ReadXMLFile {
16 |
17 | public static void main(String[] args) {
18 | File file = new File("src/com/andieguo/xmldemo/books.xml");
19 | List books = readXMLFile(file);
20 | for (Book book : books) {
21 | System.out.println(book.toString());
22 | }
23 | }
24 |
25 | public static List readXMLFile(File file) {
26 | List lists = new ArrayList();
27 | try {
28 |
29 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
30 | DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
31 | Document doc = dBuilder.parse(file);
32 | NodeList bookList = doc.getElementsByTagName("book");
33 | for (int i = 0; i < bookList.getLength(); i++) {
34 | Node bookNode = bookList.item(i);
35 | if (bookNode.getNodeType() == Node.ELEMENT_NODE) {
36 | Element bookElement = (Element) bookNode;
37 | Book book = new Book();
38 | book.setCategory(bookElement.getAttribute("category"));
39 | Element titleElement = (Element) bookElement.getElementsByTagName("title").item(0);
40 | book.setTitle(titleElement.getTextContent());
41 | book.setTitleLang(titleElement.getAttribute("lang"));
42 | NodeList authorList = bookElement.getElementsByTagName("author");
43 | String author = "";
44 | for (int j = 0; j < authorList.getLength(); j++) {
45 | author = author + authorList.item(j).getTextContent() + "/";
46 | }
47 | author = author.substring(0, author.length() - 1);
48 | book.setAuthor(author);
49 | book.setYear(Integer.valueOf(bookElement.getElementsByTagName("year").item(0).getTextContent()));
50 | book.setPrice(Double.valueOf(bookElement.getElementsByTagName("price").item(0).getTextContent()));
51 | lists.add(book);
52 | }
53 |
54 | }
55 | } catch (Exception e) {
56 | e.printStackTrace();
57 | }
58 | return lists;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/saxparserdemo/SaxHandler.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.saxparserdemo;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.xml.sax.Attributes;
7 | import org.xml.sax.SAXException;
8 | import org.xml.sax.helpers.DefaultHandler;
9 |
10 | public class SaxHandler extends DefaultHandler {
11 |
12 | private List bookList = null;
13 | private Book book = null;
14 | private boolean bTitle = false;
15 | private boolean bAuthor = false;
16 | private boolean bYear = false;
17 | private boolean bPrice = false;
18 |
19 | public List getBookList() {
20 | return bookList;
21 | }
22 |
23 | @Override
24 | public void startDocument() throws SAXException {
25 | super.startDocument();
26 | }
27 |
28 | @Override
29 | public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
30 | if (qName.equalsIgnoreCase("book")) {
31 | String category = attributes.getValue("category");
32 | book = new Book();
33 | book.setCategory(category);
34 | if (bookList == null) {
35 | bookList = new ArrayList();
36 | }
37 | } else if (qName.equalsIgnoreCase("title")) {
38 | String titleLang = attributes.getValue("lang");
39 | book.setTitleLang(titleLang);
40 | bTitle = true;
41 | } else if (qName.equalsIgnoreCase("author")) {
42 | bAuthor = true;
43 | } else if (qName.equalsIgnoreCase("year")) {
44 | bYear = true;
45 | } else if (qName.equalsIgnoreCase("price")) {
46 | bPrice = true;
47 | }
48 | }
49 |
50 | @Override
51 | public void endElement(String uri, String localName, String qName) throws SAXException {
52 | if (qName.equalsIgnoreCase("book")) {
53 | bookList.add(book);
54 | }
55 | }
56 |
57 | @Override
58 | public void characters(char[] ch, int start, int length) throws SAXException {
59 | if (bTitle) {
60 | book.setTitle(new String(ch, start, length));
61 | bTitle = false;
62 | } else if (bAuthor) {
63 | if (book.getAuthor() == null) {
64 | book.setAuthor(new String(ch, start, length));
65 | } else {
66 | book.setAuthor(book.getAuthor() + "/" + new String(ch, start, length));
67 | }
68 | bAuthor = false;
69 | } else if (bYear) {
70 | book.setYear(Integer.parseInt(new String(ch, start, length)));
71 | bYear = false;
72 | } else if (bPrice) {
73 | book.setPrice(Double.parseDouble(new String(ch, start, length)));
74 | bPrice = false;
75 | }
76 | }
77 |
78 | @Override
79 | public void endDocument() throws SAXException {
80 | super.endDocument();
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/generics/AnimalTest.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.generics;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 | import java.util.HashSet;
6 | import java.util.Iterator;
7 | import java.util.List;
8 |
9 | import junit.framework.TestCase;
10 |
11 | public class AnimalTest extends TestCase {
12 |
13 | private List extends Animal> animalList = new ArrayList();
14 |
15 | //Cat是Animal的子类
16 | private List extends Animal> catList = new ArrayList();
17 |
18 | //Bird是Magpie的父类
19 | private List super Magpie> birdList = new ArrayList();
20 |
21 | //Animal是Magpie的父类
22 | private List super Magpie> magpieList = new ArrayList();
23 |
24 | private List super Bird> birddList = new ArrayList();
25 |
26 | //compare比较器
27 | public void testAct4(){
28 | birddList.add(new Magpie("mage"));
29 | birddList.add(new Bird("cat"));
30 | Arrays.asList("a", "b", "c");
31 | HashSet s2 = new HashSet(Arrays.asList("a", "b", "c"));
32 | }
33 |
34 | public void testAct3(){
35 | List list = new ArrayList();
36 | list.add(new Cat("cat1"));
37 | list.add(new Cat("bird1"));
38 | list.add(new Cat("magpie1"));
39 | List super Magpie> magpieList = list;
40 | Iterator super Magpie> iterator = magpieList.iterator();
41 | while(iterator.hasNext()){
42 | Cat cat = (Cat)iterator.next();
43 | cat.jump();
44 | }
45 | }
46 |
47 |
48 | public void act(List list) {
49 | for (Animal animal : list) {
50 | animal.eat();
51 | }
52 | }
53 |
54 | /**
55 | * 类型通配声明
56 | * 使用>或是 extends SomeClass>的声明方式,意味着您只能通过该名称来取得所参考的实例的信息,或者是移除某些信息,但不能增加或者改写它的信息。
57 | * @param list
58 | */
59 | public void act1(List extends Animal> list){
60 | list.add(null);
61 | //list.add(new Cat("cat1"));
62 | for (Animal animal : list) {
63 | animal.eat();
64 | }
65 | }
66 |
67 | public void testAct(){
68 | List list = new ArrayList();
69 | list.add(new Cat("cat1"));
70 | list.add(new Bird("bird1"));
71 | list.add(new Magpie("magpie1"));
72 | act(list);
73 | List catList = new ArrayList();
74 | catList.add(new Cat("cat1"));
75 | catList.add(new Cat("cat2"));
76 | catList.add(new Cat("cat3"));
77 | act(list);
78 | }
79 |
80 | public void testAct1(){
81 | List list = new ArrayList();
82 | list.add(new Cat("cat1"));
83 | list.add(new Bird("bird1"));
84 | list.add(new Magpie("magpie1"));
85 | act1(list);
86 | List catList = new ArrayList();
87 | catList.add(new Cat("cat1"));
88 | catList.add(new Cat("cat2"));
89 | catList.add(new Cat("cat3"));
90 | act1(catList);
91 | }
92 |
93 |
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/src/main/java/com/andieguo/xmldemo/CreateXMLFile.java:
--------------------------------------------------------------------------------
1 | package com.andieguo.xmldemo;
2 |
3 | import java.io.File;
4 | import java.util.List;
5 |
6 | import javax.xml.parsers.DocumentBuilder;
7 | import javax.xml.parsers.DocumentBuilderFactory;
8 | import javax.xml.transform.OutputKeys;
9 | import javax.xml.transform.Transformer;
10 | import javax.xml.transform.TransformerFactory;
11 | import javax.xml.transform.dom.DOMSource;
12 | import javax.xml.transform.stream.StreamResult;
13 |
14 | import org.w3c.dom.Document;
15 | import org.w3c.dom.Element;
16 |
17 | public class CreateXMLFile {
18 |
19 | public static void main(String[] args) {
20 | File file = new File("src/com/andieguo/xmldemo/books.xml");
21 | List books = ReadXMLFile.readXMLFile(file);
22 | createXMLFile(books);
23 | }
24 |
25 | public static void createXMLFile(List books) {
26 | Document doc;
27 | Element bookstore;
28 | Element book;
29 | Element title;
30 | Element author;
31 | Element year;
32 | Element price;
33 | try {
34 | DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
35 | DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
36 | doc = dbBuilder.newDocument();
37 | if (doc != null) {
38 | bookstore = doc.createElement("bookstore");
39 | for (int i = 0; i < books.size(); i++) {
40 | book = doc.createElement("book");
41 | book.setAttribute("category", books.get(i).getCategory());
42 | title = doc.createElement("title");
43 | title.appendChild(doc.createTextNode(books.get(i).getTitle()));
44 | title.setAttribute("lang", books.get(i).getTitleLang());
45 | book.appendChild(title);
46 | String[] strAuthor = books.get(i).getAuthor().split("/");
47 | for(int j=0;j> entrySet() {
62 | return prop.entrySet();
63 | }
64 |
65 | public static Set