0
嗨我想從txt轉換爲CSV文件。在我的txt文件中我有\ t作爲分隔符。 所以我的Java代碼看起來如下將txt轉換爲csv無法使用java的大內容8
public static void main(String[] args) throws Exception {
final Path path = Paths.get("D:\\chat-log\\output\\");
final Path txt = path.resolve("summary_Traders_Violation_Rule2_Conversation.txt");
final Path csv = path.resolve("Rule2.csv");
try (
final Stream<String> lines = Files.lines(txt);
final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(csv, StandardOpenOption.CREATE_NEW))) {
lines.map((line) -> line.split("\t")).
map((line) -> Stream.of(line).collect(Collectors.joining(","))).
forEach(pw::println);
}
}
它創建CSV與異常和csv文件不列入包含從txt文件完整數據文件。直到第1,2,3列爲止的空白值都正常。第4列值開始拋出異常。
Exception in thread "main" java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
at java.io.BufferedReader$1.hasNext(Unknown Source)
at java.util.Iterator.forEachRemaining(Unknown Source)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.ReferencePipeline.forEach(Unknown Source)
at XPathEx.TxtToCsv.main(TxtToCsv.java:24)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
... 10 more
檢查您的傳入數據 –