Where to get "UTF-8" string literal in Java?
I'm trying to use a constant instead of a string literal in this piece of code:
new InputStreamReader(new FileInputStream(file), "UTF-8")
"UTF-8"
코드에 자주 나타나기 때문에 몇 가지 참조하는 것이 좋습니다.static final
대신 변수를 지정합니다.JDK에서 그런 변수를 어디서 찾을 수 있는지 아세요?
BTW, on a second thought, such constants are bad design: Public Static Literals ... Are Not a Solution for Data Duplication
Java 1.7+에서는 java.nio.charset.Standard Charsets는 다음에 대한 상수를 정의합니다.Charset
포함하여UTF_8
.
import java.nio.charset.StandardCharsets;
...
StandardCharsets.UTF_8.name();
For Android: minSdk 19
지금은 사용하고 있습니다.org.apache.commons.lang3.CharEncoding.UTF_8
상수로 되어 있다
Google Guava 라이브러리(Java에서 작업하는 경우)에는 다음과 같은 정적 필드가 있는 클래스가 있습니다.Charsets.UTF_8
,Charsets.UTF_16
,기타.
Java 7을 사용하시면 됩니다.java.nio.charset.StandardCharsets
대신 비교 가능한 상수의 경우.
이 상수는 문자열이 아니라 실제입니다.Charset
인스턴스.charset 이름을 사용하는 모든 표준 API에는 오버로드가 있으며Charset
대신 사용해야 하는 객체입니다.
In case this page comes up in someones web search, as of Java 1.7 you can now use java.nio.charset.StandardCharsets to get access to constant definitions of standard charsets.
이 상수는 다음과 같이 사용할 수 있습니다.UTF-16
,US-ASCII
(등)을 클래스 내에서 사용합니다.org.apache.commons.codec.CharEncoding
뿐만 아니라.
There are none (at least in the standard Java library). Character sets vary from platform to platform so there isn't a standard list of them in Java.
There are some 3rd party libraries which contain these constants though. One of these is Guava (Google core libraries): http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/base/Charsets.html
사용할 수 있습니다.Charset.defaultCharset()
API 또는file.encoding
소유물.
But if you want your own constant, you'll need to define it yourself.
In Java 1.7+
'UTF-8' 문자열을 사용하지 않고 대신Charset
type 파라미터:
import java.nio.charset.StandardCharsets
...
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
If you are using OkHttp for Java/Android you can use the following constant:
import com.squareup.okhttp.internal.Util;
Util.UTF_8; // Charset
Util.UTF_8.name(); // String
Constant definitions for the standard. These charsets are guaranteed to be available on every implementation of the Java platform. since 1.7
package java.nio.charset;
Charset utf8 = StandardCharsets.UTF_8;
학급org.apache.commons.lang3.CharEncoding.UTF_8
Java 7 도입 후 권장되지 않습니다.java.nio.charset.StandardCharsets
- @see JRE character encoding names
- @ 2.1 이후
- @link java.nio.charset이 도입되었습니다.다음 상수를 정의하는 StandardCharsets}
- {@link Charset}개 개체입니다.이 클래스에서 제공되는 문자열 값을 가져오려면 {@link Charset#name()}을(를) 사용하십시오.
- 이 클래스는 향후 릴리즈에서 삭제될 예정입니다.
언급URL : https://stackoverflow.com/questions/6698354/where-to-get-utf-8-string-literal-in-java
'programing' 카테고리의 다른 글
Absolute 파일 경로를 포함하는 문자열에서 파일 이름을 가져오려면 어떻게 해야 합니까? (0) | 2022.08.16 |
---|---|
"number of bytes"를 0으로 설정하고 memcpy()와 memmove()를 호출할 수 있습니까? (0) | 2022.08.16 |
TypeScript의 자기 참조형 (0) | 2022.08.16 |
VueJS/Vuex 초기 상태 (0) | 2022.08.16 |
vue-cli로 생성된 기존 vuejs 앱을 대규모 프로젝트에 통합하는 방법 (0) | 2022.08.16 |