Java获取字符串编码格式实现思路

作者:java未来王者 时间:2023-08-14 12:35:38 

Java——获取字符串编码格式

判断一个字符串的编码格式: 

public static String getEncoding(String str) {
       String encode = "GB2312";
       try {
           if (isEncoding(str, encode)) { // 判断是不是GB2312
               return encode;
           }
       } catch (Exception exception) {
       }
       encode = "ISO-8859-1";
       try {
           if (isEncoding(str, encode)) { // 判断是不是ISO-8859-1
               return encode;
           }
       } catch (Exception exception1) {
       }
       encode = "UTF-8";
       try {
           if (isEncoding(str, encode)) { // 判断是不是UTF-8
               return encode;
           }
       } catch (Exception exception2) {
       }
       encode = "GBK";
       try {
           if (isEncoding(str, encode)) { // 判断是不是GBK
               return encode;
           }
       } catch (Exception exception3) {
       }
       return ""; // 如果都不是,说明输入的内容不属于常见的编码格式。
   }

public static boolean isEncoding(String str, String encode) {
       try {
           if (str.equals(new String(str.getBytes(), encode))) {
               return true;
           }
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }
       return false;
   }

Java获取一个文本文件的编码格式

文本文件是我们在windows平台下常用的一种文件格式,这种格式会随着操作系统的语言不同,而出现其默认的编码不同

那么如何使用程序获取“文本文件”的编码方式呢?

文件编码的格式决定了文件可存储的字符类型,所以得到文件的类型至关重要

下文笔者讲述获取一个文本文件的格式信息的方法分享,如下所示:

实现思路:

通过获取文件流的前3个字节
判断其值的方式,即可获取文本文件的编码方式

例:

package com.java265.other;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class Test {
   /*
    * java265.com 获取文本文件的编码方式
    *  
    **/

public static void main(String[] args)   {
     File file = new File("E://person/java265.com/java.txt");
     System.out.println(GetEncoding(file));
     }
   public static String GetEncoding(File file)
   {
       String charset = "GBK";
       byte[] first3Bytes = new byte[3];
       try {
           boolean checked = false;
           InputStream is = new FileInputStream(file);
           int read = is.read(first3Bytes, 0, 3);

if (read == -1)
               return charset;
           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;
           }else if (first3Bytes[0] == (byte) 0xA
                   && first3Bytes[1] == (byte) 0x5B
                   && first3Bytes[2] == (byte) 0x30) {
               charset = "UTF-8";
               checked = true;
           }else if (first3Bytes[0] == (byte) 0xD
                   && first3Bytes[1] == (byte) 0xA
                   && first3Bytes[2] == (byte) 0x5B) {
               charset = "GBK";
               checked = true;
           }else if (first3Bytes[0] == (byte) 0x5B
                   && first3Bytes[1] == (byte) 0x54
                   && first3Bytes[2] == (byte) 0x49) {
               charset = "windows-1251";
               checked = true;
           }
           //bis.reset();
           InputStream istmp = new FileInputStream(file);
           if (!checked) {
               int loc = 0;
               while ((read = istmp.read()) != -1) {
                   loc++;
                   if (read >= 0xF0)
                       break;
                   if (0x80 <= read && read <= 0xBF)
                       break;
                   if (0xC0 <= read && read <= 0xDF) {
                       read = istmp.read();
                       if (0x80 <= read && read <= 0xBF)
                           continue;
                       else
                           break;
                   } else if (0xE0 <= read && read <= 0xEF) {
                       read = istmp.read();
                       if (0x80 <= read && read <= 0xBF) {
                           read = istmp.read();
                           if (0x80 <= read && read <= 0xBF) {
                               charset = "UTF-8";
                               break;
                           } else
                               break;
                       } else
                           break;
                   }
               }
           }
           is.close();
           istmp.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
       return charset;
   }
}

来源:https://www.cnblogs.com/javalove2022/archive/2022/09/21/16717560.html

标签:java,文件,字符串,编码格式
0
投稿

猜你喜欢

  • logback输出日志屏蔽quartz的debug等级日志方式

    2023-08-04 23:40:04
  • 如何解决Java多线程死锁问题

    2022-08-11 15:51:02
  • c# volatile 关键字的拾遗补漏

    2022-10-20 04:39:09
  • dubbo服务链路跟踪方式

    2023-08-24 09:54:21
  • Android CardView详解及使用方法和实例

    2023-01-11 01:04:30
  • java中struts2实现简单的文件上传与下载

    2022-12-23 22:53:21
  • java项目中的多线程实践记录

    2023-10-16 16:12:03
  • C#如何使用Task类解决线程的等待问题

    2023-01-04 18:23:22
  • Java ArrayList与LinkedList使用方法详解

    2023-08-12 08:23:46
  • 深入理解Android MD5数据加密

    2022-03-16 07:48:03
  • java web实现简单留言板功能

    2021-10-29 17:22:31
  • C#实现DataTable映射成Model的方法(附源码)

    2023-03-12 06:10:56
  • SpringCloud实现Redis在各个微服务的Session共享问题

    2022-07-14 06:33:19
  • Android开发中如何解决Fragment +Viewpager滑动页面重复加载的问题

    2023-12-24 20:21:25
  • java设计模式之工厂模式实例详解

    2023-11-24 23:35:12
  • 通过openOffice将office文件转成pdf

    2021-12-29 21:35:32
  • SpringMVC拦截器配置及运行流程解析

    2023-03-30 15:38:57
  • 详解Java利用深度优先遍历解决迷宫问题

    2022-08-20 02:46:54
  • Java并发LinkedBlockingQueue源码分析

    2022-09-01 08:35:30
  • C#中32位浮点数Float(Real)一步步按位Bit进行分析

    2023-07-19 16:01:20
  • asp之家 软件编程 m.aspxhome.com