java 获取 主机名
import java.net.*;public class DomainName
{
public static void outHostName(InetAddress address, String s)
{
System.out.println("通过" + s + "创建InetAddress对象");
System.out.println("主 机 名:" + address.getCanonicalHostName());
System.out.println("主机别名:" + address.getHostName());
System.out.println("");
}
public static void main(String[] args) throws Exception
{
outHostName(InetAddress.getLocalHost(), "getLocalHost方法");
outHostName(InetAddress.getByName("www.ibm.com"), "www.ibm.com");
outHostName(InetAddress.getByName("www.126.com"), "www.126.com");
outHostName(InetAddress.getByName("202.108.9.77"), "202.108.9.77");
outHostName(InetAddress.getByName("211.100.26.121"), "211.100.26.121");
}
} --------------------编程问答-------------------- import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 获得盘符序列号
*/
public class GetDiskNum {
public static void main(String[] args) {
System.out.println(getHdSerialInfo());
}
public static String getHdSerialInfo() {
String line = "";
String HdSerial = "";//定义变量 硬盘序列号
try {
Process proces = Runtime.getRuntime().exec("cmd /c dir c:");//获取命令行参数
BufferedReader buffreader = new BufferedReader(
new InputStreamReader(proces.getInputStream()));
while ((line = buffreader.readLine()) != null) {
if (line.indexOf("卷的序列号是 ") != -1) { //读取参数并获取硬盘序列号
HdSerial = line.substring(line.indexOf("卷的序列号是 ")
+ "卷的序列号是 ".length(), line.length());
break;
// System.out.println(HdSerial);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return HdSerial;//返回硬盘序列号 卷的序列 非物理
}
} --------------------编程问答-------------------- import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.xtlh.cn.entity.Book;
public class SaxParseService extends DefaultHandler{
private List<Book> books = null;
private Book book = null;
private String preTag = null;//作用是记录解析时的上一个节点名称
public List<Book> getBooks(InputStream xmlStream) throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
SaxParseService handler = new SaxParseService();
parser.parse(xmlStream, handler);
return handler.getBooks();
}
public List<Book> getBooks(){
return books;
}
@Override
public void startDocument() throws SAXException {
books = new ArrayList<Book>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if("book".equals(qName)){
book = new Book();
book.setId(Integer.parseInt(attributes.getValue(0)));
}
preTag = qName;//将正在解析的节点名称赋给preTag
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("book".equals(qName)){
books.add(book);
book = null;
}
preTag = null;/**当解析结束时置为空。这里很重要,例如,当图中画3的位置结束后,会调用这个方法
,如果这里不把preTag置为null,根据startElement(....)方法,preTag的值还是book,当文档顺序读到图
中标记4的位置时,会执行characters(char[] ch, int start, int length)这个方法,而characters(....)方
法判断preTag!=null,会执行if判断的代码,这样就会把空值赋值给book,这不是我们想要的。*/
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if(preTag!=null){
String content = new String(ch,start,length);
if("name".equals(preTag)){
book.setName(content);
}else if("price".equals(preTag)){
book.setPrice(Float.parseFloat(content));
}
}
}
}
public class Book {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString(){
return this.id+":"+this.name+":"+this.price;
}
}
import java.io.InputStream;
import java.util.List;
import junit.framework.TestCase;
import com.xtlh.cn.demo.DomParseService;
import com.xtlh.cn.demo.SaxParseService;
import com.xtlh.cn.entity.Book;
public class ParseTest extends TestCase{
public void testSAX() throws Throwable{
SaxParseService sax = new SaxParseService();
InputStream input = this.getClass().getClassLoader().getResourceAsStream("book.xml");
List<Book> books = sax.getBooks(input);
for(Book book : books){
System.out.println(book.toString());
}
}
}
补充:Java , Java相关