读取txt文件的输出有空格 [英] read output of txt file has white spaces

查看:162
本文介绍了读取txt文件的输出有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用/output:D:\list.txt产品获取名称,版本从cmd命令获取应用程序列表。但是,当我尝试使用java检索列表时,输出在每个字母后面都有空格。

i got the list of applications from cmd command using /output:D:\list.txt product get name,version. However when i try to retrieve the list using java the output has white spaces after each letter.

SAMPLE:

来自文本文件

links

images

lists

在java中读取

 l i n k s

 i m a g e s 

 l i s t s

有没有办法解决这个问题?

is there a way to fix this problem?

我刚用过这段代码:

public void myreader() throws IOException {
 Path path = Paths.get("D:\\list.txt");
 Charset charset = Charset.forName("ISO-8859-1");
 try (BufferedReader reader = Files.newBufferedReader(path,charset)) {
      String line = null;
      while ((line = reader.readLine()) != null) {
           System.out.println(line);
      }
 }


推荐答案

这可能是由于编码问题。尝试使用 UTF-16 字符集

This can be due to the encoding problem. Try using UTF-16 character set

BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-16"));

这篇关于读取txt文件的输出有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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