Eclipse编码问题汇总

在Eclipse中,默认编码是GBK

当我们通过网络取得一些内容时,直接System.Out.println结果会显示不正确

keyWord = URLEncoder.encode(keyWord, "UTF-8"); URL url = new URL("http://www.google.com/search?q=" + keyWord + "&hl=en&ie=UTF-8&num=" + num + "&start=" + begin + "&sa=N"); HttpURLConnection cont = (HttpURLConnection) url.openConnection(); cont.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); cont.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(cont.getInputStream()));

 

为了让结果显示正确,我们应该把Eclipse的编码方式设置为utf-8

 

但是这样的话,从本地电脑读取文件又会出问题,因为本地文件是以gbk编码存放的,如下所示

 

BufferedReader reader = new BufferedReader(new FileReader("d:/aa.txt")); //错误,编码方式不对 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("d:/aa.txt"), "GBK")); //正确,强制以GBK编码读取文件

你可能感兴趣的:(eclipse,windows,网络,url)