java_temp

● [b]读取类所在的绝对路径[/b]
Class c = this.getClass();
String classPath = "/" + c.getName().replace('.', '/') + ".class";
String path = c.getResource(classPath).getPath();
int end = path.lastIndexOf('!'); //是否被打成jar
if (end == -1){
System.out.println(c.getResource("/").getPath());
}
else{
while(end > 0 && path.charAt(end) != '/') end --;
int start = path.indexOf('/');
System.out.print(path.substring(start, end + 1));
}


● [b]中文与unicode互转[/b]
public String toUnicode(String chiness){
if (chiness == null) return null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chiness.length(); i ++){
sb.append("\\u").append(Integer.toHexString(chiness.charAt(i) & 0xffff));
}
return sb.toString();
}
public String toChiness(String unicode){
if (unicode == null) return null;
StringBuilder sb = new StringBuilder();
String[] chars = unicode.split("\\\\u");
for (int i = 1; i < chars.length; i ++){
sb.append((char)Integer.parseInt(chars[i], 16));
}
return sb.toString();
}

● [b]MD5加密[/b]
public static String md5(String src){
if (src == null)
return null;
try{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(src.getBytes());
StringBuilder dist = new StringBuilder();
for (byte b : md.digest()){
String s = Integer.toHexString(0xFF & b);
if (s.length() == 1) dist.append("0");
dist.append(s);
}
return dist.toString();
}
catch (Exception e){
throw new RuntimeException(e);
}
}


● [b]native2ascii的使用[/b]
native2ascii [options] [inputfile [outputfile]]
说明:
-reverse
Perform the reverse operation: convert a file with Latin-1 and/or
Unicode encoded characters to one with native-encoded characters.

-encoding encoding_name
Specify the encoding name which is used by the conversion procedure.
The default encoding is taken from System property file.encoding. The
encoding_name string must be taken from the first column of the table of
supported encodings in the Supported Encodings document.

-Joption
Pass option to the Java virtual machine, where option is one of the
options described on the reference page for the java application launcher.

For example, -J-Xms48m sets the startup memory to 48 megabytes

举例:native2ascii -reverse -encoding utf8 DevDescrib.java DevDescrib_.java

● [b]获取本机IP地址[/b]
InetAddress[] ips = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
for (InetAddress ia : ips){
System.out.println(ia.getHostAddress());
}

● [b]正则表达式的贪婪模式与非贪婪模式[/b]
用*匹配任意多的字符时,默认为贪婪模式,即匹配尽量多的字符。如用[b]a.*b[/b]来匹配a11ba22b,将会匹配整个字符串。
如果使用非贪婪模式,只需在[b]*[/b]后面添加[b]?[/b],即匹配尽量少的字符。还是上面的例子,改用[b]a.*?b[/b]来匹配,则匹配两个字符串:[b]a11b[/b]和[b]a22b[/b]。

● [b]发送POST请求,注意参数的格式[/b]
URL url = new URL("http://jscompress.com/");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
//发送请求参数,注意,参数一定要用URLEncoder.encode(),否则遇到一些字符不正常,如'+'
out.write("param_name=" + URLEncoder.encode("param_value", "utf-8"));
out.flush();
out.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值