Java 从类路径加载资源

String fileName = getClass().getResource("/log4j.properties").getFile();

Java 在java中使用'split'方法,使用2个或更多空格作为分隔符

String str="This is my   Red    house";
String[] result=str.split("  +");

           for (int i=0;i<result.length;i++)
           {
	     System.out.println("The result is ==== "+result[i]);
			   
            }

Java PROVA

    HttpServletRequest request = PortalUtil
        .getHttpServletRequest(portletRequest);

Java TouchOSC和Processing Accelerometer示例代码

import processing.opengl.*;
import oscP5.*;

OscP5 oscP5;

float xrot = 0;
float zrot = 0;

float xrot_targ = 0;
float zrot_targ = 0;
float orientation = 0;

float dampSpeed = 5;

void setup() {
  size(400,400, OPENGL);
  oscP5 = new OscP5(this,8000);
  smooth();
}

void draw() {
  camera(  0, 0, 300,
         0, 0, 0,
         0.0, 1.0, 0.0
     );
  background(0); 

  // Basic value smoothing

  if (xrot_targ > xrot) {
    xrot = xrot + ((xrot_targ - xrot) / dampSpeed);
  } else {
    xrot = xrot - ((xrot - xrot_targ) / dampSpeed);
  }

  if (zrot_targ > zrot) {
    zrot = zrot + ((zrot_targ - zrot) / dampSpeed);
  } else {
    zrot = zrot - ((zrot - zrot_targ) / dampSpeed);
  }

 // Detection for if the iPhone is upsidedown or not

  if (orientation < 0) {
    fill(255,0,0);
    rotateX(radians(xrot));
    rotateZ(radians(zrot));
  } else {
    fill(255,255,0);
    rotateX(radians(xrot*-1));
    rotateZ(radians(zrot*-1));
  }
  box(130,10,60);

}

void oscEvent(OscMessage theOscMessage) {
  if(theOscMessage.checkAddrPattern("/accxyz")==true) {
      xrot_targ = (theOscMessage.get(0).floatValue()*90);
      zrot_targ = (theOscMessage.get(1).floatValue()*90)*-1;
      orientation = theOscMessage.get(2).floatValue();
  }
}

Java java网页下载

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Main.java
 *
 * @author www.javadb.com
 */
public class Main {
    
    /**
     * Reads a web page into a StringBuilder object
     * and prints it out to console along with the
     * size of the page.
     */
    public void getWebSite() {
        
        try {
            
            URL url = new URL("http://www.google.com");
            URLConnection urlc = url.openConnection();
            
            BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());
            
            StringBuilder builder = new StringBuilder();
            int byteRead;
            while ((byteRead = buffer.read()) != -1)
                builder.append((char) byteRead);
            
            buffer.close();
            
            System.out.println(builder.toString());
            System.out.println("The size of the web page is " + builder.length() + " bytes.");
            
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    /**
     * Starts the program
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().getWebSite();
    }
}

Java 将X509证书转换为base64

X509Certificate cert;
BASE64Encoder encoder=new BASE64Encoder();
String psB64Certificate = encoder.encodeBuffer(cert.getEncoded());

Java 使用DocumentBuilder的DOM Parser演示。

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class DocumentBuilderDemo {

	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilder db = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		db = dbf.newDocumentBuilder();
		Document doc = db.parse(new File("c:\\simple.xml"));
		
		Element root = doc.getDocumentElement();
		System.out.println(root.getNodeName());
		
		System.out.println("========================================================================");
		
		NodeList nodeList = root.getElementsByTagName("to");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		System.out.println("========================================================================");
		
		nodeList = root.getElementsByTagName("from");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
		
		System.out.println("========================================================================");
		
		nodeList = root.getElementsByTagName("body");
		
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i).getFirstChild();
			System.out.println(node.getNodeValue());
		}
	
		System.out.println("========================================================================");
	
		
	}

}


/*
The simple.xml file that we used for parsing in above example.


<?xml version="1.0"?>
<msg>
	<note>
		<to>Tom</to>
		<from>Prince</from>
		<heading>Reminder</heading>
		<body>Hello how are u?</body>
	</note>
	<note>
		<to>Sania</to>
		<from>Prince</from>
		<heading>Joking</heading>
		<body>Welcome!!!</body>
	</note>
</msg>

*/

Java 日期差异

Calendar calendar1 = Calendar.getInstance();
				Calendar calendar2 = Calendar.getInstance();
				DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = new Date();
				try {
					date = formatter.parse(trailLoginDate);
				} catch (Exception e) {
					// TODO: handle exception
				}
				calendar1.setTime(date);
				calendar2.setTime(new Date());
				long milliseconds1 = calendar1.getTimeInMillis();
				long milliseconds2 = calendar2.getTimeInMillis();
				long diff = milliseconds2 - milliseconds1;
				long diffDays = diff / (24 * 60 * 60 * 1000);
				days = (int)diffDays;

Java 从Android应用程序开始拨打电话

try {
   Intent intent = new Intent(Intent.ACTION_CALL);
   intent.setData(Uri.parse("tel:+123456"));
   startActivity(intent);
} catch (Exception e) {
   Log.e("SampleApp", "Failed to invoke call", e);
}

Java DOM HTML输出

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DocumentType;

public class Output {
	
	public static void htmlOutput(ArrayList<Question> fiveQuestions){
		
		DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = dbfactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Document doc = builder.newDocument();
		DOMSource source = new DOMSource(doc);
				
		Element html = doc.createElement("html");				
		doc.appendChild(html);
		
		Element head = doc.createElement("head");
		html.appendChild(head);
		
		Element title = doc.createElement("title");
		head.appendChild(title);
		title.appendChild(doc.createTextNode("Quit HTML Output"));
		
		Element body = doc.createElement("body");
		html.appendChild(body);
		
		Element h1 = doc.createElement("h1");
		body.appendChild(h1);
		h1.appendChild(doc.createTextNode("Fünf Fragen aus dem Gebiet " + fiveQuestions.get(0).getArea()));
		
		Element br = doc.createElement("br");
		body.appendChild(br);
		
		
		for (Iterator<Question> fiveIterator = fiveQuestions.iterator() ; fiveIterator.hasNext();) {
			
			Element p = doc.createElement("p");
			body.appendChild(p);
			p.appendChild(doc.createTextNode((fiveIterator.next().getQuestion())));
			
			Element ul = doc.createElement("ul");
			body.appendChild(ul);
			
			
			
			for (Iterator<String> answerIterator = fiveIterator.next().getAnswers().iterator(); answerIterator.hasNext();) {
				Element li = doc.createElement("li");
				ul.appendChild(li);
				li.appendChild(doc.createTextNode(answerIterator.next()));
				
				
			}
			
		}
		
		StreamResult result = new StreamResult(new File("./data.html"));
		System.out.println("tata");
		
		Transformer transformer;
		try {
			transformer = TransformerFactory.newInstance().newTransformer();
		//	transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC);
			transformer.transform(source, result);
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}