java 基础点滴(1)

java 基础点滴(一)
1. --jdom 读取XML数据

package test.xy.jdom;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
public class MyJDom {
    public static void main(String[] args) throws Exception{
        SAXBuilder sb=new SAXBuilder();//建立构造器
        Document doc=sb.build(new FileInputStream("src/xml/test.xml"));//读入指定文件
        Element root=doc.getRootElement();//获得根节点
        List list=root.getChildren();//将根节点下的所有子节点放入List中
        for(int i=0;i<list.size();i++) {
            System.out.println("---------------------------");
            Element item=(Element)list.get(i);//取得节点实例
            String id=item.getAttribute("id").getValue();//取得属性值
            System.out.println("id-->"+id);

            Element sub=item.getChild("title");//取得当前节点的字节点
            String text=sub.getText();//取得当前节点的值
             System.out.println("Title-->"+text);
            if(item.getChild("content").getChildren().size()>0){
            Element sub2=item.getChild("content").getChild("name");
            Element age=item.getChild("content").getChild("age");
                String text2=sub2.getText();
                String ageText=age.getText();
                System.out.println("the age is:"+ageText);
                System.out.println("name-->"+text2);
            }
           

            Element sub3=item.getChild("email");
            String text3=sub3.getText();
            System.out.println("Email-->"+text3);
        }
      }
}

<?xml version="1.0" encoding="UTF-8"?>
<messages>
<message id="1">
   <title>biaoti_1</title>
   <content>
    <name>zhanghua</name>
    <age>30</age>
   </content>
   <email>zhanghua123@126.com</email>
</message>
<message id="2">
   <title>biaoti_2</title>
   <content>
    <name>lining</name>
    <age>28</age>
   </content>
   <email>lining123@126.com</email>
</message>
</messages>

------------------------------- 1 结束----------------------

2.  -- jdom对XML(增,删,改,查)

  import java.io.FileInputStream;
	import java.io.FileNotFoundException;
	import java.io.FileOutputStream;
	import java.io.IOException;
	import java.io.InputStream;
	import java.io.OutputStream;
	import java.util.List;

	import org.jdom.Document;
	import org.jdom.Element;
	import org.jdom.JDOMException;
	import org.jdom.input.SAXBuilder;
	import org.jdom.output.XMLOutputter;

	/**
	 * 
	 */
	public class JavaXML {

	 
	 //解析xml文件
	 
	 public static void XmlParse() throws JDOMException, IOException {
	  SAXBuilder builder = new SAXBuilder();
	  InputStream file = new FileInputStream("src/xml/po.xml");
	  Document document = builder.build(file);//获得文档对象
	  Element root = document.getRootElement();//获得根节点
	  List<Element> list = root.getChildren();
	  for(Element e:list) {
	   System.out.println("ID="+e.getAttributeValue("id"));
	   System.out.println("username="+e.getChildText("username"));
	   System.out.println("password="+e.getChildText("password"));
	  }
	 }
	 
	 //增
	 public static void addXml() throws JDOMException, FileNotFoundException, IOException {
	  SAXBuilder builder = new SAXBuilder();
	  Document doc = builder.build("src/xml/po.xml");//获得文档对象
	  Element root = doc.getRootElement();//获得根节点
	  
	  //添加新元素
	  Element element = new Element("person");
	  element.setAttribute("id", "3");
	  Element element1 = new Element("username");
	  element1.setText("HONGWEI");
	  Element element2 = new Element("password");
	  element2.setText("mima");
	  element.addContent(element1);
	  element.addContent(element2);
	  root.addContent(element);
	  doc.setRootElement(root);
	  
	  //文件处理
	  XMLOutputter out = new XMLOutputter();
	  out.output(doc, new FileOutputStream("src/xml/po.xml"));
	 }
	 
	 //根据ID值删除一个节点 
	 public static void deletePerson(int id) throws JDOMException, IOException {
	  SAXBuilder builder = new SAXBuilder();
	  InputStream file = new FileInputStream("src/xml/po.xml");
	  Document doc = builder.build(file);//获得文档对象
	  Element root = doc.getRootElement();//获得根节点
	  List<Element> list = root.getChildren();
	  for(Element e:list) {
	   //获取ID值
	   if(Integer.parseInt(e.getAttributeValue("id"))==id) {
	    root.removeContent(e);
	    break;//??
	   }
	  }
	  
	  //文件处理
	  XMLOutputter out = new XMLOutputter();
	  out.output(doc, new FileOutputStream("src/xml/po.xml"));
	 }
	 
	 //根据ID值修改一个节点 
	 public static void updatePerson(int id) throws JDOMException, IOException {
	  SAXBuilder builder = new SAXBuilder();
	  InputStream file = new FileInputStream("src/xml/po.xml");
	  Document doc = builder.build(file);//获得文档对象
	  Element root = doc.getRootElement();//获得根节点
	  List<Element> list = root.getChildren();
	  for(Element e:list) {
	   //获取ID值
	   if(Integer.parseInt(e.getAttributeValue("id"))==id) {
	    System.out.println("--------------------");
	    e.getChild("username").setText("111111111");
	    e.getChild("password").setText("password");
	    
	   }
	  }
	  
	  //文件处理
	  XMLOutputter out = new XMLOutputter();
	  out.output(doc, new FileOutputStream("src/xml/po.xml"));
	 }
	 
	 static public void main(String ars[]) throws JDOMException, IOException {
	  
	  //addXml();//增加XML
	 // deletePerson(3);//删除XML
	 // updatePerson(2);//修改XML
	  //XmlParse();//解析XML
	 }
	}

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <person id="1">
  <username>张三</username>
  <password>123123</password>
 </person>
 <person id="2">
  <username>1111111112</username>
  <password>password2</password>
 </person>

