集册 Java实例教程 使用StringTokenizer示例分析CSV文件

使用StringTokenizer示例分析CSV文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

568
使用StringTokenizer示例解析CSV文件

/**
来 自 时 代 J     a    v  a - nowjava.com
**/

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.FileWriter;

import java.util.StringTokenizer;

 

public class Main {

 

        public static void main(String[] args) {

               

                try

                {

                        String strFile = "C:/Folder/example.csv";/**来 自 N o w  J a v a  . c o m**/

                        BufferedReader br = new BufferedReader( new FileReader(strFile));

                        String strLine = "";

                        StringTokenizer st = null;

                        int lineNumber = 0, tokenNumber = 0;

                        while( (strLine = br.readLine()) != null)

                        {

                                lineNumber++;

                               

                                //break comma separated line using ","

                                st = new StringTokenizer(strLine, ",");

                               

                                while(st.hasMoreTokens())

                                {

                                        //display csv values

                                        tokenNumber++;

                                        System.out.println("Line # " + lineNumber +

                                                        ", Token # " + tokenNumber

                                                        + ", Token : "+ st.nextToken());

                                }

                           
展开阅读全文