日期:2014-05-17  浏览次数:20761 次

坐等求解! myeclipse文本输出截断and某些字符的乱码问题
public class pachong {
  public static void main(String[] args) throws Exception { 
  HttpClient httpClient = new DefaultHttpClient();//创建httpclient对象  
 
  //以下对url的”query=“后的内容更改,将所有可能的情况遍历一次,每次访问一次网站  
   
  String string="abddd";
   
   
   
  String uriString="http://dict.51daifu.net/search?query="+string;
  java.net.URI uri=new java.net.URI(uriString);
  HttpGet request=new HttpGet(uri);// HTTP请求
  
 System.out.println("executing request " + request.getURI()); //打印出请求的网站
 HttpResponse response = httpClient.execute(request); //用httpclient对象执行httpget对象也就是url
HttpEntity entity = response.getEntity();//定义承载反馈的对象entity  
if(entity!=null){
BufferedReader reader = new BufferedReader(  
  new InputStreamReader(entity.getContent()));  
String str = null;  
  
Scanner scanner = new Scanner(reader);  
if (scanner != null) {
scanner.useDelimiter("\\Z");
str = scanner.next(); //遍历entity查找 才能完全输出反馈的源码  
str=new String (str.getBytes("GBK"),"utf-8");//编码转换 utf-8转换为gbk  
FileWriter outFileWriter=new FileWriter("D:\\first\\c.txt");//查询什么字生成该字的txt文件?
int x=str.length();
outFileWriter.write(str);
System.out.println(x);
System.out.println(str);
}
   
  else System.out.println("请输入正确的地址");}
  }
}
请教高手指点!为什么我输出到指定生成的文件时出现代码截断!但是打印到显示器上是完整的!为什么泥 !有的字符显示还是乱码?谢谢各位了!


下面是乱码结果:“
药到病除网是国家药监??????沪C20090001)的少数几家网上药房之????




------解决方案--------------------
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent(),"utf-8")); //这里改一下
String str = null;
 
Scanner scanner = new Scanner(reader);
if (scanner != null) {
scanner.useDelimiter("\\Z");
str = scanner.next(); //str=new String (str.getBytes("GBK"),"utf-8");//这里去掉
你可以试试