简体   繁体   English

FileWritter 将整数写入为非法字符

[英]FileWritter write integers as illegal characters

i'm currently writting an algorithm who write another algorithm.我目前正在编写一个编写另一种算法的算法。 For this i'm using the FileWritter class and this worked fine until i tried to write integers in the file.为此,我正在使用 FileWritter class 并且在我尝试在文件中写入整数之前它工作得很好。

fileWriter.write(1); print `` (edit: you can't see the character in here) that intelliJ interpret as: Illegal Character U+0001 (every number has a different name of illegal character) print ``(编辑:你看不到这里的字符)intelliJ解释为: Illegal Character U+0001 (每个数字都有不同的非法字符名称)

When i'm doing fileWriter.write("1") this work fine.当我在做fileWriter.write("1")这个工作正常。

Does the problem come from Intellij or do i have to convert every integers to String?问题是来自 Intellij 还是我必须将每个整数转换为字符串?

Basically Writer.write(int) doesn't do what you expect it to.基本上Writer.write(int)不会做你期望的事情。 The documentation says: 文档说:

Writes a single character.写入单个字符。 The character to be written is contained in the 16 low-order bits of the given integer value;要写入的字符包含在给定 integer 值的低 16 位中; the 16 high-order bits are ignored. 16 个高位被忽略。

So you're writing U+0001, just as IntelliJ is saying.因此,正如 IntelliJ 所说,您正在编写 U+0001。

It's not the greatest API in the world (it would be better to accept char for a single character) but it is behaving as designed.它不是世界上最伟大的 API(最好接受单个字符的char ),但它行为符合设计。

Basically, yes, you need to convert everything to text.基本上,是的,您需要将所有内容都转换为文本。

The FileWriter.write(int) method, which is inherited from OutputStreamWriter.write(int) , writes a single character to a file.OutputStreamWriter.write(int)继承的FileWriter.write(int)方法将单个字符写入文件。 The input int is the ASCII or Unicode representation of the character.输入int是字符的 ASCII 或 Unicode 表示。 For instance, writing fileWriter.write(65) writes the letter A , as A 's ASCII value is 65. This could also be written as fileWriter.write('A') , as the character A is represented in memory as the number 65 .例如,写fileWriter.write(65)写入字母A ,因为A的 ASCII 值为 65。这也可以写为fileWriter.write('A') ,因为字符A在 memory 中表示为数字65 . If you want to write an integer to a file, you will need to call fileWriter.write(Integer.toString(i)) , where i is the value you want to write.如果要将 integer 写入文件,则需要调用fileWriter.write(Integer.toString(i)) ,其中i是您要写入的值。 Alternatively, if i is a single-digit positive number, you can write fileWriter.write('0' + i) , as adding '0' to such a digit converts it into its corresponding character.或者,如果i是一位数的正数,您可以编写fileWriter.write('0' + i) ,因为在这样的数字上添加'0'会将其转换为相应的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM