开发者社区> 问答> 正文

JAVA 字符转码怎么就是转不了 :报错

\u4e16\u754c\u4e0a\u7684\u4e00\u5207\uff0c\u8fd8\u662f\u771f\u7684\u5417\uff1f

我用正则式从网络的源码中截取这一段,不管怎么转码都无效,但是如果我复制到

a=\u4e16\u754c\u4e0a\u7684\u4e00\u5207\uff0c\u8fd8\u662f\u771f\u7684\u5417\uff1f

System.out.println(a);

以上代码就可以通过

展开
收起
kun坤 2020-06-06 18:20:38 673 0
1 条回答
写回答
取消 提交回答
  • IDE是可以设置自己的默认编码的,Eclipse和Idea都支持;设置为UTF-8就好。######@胖猫 : 你发的这段代码没有意义好像,System.out.println(unDecodedString);直接就可以打出,你发的代码应该是linux下的(utf-8),在windows下(gbk)乱码。######我试过了才发帖的,会不会是你本地的编码不对?######我试过不行,就如同前面所说的应该\u其实是\u,转义后只有一个######

    package moxia;

    import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader;

    public class AsciitoNative { public static void main(String[] args) { String str = "\u4e16\u754c\u4e0a\u7684\u4e00\u5207\uff0c\u8fd8\u662f\u771f\u7684\u5417\uff1f"; FileOutputStream fos = null; BufferedReader br = null; File txt=new File("test.txt"); if(!txt.exists()){ try { txt.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } byte bytes[]=new byte[512]; bytes=str.getBytes(); int b=str.length();

    	try {
    		fos = new FileOutputStream(txt);
    		fos.write(bytes,0,b); 
    	} catch (Exception e) {
    		e.printStackTrace();
    	} finally{
    		if(fos!=null){
    			try {
    				fos.close();
    				//txt.delete();
    			} catch (IOException e) {
    				e.printStackTrace();
    			} 
    		}
    	}
    	try {
    		Process p=Runtime.getRuntime().exec("native2ascii.exe -reverse " + txt.getAbsolutePath());
    		br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    		String n;
    		while ((n=br.readLine())!=null){
    			System.out.println(n);
    		}
    	} catch (IOException e) {
    		e.printStackTrace();
    	} finally {
    		try {
    			if(br!=null){
    				br.close();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    }

    ######ide环境编码是utf8,你程序单独运行环境编码不一定设定为utf8,因此需要显示设定,不然就会出现你那种乱码

    2020-06-06 18:25:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载