FileOutputStream   fo = null;   
        DataOutputStream   ds = null;
        
        OutputStreamWriter osw = null;
        
    
  try{   
          fo   =   new   FileOutputStream(Common.strSLExtXMLWritePath+"lists"+xmlURL);   //set   output   file   path   and   name.   
          ds   =   new   DataOutputStream(fo);   
          //osw =   new   OutputStreamWriter(fo);
            
          ds.writeUTF(StrXMl.toString());
          //osw.write(StrXMl.toString());
          System.out.println(Common.strSLExtXMLWritePath+"lists"+xmlURL);
          //System.out.println("@@@System.out.osw"+osw.getEncoding());
          
          //osw.flush();
          //osw.close();
          ds.flush();   
          ds.close();   
          fo.close();   
  }catch(Exception ex){
  ex.printStackTrace();
  }
这是我写的代码.怎么会前面头有乱码?请指教.

解决方案 »

  1.   

    byte[] nbs=StrXMl.toString().getBytes();
    fo.write(nbs);
      

  2.   

    OutputStream   fo = null;   
            DataOutputStream   ds = null;
            FileWriter fw= null;
            OutputStreamWriter osw = null;
            
        
            try{   
              fo   =   new   FileOutputStream(Common.strSLExtXMLWritePath+"lists"+xmlURL);   //set   output   file   path   and   name.   
              osw =   new   OutputStreamWriter(fo,"UTF-8");
              BufferedWriter bfw=new BufferedWriter(osw);
              bfw.write(new String(StrXMl.toString().getBytes("UTF-8"),"UTF-8"));
              System.out.println(Common.strSLExtXMLWritePath+"lists"+xmlURL);
              bfw.flush();
              bfw.close();
              fo.close();   
            }catch(Exception ex){
             ex.printStackTrace();
            }
    可以已经
      

  3.   

    bfw.write(StrXMl.toString().getBytes("UTF-8"));
    好像只用写的时候指定编码就可以了