zoukankan      html  css  js  c++  java
  • 今天使用Bufferedrader ,单缓冲区的数据读取时出现的中文乱码的问题

    1.使用这种方式可能出现的中文乱码代码:

    /**
             * 第三套********:使用BufferReader,单缓冲区的数据读取和写入(字符输入流)
             * 读取
             */
            FileReader frFileReader = null;
            BufferedReader bWriter = null;
            try {
                frFileReader = new FileReader("D:NIO.txt");
                bWriter = new BufferedReader(frFileReader);
                //读取一行数据
                String Line = bWriter.readLine();
                while(Line!=null){
                    System.out.println(Line);
                    Line=bWriter.readLine();
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    bWriter.close();
                    frFileReader.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }

    2.实际出现中文乱码的代码:

    BufferedReader br=null;
    FileReader fr=null;
                    try {
                        
                        //在这里我遇到了中文问号乱码的问题
                         fr = new FileReader(upPath);     
                         br = new BufferedReader(fr);
                        String line;
                        String fictionContent = new String();
                        while((line=bufr.readLine())!=null){
                            //将读取到一行一行数据使用concat方法连接起来
                            fictionContent = fictionContent.concat(line+"
    ");
            
                        }
                        //测试:System.out.println(fictionContent);这里我做了测试后的代码为乱码
                        //作为传递小说内容的载体赋值media
                        media.setTxtContents(fictionContent);
                        //标记为上传
                        media.setIdentity(Identity.UPLOAD);
                        media.setFiction(upFiction);
                        oos.writeObject(media);
                        media = (Media) ois.readObject();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

    3.解决方案

    BufferedReader br=null;
                    try {
                        //通过文件路径读取到文件内容,然后传给服务器
                        InputStream in = new FileInputStream(upPath);
                        //将字节流向字符流的转换,utf-8格式也不行
                        InputStreamReader isr = new InputStreamReader(in,"gbk");
                        //创建字符流缓冲区
                        BufferedReader bufr = new BufferedReader(isr);//缓冲 
                        //在这里我遇到了中文问号乱码的问题
                         //fr = new FileReader(upPath);     
                         //br = new BufferedReader(fr);
                        String line;
                        String fictionContent = new String();
                        while((line=bufr.readLine())!=null){
                            //将读取到一行一行数据使用concat方法连接起来
                            fictionContent = fictionContent.concat(line+"
    ");
            
                        }
                        //测试:System.out.println(fictionContent);没有乱码!
                        //作为传递小说内容的载体赋值media
                        media.setTxtContents(fictionContent);
                        //标记为上传
                        media.setIdentity(Identity.UPLOAD);
                        media.setFiction(upFiction);
                        oos.writeObject(media);
                        media = (Media) ois.readObject();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
  • 相关阅读:
    参数方法HttpServletRequest之getParameter
    nullnull单例的实现方式之GCD方法
    阻带窗函数[数字信号处理]使用窗函数设计FIR滤波器
    nullnullhdu4450Draw Something
    第三方设置XML解析:第三方库GDataXMLNode的使用
    class左边nbu 2414 Please help the poor donkey!
    数据可视化可视化营养含量
    速度坐标hdu4445Crazy Tank
    singletonclassDesign Patterns: Singleton Basics 设计模式:单例基础
    控制台权限MMC不能打开文件 SQL Server Enterprise Manager.MSC 或权限不够 (解决方法 )
  • 原文地址:https://www.cnblogs.com/java-123/p/9163080.html
Copyright © 2011-2022 走看看