在打印台输出的时候出现 中文乱码, 我在字符串上面做了编码转换都不行, 高手们帮下手,谢谢/*
 * 获取系统记录的开机时间
 */
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());
}

解决方案 »

  1.   

    换成下面的试试
    String startTime = new String(startUpTime.getBytes("GB18030"), "UTF-8");
      

  2.   

    呵呵,没有Windows系统,帮不了你了。
      

  3.   

    正常来说不需要转换编码啊。除非你的JVM有问题,否则String startTime = new String(startUpTime.getBytes("ISO8859-1"), "UTF-8");
    是多余的,默认应该就是GBK编码的。
      

  4.   

    import java.io.BufferedReader;
    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; 

    }
     
      

  5.   


    你的是有道理的, 我手工在cmd 下面 运行 java Test
    它就没有乱码我看了看 我的MyEclipse , Text file encoding 属性, 没有GBK
    我用的是UTF-8
    这样如何整好呢?
      

  6.   

    在那个 Text file encoding 的下拉框 手工输入 GBK 就可以了
      

  7.   

    是喔,楼上的真行
     但是
    如果我需要UTF-8的文件呢?
      

  8.   

    各种测试ing
    //原串输出
    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);看看到底哪个正确
      

  9.   

    无语, 如果把文件 Text file encoding 变回 utf8 的话
    楼上的 那几个测试都不行
      

  10.   

    算了, 我把编码变成回GBK 算了 先结贴, 反正运行环境也是中文版的xp上面如果路过的 高手们 帮忙看看 另外一张贴CMD后台运行java命令 谢谢
    http://topic.csdn.net/u/20110125/09/a98c0dd6-9b69-4d75-b5a2-24f2b7508c5d.html