工作中一些功能

1、内网穿透
内网穿透的目的是:让外网能访问你本地的应用
ngrok是一个反向代理,通过在公共的端点和本地运行的Web服务器之间建立一个安全的通道。
ngrok使用go语言开发,源代码分为客户端与服务器端。
国内免费服务器:http://ngrok.ciqiuwl.cn/

1,下载windows版本的客户端,解压到你喜欢的目录
2,在命令行下进入到ngrok客户端目录下
3,执行 ngrok -config=ngrok.cfg -subdomain xxx 80 //(xxx 是你自定义的域名前缀)
4,如果开启成功 你就可以使用 xxx.ngrok.xiaomiqiu.cn 来访问你本机的 127.0.0.1:80 的服务啦
5,如果你自己有顶级域名,想通过自己的域名来访问本机的项目,那么先将自己的顶级域名解析到120.78.180.104(域名需要已备案哦),然后执行 ngrok -config=ngrok.cfg -hostname xxx.xxx.xxx 80 //(xxx.xxx.xxx是你自定义的顶级域名)
6,如果开启成功 你就可以使用你的顶级域名来访问你本机的 127.0.0.1:80 的服务啦

这个是做个临时的服务,如果要长期的,需要自己搭建服务器。

2、读取文件的最后一行
File file = new File(“F:/2”);
RandomAccessFile accessFile = new RandomAccessFile(file, “r”);
long len = accessFile.length();
long length = len - 1;
long pos = length;
while (pos > 0) {
accessFile.seek(pos);
byte b = accessFile.readByte();
if (b == ‘\n’) {
accessFile.seek(pos + 1);
break;
}
pos–;
}
if (pos == 0) accessFile.seek(pos);
byte[] m = new byte[(int) (length - pos)];
accessFile.read(m);
String s = new String(m, Charset.forName(“utf-8”));
System.out.println(s);
accessFile.close();

最后一行为空和最后一行不为空也有些差别,按具体情况处理。

3、xml解析
XML的解析方式分为四种:1、DOM解析;2、SAX解析;3、JDOM解析;4、DOM4J解析。其中前两种属于基础方法,是官方提供的平台无关的解析方式;后两种属于扩展方法,它们是在基础的方法上扩展出来的,只适用于java平台。
book.xml

<?xml version="1.0" encoding="UTF-8"?> 计算机科学与技术 张三 2014 89 java从入门到精通 2014 77 English spring从入门到精通 2014 63 1)dom解析:对内存的耗费比较大,效率不十分理想 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); InputStream stream = Test5.class.getResourceAsStream("book.xml"); Document document = builder.parse(stream);
	NodeList list = document.getElementsByTagName("book");
	int len = list.getLength();
	System.out.println("一共" + len + "本书");
	for (int i = 0; i < len; i++) {
		Node node = list.item(i);
		NodeList childList = node.getChildNodes();
		int v = childList.getLength();
		for (int j = 0; j < v; j++) {
			Node m = childList.item(j);
			if (m.getNodeType() == node.ELEMENT_NODE)
				System.out.println(m.getNodeName() + ":" + m.getTextContent() + "\t");
		}
	}

