在打印台输出的时候出现 中文乱码, 我在字符串上面做了编码转换都不行, 高手们帮下手,谢谢/*
* 获取系统记录的开机时间
*/
public static String readSystemStartTime() throws IOException,
InterruptedException {
Process process = Runtime.getRuntime().exec(
"cmd /c net statistics workstation");
String startUpTime = "";
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int i = 0;
String timeWith = "";
while ((timeWith = bufferedReader.readLine()) != null) {
if (i == 3) {
//System.out.println(timeWith);
startUpTime = timeWith;
}
i++;
}
String startTime = new String(startUpTime.getBytes("ISO8859-1"), "UTF-8");
System.out.println("===="+startTime);
process.destroy();
return startUpTime;
} public static void main(String[] args) throws IOException,
InterruptedException {
System.out.println(new ShutDownServiceImpl().readSystemStartTime());
}
* 获取系统记录的开机时间
*/
public static String readSystemStartTime() throws IOException,
InterruptedException {
Process process = Runtime.getRuntime().exec(
"cmd /c net statistics workstation");
String startUpTime = "";
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int i = 0;
String timeWith = "";
while ((timeWith = bufferedReader.readLine()) != null) {
if (i == 3) {
//System.out.println(timeWith);
startUpTime = timeWith;
}
i++;
}
String startTime = new String(startUpTime.getBytes("ISO8859-1"), "UTF-8");
System.out.println("===="+startTime);
process.destroy();
return startUpTime;
} public static void main(String[] args) throws IOException,
InterruptedException {
System.out.println(new ShutDownServiceImpl().readSystemStartTime());
}
String startTime = new String(startUpTime.getBytes("GB18030"), "UTF-8");
是多余的,默认应该就是GBK编码的。
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {/**
* @param args
*/
public static void main(String[] args) throws Exception{
System.out.println(Test.readSystemStartTime());
}
public static String readSystemStartTime() throws IOException,
InterruptedException {
Process process = Runtime.getRuntime().exec(
"cmd /c net statistics workstation");
String startUpTime = "";
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int i = 0;
String timeWith = "";
while ((timeWith = bufferedReader.readLine()) != null) {
if (i == 3) {
System.out.println(timeWith);
startUpTime = timeWith;
}
i++;
}
process.waitFor();
return startUpTime;
}
}
你的是有道理的, 我手工在cmd 下面 运行 java Test
它就没有乱码我看了看 我的MyEclipse , Text file encoding 属性, 没有GBK
我用的是UTF-8
这样如何整好呢?
但是
如果我需要UTF-8的文件呢?
//原串输出
System.out.println("time="+startUpTime);//ISO8859-1到UTF-8
String startTime1 = new String(startUpTime.getBytes("ISO8859-1"), "UTF-8");
System.out.println("time1="+startTime1);//ISO8859-1到GBK
String startTime2 = new String(startUpTime.getBytes("ISO8859-1"), "GBK");
System.out.println("time2="+startTime2);//GBK到UTF-8
String startTime3 = new String(startUpTime.getBytes("GBK"), "UTF-8");
System.out.println("time3="+startTime3);//UTF-8到GBK
String startTime4 = new String(startUpTime.getBytes("UTF-8"), "GBK");
System.out.println("time4="+startTime4);看看到底哪个正确
楼上的 那几个测试都不行
http://topic.csdn.net/u/20110125/09/a98c0dd6-9b69-4d75-b5a2-24f2b7508c5d.html