首页 > 编程语言 >JAVA将文件另存为UTF8格式

JAVA将文件另存为UTF8格式

时间:2022-11-09 08:57:04浏览次数:41  
标签:JAVA String 另存为 read UTF8 bis new byte first3Bytes

/**
     * @description:  将文本文件另存为UTF8格式
     * @author: bug
     * @date: 2022/11/8 19:22
     * @param file
     * @param srcFilePath
     * @return java.lang.String
     */
    private String saveAsUtf8(MultipartFile file, String srcFilePath) throws Exception{
        InputStream inputStream = file.getInputStream();

        String fileName = UUIDUtil.uuid32() + ".txt";
        File tempFile = new File(srcFilePath);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        String srcFileName = tempFile.getPath() + File.separator + fileName;

        String inputFileEncode = this.getFileCharset(inputStream);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(file.getInputStream(), inputFileEncode));
        BufferedWriter bufferedWriter = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(srcFileName), "UTF8"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            bufferedWriter.write(line + "\n");
        }
        bufferedWriter.close();
        bufferedReader.close();
        return fileName;
    }

    /**
     * @description:  获取文件的编码
     * @author: bug
     * @date: 2022/11/8 19:19
     * @param fileInputStream
     * @return java.lang.String
     */
    private String getFileCharset(InputStream fileInputStream) {
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
        try {
            boolean checked = false;
            //BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
            BufferedInputStream bis = new BufferedInputStream(fileInputStream);
            bis.mark(0);
            int read = bis.read(first3Bytes, 0, 3);
            if (read == -1) {
                return charset; // 文件编码为 ANSI
            } else if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
                charset = "UTF-16LE"; // 文件编码为 Unicode
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
                charset = "UTF-16BE"; // 文件编码为 Unicode big endian
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
                    && first3Bytes[2] == (byte) 0xBF) {
                charset = "UTF-8"; // 文件编码为 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) // 单独出现BF以下的,也算是GBK
                        break;
                    if (0xC0 <= read && read <= 0xDF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
                            // (0x80
                            // - 0xBF),也可能在GB编码内
                            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) {
            log.info("获取文件编码异常");
        }
        return charset;
    }

  

标签:JAVA,String,另存为,read,UTF8,bis,new,byte,first3Bytes
From: https://www.cnblogs.com/guliang/p/16872383.html

相关文章

  • Java类与对象
    1:初学JAVA,都知道JAVA是面向对象的编程。笔者这节开始说说类和对象。(实例仅供参考,如若复制粘贴记得修改包名和类名,避免出错)学习JAVA的快捷键,Alt+/代码补全功能,其实此快捷键......
  • Java中的封装,继承,多态
    一,前言​今天总结一下关于Java的三大特性,封装,继承,多态。其实关于三大特性对于从事编程人员来说都是基本的了,毕竟只要接触Java这些都是先要认识的,接下来就系统总结一下。二......
  • JavaWeb期中考试-2021年版(二)
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@pageimport="S.s"%><%@pageimport="Dao.dao"%><%@pageimp......
  • JavaWeb期中考试-2021年版(四)
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@pageimport="S.s"%><%@pageimport="Dao.dao"%><%@pageim......
  • JavaWeb期中考试-2021年版(三)
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@pageimport="S.s"%><%@pageimport="Dao.dao"%><!DOCTYPE......
  • JavaWeb期中考试-2021年版(五)
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@pageimport="S.s"%><%@pageimport="java.util.ArrayLi......
  • JavaWeb期中考试-2021年版(六)
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@pageimport="S.s"%><%@pageimport="Dao.dao"%><%@pageimp......
  • JavaScript基础
    一、JavaScript基础(一)变量和数据类型1、JavaScript介绍​ 1)JavaScript是什么?​ JavaScript是运行在客户端(浏览器)的编程语言,实现人机交互。​ 2)作用​ 网页特效(监听......
  • JAVA 集合知识点整理
    对JAVA集合这部分知识还是应该多了解学习因为常用和常被面,出于两方面原因对其最近又重新学习了一下,整理如一下与大家分享一、java集合可以分为两大阵营Collection和Map......
  • 【Javaweb】implements Serializable是什么意思?反序列化是什么意思?
    为了保证数据传输的可靠性,常常要implementsSerializable,为什么?对象本质上是虚无缥缈的,只是内存中的一个地址,如果想要让对象持久化,让对象在网络上传输,总不可能传送一个内......