2)SAX的全称是Simple APIs for XML:编码比较麻烦,很难同时访问XML文件中的多处不同数据
public class SAXParserHandler extends DefaultHandler{

String value = null;
Book book = null;
int bookIndex = 0;
private ArrayList<Book> bookList = new ArrayList<Book>();
public ArrayList<Book> getBookList() {
    return bookList;
}

@Override
public void startDocument() throws SAXException {
	
	super.startDocument();
}

@Override
public void endDocument() throws SAXException {
	
	super.endDocument();
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
	
	super.startElement(uri, localName, qName, attributes);
	if (qName.equals("book")) {
		bookIndex++;
		book = new Book();
		int num = attributes.getLength();
		for (int i = 0; i < num; i++) {
			System.out.println(attributes.getQName(i) + ":" 
					+ attributes.getValue(i));
			if (attributes.getQName(i).equals("id")) {
                book.setId(attributes.getValue(i));
            }
		}
	}
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
	// TODO Auto-generated method stub
	super.endElement(uri, localName, qName);
	if (qName.equals("book")) {
		bookList.add(book);
        book = null;
	} else if (qName.equals("name")) {
        book.setName(value);
    }
    else if (qName.equals("author")) {
        book.setAuthor(value);
    }
    else if (qName.equals("year")) {
        book.setYear(value);
    }
    else if (qName.equals("price")) {
        book.setPrice(value);
    }
    else if (qName.equals("language")) {
        book.setLanguage(value);
    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
	// TODO Auto-generated method stub
	super.characters(ch, start, length);
	if (ch != null) {
		value = new String(ch, start, length);
        if (!value.trim().equals("")) {
            System.out.println("节点值是:" + value);
        }
	}
	
}

}

执行方法
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parse = factory.newSAXParser();
InputStream stream = Test6.class.getResourceAsStream(“book.xml”);
SAXParserHandler handler = new SAXParserHandler();
parse.parse(stream, handler);
for (Book book : handler.getBookList()) {
System.out.println(book.getId());
System.out.println(book.getName());
System.out.println(book.getAuthor());
System.out.println(book.getYear());
System.out.println(book.getPrice());
System.out.println(book.getLanguage());
System.out.println("----finish----");
}

3)JDom解析
private static ArrayList booksList = new ArrayList();
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder saxBuilder = new SAXBuilder();
InputStream in;
in = Test7.class.getResourceAsStream(“book.xml”);
Document document = saxBuilder.build(in);
Element element = document.getRootElement();
List list = element.getChildren();
int len = list.size();
for (int i = 0; i < len; i++) {
Book bookEntity = new Book();
Element ele = list.get(i);
List attributesList = ele.getAttributes();
for (Attribute attr : attributesList) {
String attrName = attr.getName();
String attrValue = attr.getValue();
if (attrName.equals(“id”)) {
bookEntity.setId(attrValue);
}
}
List bookChilds = ele.getChildren();
for (Element child : bookChilds) {
System.out.println(“节点名:” + child.getName() + “----节点值:”
+ child.getValue());
if (child.getName().equals(“name”)) {
bookEntity.setName(child.getValue());
}
else if (child.getName().equals(“author”)) {
bookEntity.setAuthor(child.getValue());
}
else if (child.getName().equals(“year”)) {
bookEntity.setYear(child.getValue());
}
else if (child.getName().equals(“price”)) {
bookEntity.setPrice(child.getValue());
}
else if (child.getName().equals(“language”)) {
bookEntity.setLanguage(child.getValue());
}
}

        booksList.add(bookEntity);
        bookEntity = null;
        System.out.println(booksList.size());
        System.out.println(booksList.get(0).getId());
        System.out.println(booksList.get(0).getName());
        
    }

}

4)DOM4J性能最好
SAXReader reader = new SAXReader();
InputStream input = Test8.class.getResourceAsStream(“book.xml”);
Document document = reader.read(input);
Element bookStore = document.getRootElement();
Iterator it = bookStore.elementIterator();
while (it.hasNext()) {
Element book = (Element) it.next();
List bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
System.out.println(“属性名:” + attr.getName() + “–属性值:”
+ attr.getValue());
}
Iterator itt = book.elementIterator();
while (itt.hasNext()) {
Element bookChild = (Element) itt.next();
System.out.println(“节点名:” + bookChild.getName() + “–节点值:” + bookChild.getStringValue());
}
System.out.println("=结束遍历某一本书=");
}

3、DecimalFormat用法
“0” 指定位置不存在数字则显示为0
“#” 指定位置不存在数字则不显示
DecimalFormat df1 = new DecimalFormat(“0.0”);

DecimalFormat df2 = new DecimalFormat("#.#");

DecimalFormat df3 = new DecimalFormat(“000.000”);

DecimalFormat df4 = new DecimalFormat("###.###");

System.out.println(df1.format(12.34));

System.out.println(df2.format(12.34));

System.out.println(df3.format(12.34));

System.out.println(df4.format(12.34));

结果
12.3

12.3

012.340

12.34

猜你喜欢

转载自blog.csdn.net/zhanglinlove/article/details/84594768