<person id="3">
<username>liming</username>
<password>mima</password></person>

<person id="3">
<username>liushan</username>
<password>mima</password></person>
</root>




----------------------- 2 结束 -------------------------------

3.  -- java split 与tokenizer的区别
          String sample1="ben        ben" ;    //其中连个ben之间间隔8个空格

           String[] split1 = sample1.split(" "); //通过一个空格隔离

           StringTokenizer tokens = new StringTokenizer(sample1, " "); 
         List ls = new ArrayList();
while(tokens.hasMoreTokens()){
String str = (String)tokens.nextElement();
ls.add(str);
}

结果:split1.length 为 9  ls.size 为 2
解释:如果用split进行分离的话,他会将空格也作为一个字符串存入数组, 而tokenizer不会

--------------------------- 3 结束-------------------------------------

4. 把不连续的数字按部分连续的段分组(代码见附件TestGroup.rar)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;


public class TestGroup {
	public static void main(String args[]){
	int[] systemArray = {10,11,12,13,55,606,56,57,58,59,61,70,79,80,86,87,88,89,600,800};
	
	TestGroup test = new TestGroup();
	Arrays.sort(systemArray);
	List listGroup = (ArrayList) test.getSystemGroup(systemArray);
	System.out.println(" ------------begin----------");
	for(int i = 0; i < listGroup.size(); i++){
		
		List pageList = (List)listGroup.get(i); //get page
		
		System.out.println("current page is:" + i);
		System.out.println("页范围:" + pageList.get(0) + "--" + pageList.get(1));
		
	}
	System.out.println(" ------------end----------");	
	}
	//systemList 是排序过的不连续的系统号码
	public List<Object> getSystemGroup(int[] systemArray){
		
		List<Object> groupList = new ArrayList<Object>();//保存每段的pageList
		List<Integer> pageList = new ArrayList<Integer>();
		
		Integer pageMinNum = 0;//当前连续页系统号最小值
		Integer pageMaxNum = 0;//当前连续页系统号最大值
		
		//如果是连续的取(systemMin取首个数据)和(systemMax取第二个数据)保存到pageList.add
		//
		int systemArrayLen = systemArray.length;
		
		//如果当前数据和上一个数据是连续的数据,设置systemMaxNum = 当前数据,继续下一个数据判断,
		//连续的数据从systemMin到systemMin为一页。
		//连续的判断是当前数据+1 是否等于上一个数据。
		for(int i = 0; i < systemArrayLen; i++){		
			
			if(i == 0){
				pageList = new ArrayList<Integer>();
				pageMinNum = systemArray[i];
				pageMaxNum = systemArray[i];
				pageList = new ArrayList<Integer>();
				pageList.add(pageMinNum);
				pageList.add(pageMaxNum);
				groupList.add(pageList);
				//如果当前数据不是最后一个数据,则下一页的最小值是当前下一个数据( systemList.get(i+1) );
				if((i + 1) < systemArrayLen){
					pageMinNum = systemArray[i+1];
				}
				
			
			}else{
				
				int preSystemNum = 0;
				int currentSystemNum = 0;
				
				preSystemNum = systemArray[i-1];
				currentSystemNum = systemArray[i];
				//连续的数据,继续判断下一个
				if(currentSystemNum == (preSystemNum + 1)){
					pageMaxNum = currentSystemNum;
					
					//如果当前是最后一个数据,不再判断
					if(i == (systemArrayLen - 1) ){
						pageList = new ArrayList<Integer>();
						pageList.add(pageMinNum);
						pageList.add(pageMaxNum);
						groupList.add(pageList);
					}
					
				}
				else{//不连续的数据,只有一个的情况
					pageList = new ArrayList<Integer>();
					pageList.add(pageMinNum);
					pageList.add(pageMaxNum);
					groupList.add(pageList);
					
					//如果当前数据不是最后一个数据,则下一页的最小值是当前数据( currentSystemNum);
					if((i + 1) < systemArrayLen){
						pageMinNum = currentSystemNum;
						pageMaxNum = pageMinNum;//如果是不连续数据,只一页,设置最大值等于最小值。
					}
					
					//如果当前是最后一个数据,不再判断
					if(i == systemArrayLen - 1){
						pageMinNum = currentSystemNum;
						pageMaxNum = pageMinNum;//如果是不连续数据,只一页,设置最大值等于最小值。
						pageList = new ArrayList<Integer>();
						pageList.add(pageMinNum);
						pageList.add(pageMaxNum);
						groupList.add(pageList);
					}
				}
				
			}
								
		}
		
		return groupList;
		
	}
	
	
}


