java
知识点
URL
打印一个网址的信息
try {
URL url = new URL("http://www.baidu.com");
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String content = br.readLine();
while(content!=null){
System.out.println(content);
content = br.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
将一个地址的图片复制到本地
try {
URL url = new URL("http://cdn.duitang.com/uploads/item/201112/17/20111217144319_WW5Mj.thumb.600_0.jpg");
File fileCopy = new File("d:\\image.jpeg");
if(!fileCopy.exists()){
fileCopy.createNewFile();
}
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(fileCopy);
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
byte[] array = new byte[1024];
int len = is.read(array);
while(len!=-1){
os.write(array, 0, len);
len = is.read(array);
}
os.flush();
is.close();
os.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
UDP互联(非多线程)
接收方
public class UDPReceiver {
public static void main(String[] args) {
DatagramSocket receiver;
DatagramPacket packetR;
DatagramSocket send;
DatagramPacket packetS;
Scanner scanner = new Scanner(System.in);
try {
receiver = new DatagramSocket(8080, InetAddress.getLocalHost());
send = new DatagramSocket();
byte[] array = new byte[1024];
while(true){
packetR = new DatagramPacket(array, 1024);
receiver.receive(packetR);
System.out.println(new String(packetR.getData()));
String s = "小明:"+scanner.next();
byte[] arrayS = s.getBytes();
packetS = new DatagramPacket(arrayS,arrayS.length, InetAddress.getByName("192.168.0.65"), 8088);
send.send(packetS);
System.out.println("已发送");
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
发送方
public class UDPSend {
public static void main(String[] args) {
DatagramSocket receiver;
DatagramPacket packetR;
DatagramSocket send;
DatagramPacket packetS;
Scanner scanner = new Scanner(System.in);
try {
receiver = new DatagramSocket(8088, InetAddress.getLocalHost());
send = new DatagramSocket();
byte[] array = new byte[1024];
while(true){
String s = "小红:"+scanner.next();
byte[] arrayS = s.getBytes();
packetS = new DatagramPacket(arrayS,arrayS.length, InetAddress.getByName("192.168.0.65"), 8080);
send.send(packetS);
System.out.println("已发送");
packetR = new DatagramPacket(array, 1024);
receiver.receive(packetR);
System.out.println(new String(packetR.getData()));
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
XML数据解析
DOM解析
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();//创建dom解析器的工厂
try {
DocumentBuilder builder = factory.newDocumentBuilder(); //使用解析器工厂创建一个解析器
Document document = builder.parse("C:\\11.txt"); //使用解析器解析文档设生成倒树的document
NodeList list = document.getElementsByTagName("Weather");//得到文档中的所有weather标签
Node node = list.item(0); //得到第一个weather
Node child = node.getFirstChild(); //得到weather标签的第一个子标签city
Node next = child.getNextSibling(); //得到city的下一个标签
while(next!=null){ //判断有没有走到最后一个标签
if(next.getNodeType()==Node.ELEMENT_NODE){ //判断节点类型是ELEMENT_NODE
Node content = next.getFirstChild(); //得到该节点的第一个子节点不能为空
if(content!=null){
System.out.println(next.getFirstChild().getNodeValue().trim());
}
}
next = next.getNextSibling(); //得到下一个标签
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
SAX解析
新建一个继承于DefaultHandler的类
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MySAXHandler extends DefaultHandler{
private boolean isArrive = false;
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
if(isArrive){
System.out.println("解析到需要的内容:");
System.out.println(new String(ch,start,length));
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("解析文档结束");
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
isArrive = false;
System.out.println(localName+"解析到结束标签:"+qName);
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("开始解析文档");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
isArrive = qName.equals("city");
System.out.println(localName+"解析到开始标签:"+qName);
}
}
public class TestSax {
public static void main(String[] args) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
MySAXHandler handler = new MySAXHandler();
parser.parse(new File("c:\\11.txt"), handler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
JSON解析
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TestJSON {
public static void main(String[] args) {
String json = createJSON();
JSONObject obj= JSONObject.fromObject(json);
System.out.println(obj.getString("city"));
JSONObject today = obj.getJSONObject("today");
JSONArray array = today.getJSONArray("index");
for (int i = 0; i < array.size(); i++) {
JSONObject obj1 = array.getJSONObject(i);
System.out.println(obj1.getString("name"));
}
}
private static String createJSON(){
JSONObject obj = new JSONObject();
obj.put("city", "北京");
obj.put("cityid", "12345546");
JSONObject today = new JSONObject();
today.put("date", "2015-8-03");
today.put("week", "星期一");
JSONArray array = new JSONArray();
JSONObject index1 = new JSONObject();
index1.put("name", "吃饭");
index1.put("name", "砍人");
JSONObject index2 = new JSONObject();
index2.put("name", "喝酒");
JSONObject index3 = new JSONObject();
index3.put("name", "唱K");
array.add(index1);
array.add(index2);
array.add(index3);
today.put("index", array);
obj.put("today", today);
System.out.println(obj.toString());
return obj.toString();
}
}
运行结果: