开心一笑: 会买水果的狗狗

    关注微信公众号

    QQ群:831045818

    app下载

    当前位置:首页> java > 技术文档 > 正文
    读不同编码txt 解决乱码问题 并附读写操作
    发布时间:2020-06-29 11:05:52.0 浏览次数:
    //写
        public static String writeTxt(String path,String txt)  {
            try{
                File f = new File(path);
                if (!f.exists()){
                    f.createNewFile();
                }
                //定义编码   
                OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
                BufferedWriter writer = new BufferedWriter(write);
                writer.write(txt);
                writer.close();
            } catch (Exception e) {
                System.out.println("写文件内bai容操作出错");
                e.printStackTrace();
            }
    
    
    
            FileWriter writer;
            try {
                writer = new FileWriter(path);
                writer.write("");
                writer.write(txt);
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
       //指定原文编码 
        public static void txtWriter(String path,String txt,){
    		FileWriter fw = null;
    		try {			
    			//如果文件存在,则追加内容;如果文件不存在,则创建文件
    			File f=new File(path);			
    			if(!f.exists()){
    				 //f.createNewFile();//如果文件不存在,则创建文件
    				 fw = new FileWriter(path);
    			        fw.flush();
    			 }						
    			fw.close();							
    			FileOutputStream fos=new FileOutputStream(path,true);
    			//写入时指定原来的编码格式
    			OutputStreamWriter osw=new OutputStreamWriter(fos,getFilecharset(new File(path)));
    			PrintWriter pw = new PrintWriter(osw);			
    			pw.append(txt);				
    									
    			pw.flush(); // 清空缓冲区的数据流
    			//pw.println("copy "+fileNames[i]+" c:\\u\\201506");
    			pw.close();
    			osw.close();
    			fos.close();			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
        //读
        public static String read(File file){
            String encoding=getFilecharset(file);
            StringBuilder result = new StringBuilder();
            try{
                InputStreamReader read = new InputStreamReader (new FileInputStream(file),encoding);
                BufferedReader br = new BufferedReader(read);
                String s = null;
                
                while((s = br.readLine())!=null){//使用readLine方法,一次读一行
                    result.append(s+"\r\n");//换行               
                }
                br.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            System.out.println(result);
            return result.toString();
        }
    
        //编码识别
        public static String getFilecharset(File sourceFile) {
            String charset = "GBK";
            byte[] first3Bytes = new byte[3];
            try {
                boolean checked = false;
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
                bis.mark(0);
                int read = bis.read(first3Bytes, 0, 3);
                if (read == -1) {
                    return charset;
                } else if (first3Bytes[0] == (byte) 0xFF
                        && first3Bytes[1] == (byte) 0xFE) {
                    charset = "UTF-16LE";
                    checked = true;
                } else if (first3Bytes[0] == (byte) 0xFE
                        && first3Bytes[1] == (byte) 0xFF) {
                    charset = "UTF-16BE";
                    checked = true;
                } else if (first3Bytes[0] == (byte) 0xEF
                        && first3Bytes[1] == (byte) 0xBB
                        && first3Bytes[2] == (byte) 0xBF) {
                    charset = "UTF-8";
                    checked = true;
                }
                bis.reset();
                if (!checked) {
                    int loc = 0;
                    while ((read = bis.read()) != -1) {
                        loc++;
                        if (read >= 0xF0){
                            break;}
                        if (0x80 <= read && read <= 0xBF)
                        {break;}
                        if (0xC0 <= read && read <= 0xDF) {
                            read = bis.read();
                            if (0x80 <= read && read <= 0xBF)
                            {continue;}
                            else
                            {break;}
                        } else if (0xE0 <= read && read <= 0xEF) {
                            read = bis.read();
                            if (0x80 <= read && read <= 0xBF) {
                                read = bis.read();
                                if (0x80 <= read && read <= 0xBF) {
                                    charset = "UTF-8";
                                    {break;}
                                } else
                                {break;}
                            } else
                            { break;}
                        }
                    }
                }
                bis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return charset;
        }
        
        //InputStream  流输出内容
        
         //<1>创建字节数组输出流,用来输出读取到的内容
               ByteArrayOutputStream baos = new ByteArrayOutputStream();
               //<2>创建缓存大小
               byte[] buffer = new byte[1024]; // 1KB
               //每次读取到内容的长度
               int len = -1;
               //<3>开始读取输入流中的内容
               while ((len = in.read(buffer)) != -1) { //当等于-1说明没有数据可以读取了
                   baos.write(buffer, 0, len);   //把读取到的内容写到输出流中
               }
               //<4> 把字节数组转换为字符串
               String content = baos.toString();
               //<5>关闭输入流和输出流
               in.close();
               baos.close();
               //<6>返回字符串结果


    关注"都市百货" 了解南陵

    微信咨询wanglf2r(不拉群 发广告者勿加)

    0
    0
    上一篇:南陵7月3日停电通知 上一篇:南陵博文中学盛川冰、吴俊、毛狄获得优秀班主任称号

    评论已有0

    提交评论

    热门评论

    南陵新闻
    公示公告
    常用查询
    风光南陵
    走出南陵
    友情链接