java.io FileOutPutStream-字符之间有空格.为什么? [英] java.io FileOutPutStream - There are white spaces among chars. Why?

查看:270
本文介绍了java.io FileOutPutStream-字符之间有空格.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i want to write something with this code but after i run there are white spaces between characters. but in code i dont give space to string.

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 !

Output is (in text file) : H e l l o W o r l d !

推荐答案

使用

本质上,writeChars将每个字符写为2个字节.第二个是多余的空格.

Essentially, writeChars will write every character as 2 bytes. The second one you are seeing as extra spaces.

这篇关于java.io FileOutPutStream-字符之间有空格.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