关于io读取缓存会有中文部分乱码的问题

今天老板突然说,要给用户友评做个缓存,不至于一上来就请求数据库。
可是当断网的时候,缓存读取出现了中文乱码。不是一下子都是乱的,而是个别字是乱码。
后来网上看到用BufferedReader这个类可以不乱码,我就试了试。没想到还真不错,成功了!

public String getAsStringNew( String key )
{
    BufferedReader  buf     = null;
    StringBuffer    stringBuffer    = new StringBuffer();
    try {
        String line = null;
        buf = new BufferedReader( new InputStreamReader( get( key ) ) );
        while ( (line = buf.readLine() ) != null )
        {
            stringBuffer.append( line );
        }
    } catch ( IOException e ) {
        e.printStackTrace();
    } finally {
        try {
            buf.close();
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    return(stringBuffer.toString() );
}

还有一个方法就是使用解码。在缓存的时候转码,读取缓存的时候解码。

缓存时:

    public void put(String key, String value) {
        try {
            value = URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        DiskLruCache.Editor edit = null;
        BufferedWriter bw = null;
        try {
            edit = editor(key);
            if (edit == null) return;
            OutputStream os = edit.newOutputStream(0);
            bw = new BufferedWriter(new OutputStreamWriter(os));
            bw.write(value);
            edit.commit();//write CLEAN
        } catch (IOException e) {
            e.printStackTrace();
            try {
                //s
                edit.abort();//write REMOVE
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

读取缓存时:

 public String getAsString(String key) {
        InputStream inputStream = null;
        try {
            //write READ
            inputStream = get(key);
            if (inputStream == null) return null;
            StringBuilder sb = new StringBuilder();
            int len = 0;
            byte[] buf = new byte[128];
            while ((len = inputStream.read(buf)) != -1) {
                sb.append(new String(buf, 0, len));
            }
            return URLDecoder.decode(sb.toString(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            if (inputStream != null)
                try {
                    inputStream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
        }
        return null;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 主要内容 1. 字符编码理论简述 本文主要是围绕Web开发中涉及到的中文编码这一常见问题展开,包括了对字符编码基础...
    topgunviper阅读 13,582评论 5 28
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • tags:io categories:总结 date: 2017-03-28 22:49:50 不仅仅在JAVA领...
    行径行阅读 2,204评论 0 3
  • 昨天晚上儿子完成了日常功课后,按照要求开始看课外读物。他拿起了妈妈给买的《米小圈》,异常有趣的读了起来。念完了第一...
    dexj阅读 384评论 1 1
  • 我写这篇文章的时候,是在10月22日00:46分,也就是我最好朋友婚礼过后的3小时多。本人跟随着老好参加好几个婚礼...
    Yoki_yang阅读 825评论 1 2