首页 \ 问答 \ Java getBytes UTF-8编码(Java getBytes UTF-8 encoding)

Java getBytes UTF-8编码(Java getBytes UTF-8 encoding)

我正在尝试处理编码问题(我想将字符串中的特殊字符转换为正确的UTF-8字符...):

当我执行这个简单的代码时:

System.out.println(new String("é".getBytes("UTF-8"), "UTF-8"));

在控制台我期待:'é'但是我得到了

é 

I am trying to deal with an encoding problem (I want to transform the special characters from a string into correct UTF-8 characters...):

When I execute this simple code:

System.out.println(new String("é".getBytes("UTF-8"), "UTF-8"));

In the console I expect: 'é' but I get

é 

原文:https://stackoverflow.com/questions/27828600
更新时间:2022-05-05 11:05

最满意答案

您提供的代码中存在一些误解:

byte[] bytearray = new byte[size];
foreach (byte bit in bytearray) 
{
     bit = 0;
}

您似乎认为您正在初始化数组bytearray每个字节为零。 相反,您只需将循环变量bit (不幸命名)设置为零size 。 实际上这个代码甚至不会编译,因为你不能分配给foreach迭代变量。

此外,您首先不需要初始化:字节数组元素自动初始化为0。

string tobewritten = string.Empty;
foreach (byte bit in bytearray) 
{
     tobewritten += bit.ToString();
}

您希望将数组中每个字节的字符串表示形式组合到要写入的字符串变量中。 由于字符串是不可变的,因此为每个必须进行垃圾收集的元素创建一个字符串以及为bit创建的字符串,这相对较贵,尤其是当您创建其中一个Stringbuilder - 请改用Stringbuilder

最后,根本不需要所有这些 - 似乎你只想在文本文件中写一堆“0”字符 - 如果你不担心创建一个大的零字符串(它取决于值的size是否有意义)您可以直接创建字符串来执行此操作 - 或者将一个较小的字符串直接写入流中。

using (var file = File.CreateText(newpath))
{
    file.WriteLine(new string('0', size));
}

There are several misunderstandings in the code you provided:

byte[] bytearray = new byte[size];
foreach (byte bit in bytearray) 
{
     bit = 0;
}

You seem to think that your are initializing each byte in your array bytearray with zero. Instead you just set the loop variable bit (unfortunate naming) to zero size times. Actually this code wouldn't even compile since you cannot assign to the foreach iteration variable.

Also you didn't need initialization here in the first place: byte array elements are automatically initialized to 0.

string tobewritten = string.Empty;
foreach (byte bit in bytearray) 
{
     tobewritten += bit.ToString();
}

You want to combine the string representation of each byte in your array to the string variable tobewritten. Since strings are immutable you create a new string for each element that has to be garbage collected along with the string you created for bit, this is relatively expensive, especially when you create 2048000 one of them - use a Stringbuilder instead.

Lastly all of that is not needed at all anyway - it seems you just want to write a bunch of "0" characters to a text file - if you are not worried about creating a single large string of zeros (it depends on the value of size whether this makes sense) you can just create the string directly to do this one go - or alternatively write a smaller string directly to the stream a bunch of times.

using (var file = File.CreateText(newpath))
{
    file.WriteLine(new string('0', size));
}

相关问答

更多
  • 也许尝试java.nio.file中的功能,例如: Path temp = Paths.get(pathString+"/TileRecordings/"+jTextField1.getText()+".txt"); Files.write(temp, contentToSave.toString().getBytes()); Maybe try features from java.nio.file for example: Path temp = Paths.get(pathString+"/Tile ...
  • 您提供的代码中存在一些误解: byte[] bytearray = new byte[size]; foreach (byte bit in bytearray) { bit = 0; } 您似乎认为您正在初始化数组bytearray每个字节为零。 相反,您只需将循环变量bit (不幸命名)设置为零size 。 实际上这个代码甚至不会编译,因为你不能分配给foreach迭代变量。 此外,您首先不需要初始化:字节数组元素自动初始化为0。 string tobewritten = string.E ...
  • 您可以定义一个类来保存每个人的数据: class Person { public: std::string name; std::string study_path; std::string semester; unsigned int age; }; 然后,您可以为该类定义流提取运算符: std::istream & operator>>(std::istream & stream, Person & person) { stream >> person.name ...
  • 检查你在哪写这个目录。 如果可以,请使用绝对路径写入文件以及错误。 check the directory in which where you are writing this. Use an absolute path to write the file as well as errors if you can.
  • 也许是这样的 StringBuilder s = new StringBuilder(); while(kb.hasNextLine()){ s.append(kb.nextLine()) } text = s.toString(); 或者也许在while循环中构建数组..使用kb.hasNext(pattern) ----编辑您可以使用./java filename
  • 查看文件的前20行,很明显编码使用字节0x8C作为某些多字节序列的第一个字节。 具有此属性的编码为: BIG5 BIG5-HKSCS CP932 CP936 CP949 CP950 GB18030 GBK 裘哈 SHIFT_JIS Shift_JISX0213 依次尝试: $ for encoding in BIG5 BIG5-HKSCS CP932 CP936 CP949 CP950 GB18030 GBK \ JOHAB Shift_JIS Shift_JISX021 ...
  • 在浏览器中仅使用jQuery / javascript,以及通常配置的Web服务器,这是不可能的。 服务器的文件系统不会以可直接从客户端写入的方式公开,这将是一个相当大的安全风险。 为了实现这一点,您需要使用一些服务器端代码(例如PHP)来帮助在服务器上写入文件。 此时,您可以将所需内容和文件名称作为请求发送到服务器端代码,然后该代码可以将其写入服务器文件系统上的所需位置。 确保采用适当的保护措施,以便您指定的某些用户只能写入某些(安全)文件,否则您可以打开前面提到的安全漏洞。 Using only jQu ...
  • 尝试这个: @echo off setlocal enableDelayedExpansion :: you can set full file paths to the resource files set "source=source.txt" set "names=names.txt" set "final=final.txt" break>"%final%" for /f "usebackq delims=" %%a in ("%names%") do ( set counter=0 ...
  • 你当然可以这样做。 但再想一想。 用户不从Eclipse运行程序。 用户通常运行在jar中打包的程序,因此所有类文件都不在文件系统中,而是在jar中。 此外,除特殊文件夹外,用户甚至可能无权写入文件系统。 底线:您要创建在文件系统中存储一些运行时数据的应用程序,您应该是用户用户主目录或临时目录,或者使用java.util.prefs.Preferences ,它们提供与平台无关的方式来使用文件系统保存和检索数据在unix和windows中的注册表中。 如果您选择自己创建文件,则可以使用系统属性user.ho ...
  • Boolean writeHeader = (!File.Exists(fileName)); using (StreamWriter file = new StreamWriter(fileName, true)) { if (writeHeader) { file.WriteLine(headerLine); } file.WriteLine(line); } Boolean writeHeader = (!File.Exists(fileName)) ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)