Last active
November 15, 2016 17:02
-
-
Save sadedv/7449958134e81b40ef8b to your computer and use it in GitHub Desktop.
Узнать текущую кодировку (charset), преобразовать в другую кодировку в Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Encodings | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
FileInputStream inputStream = new FileInputStream("d:/data.txt"); | |
FileOutputStream outputStream = new FileOutputStream("d:/data.txt"); | |
SortedMap<String, Charset> charsets = Charset.availableCharsets();//список доступных кодировок | |
Charset currentCharset = Charset.defaultCharset();//узнать текущую кодировку | |
String s = "Good news everyone!"; | |
byte[] buffer = s.getBytes("Windows-1251");//создать массив байт в любой известной Java кодировке | |
byte[] fileBuffer = new byte[1000]; | |
inputStream.read(fileBuffer); | |
String s1 = new String(fileBuffer, "Windows-1251");//преобразовать набор байт, прочитанных из файла в строку | |
//преобразовать набор байт из одной кодировки в другую | |
Charset koi8 = Charset.forName("KOI8-R"); | |
Charset windows1251 = Charset.forName("Windows-1251"); | |
byte[] buffer3 = new byte[1000]; | |
inputStream.read(buffer3); | |
String s3 = new String(buffer3, koi8); | |
buffer3 = s3.getBytes(windows1251); | |
outputStream.write(buffer3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment