Parse .txt to .csv

前端 未结 6 934
说谎
说谎 2020-12-21 06:56

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?

My .txt

6条回答
  •  -上瘾入骨i
    2020-12-21 07:02

    try this may help

        public class Test {
    
        public static void main(String[] args) throws URISyntaxException,
                IOException {
    
            FileWriter writer = null;
            File file = new File("d:/sample.txt");
            Scanner scan = new Scanner(file);
            File file2 = new File("d:/CSV.csv");
            file.createNewFile();
            writer = new FileWriter(file2);
    
            while (scan.hasNext()) {
                String csv = scan.nextLine().replace("|", ",");
                System.out.println(csv);
                writer.append(csv);
                writer.append("\n");
                writer.flush();
            }
        }
    }
    

    sample.txt:-

      He|looked|for|a|book.
    
      He|picked|up|the|book.
    

提交回复
热议问题