gpt4 book ai didi

java.io FileOutPutStream - 字符之间有空格。为什么?

转载 作者:行者123 更新时间:2023-11-29 07:11:22 35 4
gpt4 key购买 nike

我想用这段代码写一些东西,但在我运行之后,字符之间有空格。但在代码中我没有给字符串留空间。

import java.io.*;

public class WriteText{

public static void main(String[] args) {

FileOutputStream fos;
DataOutputStream dos;

try {

File file= new File("C:\\JavaWorks\\gui\\bin\\hakki\\out.txt");
fos = new FileOutputStream(file);
dos=new DataOutputStream(fos);

dos.writeChars("Hello World!");
}

catch (IOException e) {
e.printStackTrace();
}

}

}

输出是(在文本文件中):H e l l o W o r l d !

最佳答案

使用writeBytes

dos.writeBytes("Hello World!");

本质上,writeChars 会将每个字符写入 2 个字节。第二个你看到的是额外的空间。

关于java.io FileOutPutStream - 字符之间有空格。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617353/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com