English 中文(简体)
Java 读取文本文件, 带有额外的空白和 CRLF
原标题:Java reads text file with extra whitespace and CRLF

我有一个由 PowerShell 使用命令生成的文本文件

获取儿童项目C: 源Path {ForEach-Object {$_.Name} & gt; “C: MyPlaceoutfile. txt”

This generates outfile.txt which opens like this Notepad++ Notepad++

当以爪哇语打开并用类似文字的行读取时 :

while((line = br.readLine()) != null) {
    line = line.replaceAll("\s", "");
    System.out.println(i + ":	" + line);
}

它生产了以下产品:

"https://i.sstatic.net/ipHgb.png" alt="贾瓦输出"/ >

我尝试过替换白空字符,但似乎没有这样做。有什么想法吗?

最佳回答

问题似乎是 Powershell 正在用一个 Unicode 编码发布一个文件, 但 Java 却把它看成普通的 ASCII 。 您需要更改 java 代码, 才能将文件读成 Unicode 。

问题回答

Out-File 的同义糖, 带有某些预定义的参数设置。 其中之一是编码。 默认情况下, Out- File 将以小义式 Unicode 创建文件。 因此, 要用 ASCII 编码创建文件, 您可以这样做 :

GetChild-Item C:SourcePath | ForEach-Object { $_.Name } | 
    Out-File "C:MyPlaceoutfile.txt" -Encoding ASCII

... 或者您可以使用 Add- content 默认输出 ASCII 的 Add- content :

GetChild-Item C:SourcePath | ForEach-Object { $_.Name } | 
    Add-Content "C:MyPlaceoutfile.txt"




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签