📜  如何在Java获取和设置默认字符编码或字符集?

📅  最后修改于: 2022-05-13 01:54:35.097000             🧑  作者: Mango

如何在Java获取和设置默认字符编码或字符集?

默认字符编码字符集或在Java中,使用由Java虚拟机(JVM)转换字节转换成字符的在不存在的file.encoding Java系统属性的字符串。在 JVM 启动期间, Java通过调用System.getProperty(“file.encoding”,”UTF-8″)获取字符编码。在没有file.encoding属性的情况下, Java默认使用“UTF-8”字符编码。

字符编码基本上将字节序列解释为特定字符的字符串。字节的相同组合可表示在不同的字符编码不同的字符。因此,该 正确的字符编码规范起着重要的作用。 Java的缓存字符编码在大多数其主要的类需要字符编码。因此,在使用 InputStreamReader 和其他Java包时调用 System.setProperty(“file.encoding”, “UTF-16”) 可能不会达到预期效果。

获取默认字符编码或字符集

在Java有多种检索默认字符集的方法,如下所示:

  • 使用“file.encoding”系统属性
  • 使用 Java.nio.Charset
  • 使用Charset.defaultCharset()方法

方法:



  1. “file.encoding”系统属性
  2. Java.nio.Charset
  3. 代码 InputStreamReader.getEncoding()

现在让我们先简单介绍一下它们,然后再在实现部分调用它们以获得默认字符编码或 Charset

方法一: “file.encoding”系统属性

Java的System.getProperty(“file.encoding”) 返回应用程序中使用的默认字符集,以防 JVM 以 -Dfile.encoding 属性启动或 JavaScript 未显式调用 System.setProperty(“ file.encoding, encoding) 方法,其中指定了编码类型。

方法二: Java.nio.Charset

Java包提供了一种静态方法来检索用于在字节和 Unicode字符之间进行转换的默认字符编码。 Charset.defaultCharset()方法返回正在使用的默认字符集。

方法三:代码 InputStreamReader.getEncoding()

Java的InputStreamReader 包使用方法getEncoding()返回此流使用的字符编码的名称。

例子:



Java
// Java Program to Get and Set
// Default Character encoding or Charset
 
// Importing input output classes
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
// Importing Charset class that defines charsets and
// translation between bytes and Unicode characters.
import java.nio.charset.Charset;
 
// Class 1
// Helper Class for character encoding
public class GFG {
 
    // Method
    // To
    public static String getCharacterEncoding()
    {
 
        // Creating an array of byte type chars and
        // passing random  alphabet as an argument.abstract
        // Say alphabet be 'w'
        byte[] byte_array = { 'w' };
 
        // Creating an object of InputStream
        InputStream instream
            = new ByteArrayInputStream(byte_array);
 
        // Now, opening new file input stream reader
        InputStreamReader streamreader
            = new InputStreamReader(instream);
        String defaultCharset = streamreader.getEncoding();
 
        // Returning default character encoding
        return defaultCharset;
    }
 
    // Main driver method
    public static void main(String args[])
        throws FileNotFoundException,
               UnsupportedEncodingException, IOException
    {
 
        // Method returns a string of character encoding
        // used by using System.getProperty()
        String defaultencoding
            = System.getProperty("file.encoding");
 
        System.out.println("Default Charset: "
                           + defaultencoding);
 
        // Getting character encoding by InputStreamReader
        System.out.println(
            "Default Charset by InputStreamReader: "
            + getCharacterEncoding());
 
        // Getting character encoding by java.nio.charset
        System.out.println("Default Charset: "
                           + Charset.defaultCharset());
    }
}


Java
// Java Program to Get and Set
// Default Character encoding or Charset
 
// Importing all input output classes
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
// Importing Charset class that defines charsets and
// translation between bytes and Unicode characters
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
 
// Class
// Class to encode characters
public class GFG {
 
