Encode String in UTF-8 in Java | Delft Stack

文章推薦指數: 80 %
投票人數:10人

Encode String in UTF-8 in Java · Encode a String to UTF-8 by Converting It to Bytes Array and Using new String() · Encode a String to UTF-8 Using ... JavaHowtosPerformStringtoStringArrayConversioninJavaRemoveSubstringFromStringinJavaSortObjectsinArrayListbyDateinJavaConcatenateTwoArraysinJavaConvertByteArrayinHexStringinJavaConvertJavaStringIntoByteAddOneDaytoaDateinJavaCompareTwoDatesinJavareportthisad reportthisadHowToJavaHowtosEncodeStringinUTF-8inJavaEncodeaStringtoUTF-8byConvertingIttoBytesArrayandUsingnewString()EncodeaStringtoUTF-8UsingStandardCharsets.UTF_8.encodeandStandardCharsets.UTF_8.decode(byteBuffer)EncodeStringsFromaFiletoUTF-8UsingFiles.readString()WeneedtousetheconceptofencodinganddecodingwhenweworkwithStrings,andwewanttoconvertthatstringtoanothercharacterset.UTF-8,whichisshortforUnicodeTransformationFormat-8bit,isavariable-widthstandardthatassignsadifferentnumberofbytesfromonetofourtoeverycodepointorcharacter.Belowwecheckouthowtoencodeastringandafile’scontentstoUTF-8standard.EncodeaStringtoUTF-8byConvertingIttoBytesArrayandUsingnewString()WefirstconvertthestringtoanarrayofbytesinthefirstmethodandcreateastringwiththeUTF-8encoding.WecreateastringjapaneseStringthatcontainsJapanesecharacters.Next,weconvertthestringtoabytearraybecausewecannotencodeastringdirectlytoUTF-8.japaneseString.getBytes()returnsanarrayofbytetype.NowwecreateanewStringusingnewString()andpassintwoarguments,thefirstargumentisthebytearrayjapaneseBytesArray,andthesecondargumentistheencodingformatthatwewanttouse.WeusetheStandardCharsetsclasstogettheencodingcharsetandaccesstheUTH_8field.TheencodedStringcontainsastringthatisencodedwithUTF-8.importjava.nio.charset.StandardCharsets; publicclassJavaExample{ publicstaticvoidmain(String[]args){ StringjapaneseString="これはテキストです"; byte[]japaneseBytesArray=japaneseString.getBytes(); StringencodedString=newString(japaneseBytesArray,StandardCharsets.UTF_8); System.out.println(encodedString); } } Output:これはテキストです EncodeaStringtoUTF-8UsingStandardCharsets.UTF_8.encodeandStandardCharsets.UTF_8.decode(byteBuffer)WecanusetheStandardCharsetsclasstoencodeastringtospecifiedcharsetlikeUTF-8.WecreateajapaneseStringandthencallencode()ofStandardCharsets.UTF_8thatisoftypecharsets.Intheencode()method,wepassthejapaneseString,returningaByteBufferobject.ThestringiscurrentlyintheformofaByteBuffer,sowecallthedecode()methodofStandardCharsets.UTF_8thattakestheByteBufferobjectasanargument,andatlast,weconverttheresulttoastringusingtoString().importjava.nio.ByteBuffer; importjava.nio.charset.StandardCharsets; publicclassJavaExample{ publicstaticvoidmain(String[]args){ StringjapaneseString="これはテキストです"; ByteBufferbyteBuffer=StandardCharsets.UTF_8.encode(japaneseString); StringencodedString=StandardCharsets.UTF_8.decode(byteBuffer).toString(); System.out.println(encodedString); } } Output:これはテキストです EncodeStringsFromaFiletoUTF-8UsingFiles.readString()IntheLastexample,insteadofencodingasinglestringtoUTF-8format,wereadafileandencodeallthestringsinthefile.First,wecreateatextfileandaddsometexttoencodeintheUTF-8standard.Togetthefile’spath,weusePaths.get()andpassinthefile’spathasanargumentthatreturnsaPathobject.WecallthereadString()methodoftheFilesclassthattakestwoarguments,thefirstargumentisthePathobject,andthesecondargumentisthecharsettousethatweaccessusingStandardCharsets.UTF_8.WegettheencodedstringreadStringandprintitintheoutput.importjava.io.IOException; importjava.nio.charset.StandardCharsets; importjava.nio.file.Files; importjava.nio.file.Path; importjava.nio.file.Paths; publicclassJavaExample{ publicstaticvoidmain(String[]args){ try{ Pathpath=Paths.get("C:\\Users\\User1\\IdeaProjects\\JavaExamples\\src\\main\\java\\example_file.txt"); StringreadString=Files.readString(path,StandardCharsets.UTF_8); System.out.println(readString); }catch(IOExceptione){ e.printStackTrace(); } } } Output:これはテキストです Tämäontekstiä RelatedArticle-JavaStringPerformStringtoStringArrayConversioninJavaRemoveSubstringFromStringinJavaConvertByteArrayinHexStringinJavaConvertJavaStringIntoByteCheckifStringContainsNumbersinJavaOperatorOverloadinginJavax



請為這篇文章評分?