Java在Linux下获取当前程序路径&解决中文编码问题
在Linux下,可以通过获取当前的类所在路径的方法获取当前路径,但如果路径中存在中文或者空格,则中文部分会显示成%xx,这是要对路径进行编码转换,具体代码如下:
/*通过获取当前Class所在位置来获取当前路径,如果该用户的文件夹不存在,则删除*/
public String FindCurrentParentPath(){
String Result="";
CODER Coder = new CODER();
String Path = this.getClass().getResource("Main.class").getPath().replaceAll("%20", " ");
String[] aa = Path.split("/");
int k = 3;
// System.out.println("aa="+aa[(aa.length-3)]);
if(aa[(aa.length-4)].equals("build")){
k = 4;
}
for(int i = 1;i<(aa.length-k);i++){
if(Coder.isUtf8Url(aa[i])){
String path1 = Coder.Utf8URLdecode(aa[i]);
// System.out.println(Coder.Utf8URLdecode(aa[i]));
Result = Result+"/"+path1;
}else{
String path2 = URLDecoder.decode(aa[i]);
// System.out.println(URLDecoder.decode(aa[i]));
Result = Result+"/"+path2;
}
}
if(System.getProperty("os.name").contains("Windows")){
Result = Result.replaceFirst("/", "");
}
return Result;
}
CODER类定义如下:
/*编码转换(把%..恢复成中文)*/
class CODER {
/**
* 转换编码 ISO-8859-1到GB2312
* @param text
* @return
*/
public String ISO2GB(String text) {
String result = "";
try {
result = new String(text.getBytes("ISO-8859-1"), "GB2312");
}
catch (UnsupportedEncodingException ex) {
result = ex.toString();
}
return result;
}
/**
* 转换编码 GB2312到ISO-8859-1
* @param text
* @return
*/
public String GB2ISO(String text) {
String result = "";
try {
result = new String(text.getBytes("GB2312"), "ISO-8859-1");
}
catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
return
相关文档:
Java学习从入门到精通
一、 JDK (Java Development Kit)
JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar)。不论什么Java应用服务器实质都是内置了某个版本的JDK。因此掌握JDK是学好Java的第一步。最主流的J ......
pgrep 是通过程序的名字来查询进程的工具,一般是用来判断程序是否正在运行。在服务器的配置和管理中,这个工具常被应用,简单明了;
用法:
#ps 参数选项 程序名
常用参数
-l 列出程序名和进程ID;
-o 进程起始的ID;
-n 进程终止的ID;
举例:
[root@localhost ~]# pgrep ......
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<net/if.h>
static char *bad_interface_names[] = {
"lo:",
"lo",
&n ......
1、查看IP Ifconfig
2、配置IP的方法:
A、 这种方法立即生效 但是重启将不会保存。(除了这个方法的其它方法都可以保存下来)
Ifconfig eth0 1.1.1.1 netmask 255.0.0.0 up
Ifconfig eth0:1 1.1.1.3 up 设置一个网卡多个IP的方法(在设置虚拟主机时有 ......