Parse .txt to .csv

前端 未结 6 962
说谎
说谎 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条回答
  •  情深已故
    2020-12-21 07:18

    Yes it is very much possible. Replace | by , and write it to a csv

    public class NewClass {
    
    public static void main(String[] args) throws IOException {
    
       String data = "one|two|three|four"+"\n"+
               "one|two|three|four";
       //Use a BufferedReader to read from actual Text file
        String csv = data.replace("|", ",");
        System.out.println(csv);
    
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("MyCSV.csv")));
        out.println(csv);
        out.close();
    }
    }
    

    Output

    run:
    one,two,three,four
    one,two,three,four
    BUILD SUCCESSFUL (total time: 0 seconds)
    

提交回复
热议问题