将.txt解析为.csv [英] Parse .txt to .csv

查看:158
本文介绍了将.txt解析为.csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个Java程序来识别.txt文件中的文本并将其写入.csv文件?如果是的话,你会如何从这个问题开始?

Is it possible to create a Java program which recognizes the text in a .txt file and write it in a .csv file? If yes,how would you start with such a problem?

我的.txt文件是Text1 | Text 2所以我可以以某种方式得到字符|

My .txt file is Text1 |Text 2 so I could somehow get the char "|" and split it into two cells.

推荐答案

这在Java 8中非常简单:

This is very simple in Java 8:

public static void main(String[] args) throws Exception {
    final Path path = Paths.get("path", "to", "folder");
    final Path txt = path.resolve("myFile.txt");
    final Path csv = path.resolve("myFile.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("\\|")).
                map((line) -> Stream.of(line).collect(Collectors.joining(","))).
                forEach(pw::println);
    }
}

首先你的文件是路径对象。

然后你打开一个 PrintWriter 到你的目的地路径

First you get your files at Path objects.
Then you open a PrintWriter to your destination Path.

现在你用lambdas做一些Java 8流处理:

Now you do some Java 8 stream processing with lambdas:


  • Files.lines(txt)从文件中流出行

  • map((line) - > line.split(\\ |))上将每一行拆分为 String [] |

  • map((line) - > Stream.of(line).collect(Collectors.joining(,) ))使用再次加入单个 String []

  • forEach(pw :: println)将新行写入目标文件。

  • Files.lines(txt) streams the lines from the file
  • map((line) -> line.split("\\|")) splits each line to a String[] on |
  • map((line) -> Stream.of(line).collect(Collectors.joining(","))) joins the individual String[] again using ,
  • forEach(pw::println) writes the new lines to the destination file.

使用导入静态

    try (
            final Stream<String> lines = Files.lines(txt);
            final PrintWriter pw = new PrintWriter(newBufferedWriter(csv, StandardOpenOption.CREATE_NEW))) {
        lines.map((line) -> line.split("\\|")).
                map((line) -> Stream.of(line).collect(joining(","))).
                forEach(pw::println);
    }

由于Java 8仅在昨天发布,因此这是一个Java 7解决方案:

As Java 8 was released only yesterday here is a Java 7 solution:

public static void main(String[] args) throws Exception {
    final Path path = Paths.get("path", "to", "folder");
    final Path txt = path.resolve("myFile.txt");
    final Path csv = path.resolve("myFile.csv");
    final Charset utf8 = Charset.forName("UTF-8");
    try (
            final Scanner scanner = new Scanner(Files.newBufferedReader(txt, utf8));
            final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(csv, utf8, StandardOpenOption.CREATE_NEW))) {
        while (scanner.hasNextLine()) {
            pw.println(scanner.nextLine().replace('|', ','));
        }
    }
}

再次, import static

    try (
            final Scanner scanner = new Scanner(newBufferedReader(txt, utf8));
            final PrintWriter pw = new PrintWriter(newBufferedWriter(csv, utf8, StandardOpenOption.CREATE_NEW))) {
        while (scanner.hasNextLine()) {
            pw.println(scanner.nextLine().replace('|', ','));
        }
    }

这篇关于将.txt解析为.csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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