------------------------------- 4结束----------------------


5同步的例子

public class TestCurrentThread {

	private static int[]arrayb = new int[100];
	private static int countNum; //计数器
	
	private synchronized int addQueryNeCount(){
		
		
		if(countNum <= 2000){
			countNum += 20;
		}
		
		System.out.println(Thread.currentThread().getName()+" 当前线程查询出 countNum:" + countNum);
		return countNum;
		
	}
	
	private synchronized int abstractQueryNeCount(){
		
		
		if(countNum >= 0){
			countNum -= 30;
		}
		
		System.out.println(Thread.currentThread().getName()+" 当前线程查询出 countNum:" + countNum);
		return countNum;
		
	}
	
	//同步部件方法导致阻塞
	public  synchronized void printArraya(){
		
		//查询网元个数,如果网元个数大于60,当前线程阻塞
		int neCount = addQueryNeCount();
		
		while(neCount > 100){
			System.out.println(Thread.currentThread().getName()+"111111111111111111111111");
			System.out.println(Thread.currentThread().getName()+":当前部件线程被阻塞--count:"+countNum);
			try {
				wait();
				System.out.println(Thread.currentThread().getName()+"ccccccccccccccccccccccccccc");
			
			} catch (InterruptedException e) {
				
				e.printStackTrace();
			}
		}
		System.out.println(Thread.currentThread().getName()+":部件线程同步完成 --count:"+countNum);
	}
	
	//同步部件方法解除阻塞
	public  synchronized void printArrayb(){
		
		//查询网元个数,如果网元个数大于60,当前线程阻塞
		int neCount = abstractQueryNeCount();
		
		if(neCount >100){
			System.out.println(Thread.currentThread().getName()+":正在解除被阻塞的线程塞--count:"+countNum);
			
		}else{
			System.out.println(Thread.currentThread().getName()+":已经解除阻塞--count:"+countNum);
			notifyAll();
		}
	}
	
    public void test(){
    	System.out.println("当前线程:"+ Thread.currentThread().getName());
    	InnerBlockClass innerThread1 = new InnerBlockClass();
    	InnerBlockClass innerThread2 = new InnerBlockClass();
    	InnerBlockClass innerThread3 = new InnerBlockClass();
    	InnerBlockClass innerThread4= new InnerBlockClass();
		InnerBlockClass innerThread5 = new InnerBlockClass();
		InnerBlockClass innerThread6 = new InnerBlockClass();
		InnerBlockClass innerThread7 = new InnerBlockClass();
		InnerBlockClass innerThread8 = new InnerBlockClass();
		InnerBlockClass innerThread9 = new InnerBlockClass();
		
		innerThread1.start();
		innerThread2.start();
		innerThread3.start();
		innerThread4.start();
		innerThread5.start();
		innerThread6.start();
		innerThread7.start();
		innerThread8.start();
		innerThread9.start();
		
		
		
		InnerUnBlockClass innerOpenThread1 = new InnerUnBlockClass();
		InnerUnBlockClass innerOpenThread2 = new InnerUnBlockClass();
		InnerUnBlockClass innerOpenThread3 = new InnerUnBlockClass();
		InnerUnBlockClass innerOpenThread4= new InnerUnBlockClass();
		InnerUnBlockClass innerOpenThread5 = new InnerUnBlockClass();
		InnerUnBlockClass innerOpenThread6 = new InnerUnBlockClass();
		InnerUnBlockClass innerOpenThread7 = new InnerUnBlockClass();
		InnerUnBlockClass innerOpenThread8 = new InnerUnBlockClass();
		
		innerOpenThread1.start();
		innerOpenThread2.start();
		innerOpenThread3.start();
		innerOpenThread4.start();
		innerOpenThread5.start();
		innerOpenThread6.start();
		innerOpenThread7.start();
		innerOpenThread8.start();
    }
	public static void main(String args[]){
		TestCurrentThread test = new TestCurrentThread();
		test.test();
		
	}
	//导致阻塞的线程
	private class InnerBlockClass extends Thread{
		public void run(){
			printArraya();
		}
	}
	
	//解除阻塞的线程
	private class InnerUnBlockClass extends Thread{
		public void run(){
			printArrayb();
		}
	}
}