    // Method 1
    // To encode the characters
    public static String getCharacterEncoding()
    {
 
        // Creating and initializing byte array
        // with some random character say it be N
 
        // Here N = w
        byte[] byte_array = { 'w' };
 
        // Creating an object of inputStream
        InputStream instream
            = new ByteArrayInputStream(byte_array);
 
        // Now, opening new file input stream reader
        InputStreamReader streamreader
            = new InputStreamReader(instream);
 
        String defaultCharset = streamreader.getEncoding();
 
        // Returning the default character encoded
        // Here it is for N = 'w'
        return defaultCharset;
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
        throws FileNotFoundException,
               UnsupportedEncodingException, IOException
    {
 
        // Setting the file encoding explicitly
        // to a new value
        System.setProperty("file.encoding", "UTF-16");
 
        // Returns a string of character encoding
        // using the getProperty() method
        String defaultencoding
            = System.getProperty("file.encoding");
 
        // Return the above string of character encoded
        System.out.println("Default Charset: "
                           + defaultencoding);
 
        // Getting character encoding by InputStreamReader
        // using the getCharacterEncoding() method
        System.out.println(
            "Default Charset by InputStreamReader: "
            + getCharacterEncoding());
 
        // Getting character encoding by java.nio.charset
        // using the default charset() method
        System.out.println("Default Charset: "
                           + Charset.defaultCharset());
    }
}


输出:

设置默认字符编码或字符集

方法:在Java有多种指定默认字符集值的方法。

  • 使用Java系统属性
  • 使用 JAVA_TOOLS_OPTIONS



现在让我们先简单介绍一下它们,然后再在实现部分调用它们以获得默认字符编码或 Charset

方法 1:使用Java系统属性“file.encoding”

在启动Java虚拟机时,通过提供 file.encoding 系统属性

java -Dfile.encoding="UTF-8"  HelloWorld, we can specify UTF-8 charset.

方法二:指定环境变量“JAVA_TOOLS_OPTIONS”。

如果我们使用某些脚本和工具启动 JVM,则可以使用环境变量 JAVA_TOOL_OPTIONS 将默认字符集设置为-Dfile.encoding = ”UTF-16”或任何其他字符集,然后每当 JVM 启动时程序就会使用该字符集机器。作为该方法的输出, 控制台显示如下:

以下代码段指示使用 JAVA_TOOLS_OPTIONS 设置默认字符编码:

test@system:~/java java HelloWorld
þÿExecuting HelloWorld
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF16

例子:

Java

// Java Program to Get and Set
// Default Character encoding or Charset
 
// Importing all input output classes
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
// Importing Charset class that defines charsets and
// translation between bytes and Unicode characters
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
 
// Class
// Class to encode characters
public class GFG {
 
    // Method 1
    // To encode the characters
    public static String getCharacterEncoding()
    {
 
        // Creating and initializing byte array
        // with some random character say it be N
 
        // Here N = w
        byte[] byte_array = { 'w' };
 
        // Creating an object of inputStream
        InputStream instream
            = new ByteArrayInputStream(byte_array);
 
        // Now, opening new file input stream reader
        InputStreamReader streamreader
            = new InputStreamReader(instream);
 
        String defaultCharset = streamreader.getEncoding();
 
        // Returning the default character encoded
        // Here it is for N = 'w'
        return defaultCharset;
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
        throws FileNotFoundException,
               UnsupportedEncodingException, IOException
    {
 
        // Setting the file encoding explicitly
        // to a new value
        System.setProperty("file.encoding", "UTF-16");
 
        // Returns a string of character encoding
        // using the getProperty() method
        String defaultencoding
            = System.getProperty("file.encoding");
 
        // Return the above string of character encoded
        System.out.println("Default Charset: "
                           + defaultencoding);
 
        // Getting character encoding by InputStreamReader
        // using the getCharacterEncoding() method
        System.out.println(
            "Default Charset by InputStreamReader: "
            + getCharacterEncoding());
 
        // Getting character encoding by java.nio.charset
        // using the default charset() method
        System.out.println("Default Charset: "
                           + Charset.defaultCharset());
    }
}

输出

Default Charset: UTF-16
Default Charset by InputStreamReader: UTF8
Default Charset: UTF-8