没法写,不可能写因为不能判断字符串是不是gbk编码。这个问题已经被提出到让人看了就厌的地步。先看看编码是什么东西吧,怎么编码的。

解决方案 »

  1.   

    private static void writeOutput(String strFN, String str)
        throws Exception
        {
            FileOutputStream fos = new FileOutputStream(strFN);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            BufferedWriter bw = new BufferedWriter(osw);        StringReader sr = new StringReader(str);
            BufferedReader br = new BufferedReader(sr);        String line = br.readLine();
            if (line != null)
                bw.write(line);
            while ((line = br.readLine()) != null)
            {
                bw.newLine();
                bw.write(line);
            }
            bw.flush();
            bw.close();
            br.close();
        }
      

  2.   

    如果楼主一定要写那么个函数来辨别一段字符串是不是含中文就用下面这个吧,
    public static boolean isISO88591(String src)
        {
            try
            {
                return isISO88591(src.getBytes("iso8859-1"));
            }
            catch (UnsupportedEncodingException ex)
            {
                return false;
            }
        }
        public static boolean isISO88591(byte[] src)
        {
            return isISO88591(new ByteArrayInputStream(src));
        }
        public static boolean isISO88591(InputStream src)
        {
            try
            {
                for (byte b = (byte)src.read(); b >= 0; b = (byte)src.read())
                {
                    if ((b & (byte)0x80) == 0x80)
                    {
                        return false;
                    }
                }
                return true;
            }
            catch (IOException ex)
            {
                return false;
            }
        }