我使用/output:D:\list.txt product get name,version 从 cmd 命令获得了应用程序列表。但是,当我尝试使用 java 检索列表时,输出的每个字母后都有空格。
样本:
来自文本文件
links
images
lists
在 java 中读取时
l i n k s
i m a g e s
l i s t s
有办法解决这个问题吗?
我刚刚使用了这段代码:
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
字符集
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-16"));
关于java - txt文件的读取输出有空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14411600/