0

下面的代码正在读取一个 .csv 文件并以(当前输出)方式在控制台上显示它。

我希望它在控制台上显示为(想要的输出)方式...对不起,我无法添加图像... (行)表示在 2 个数据之间,我需要一条垂直线...请指导我.谢谢你提前

想要的输出:-

Firstname (line) Lastname (line) Main-Email (line) (current employer) (line)
ram       (line) rfa      (line) k@.com     (line) (golhys)           (line)

电流输出:-

Line # 1 Token : "First Name","Last Name","Main-Email","Current Employer","Internal    Contact ID","PSI Club Member Nbr.","Membership Type","Membership Status","Member Since","Membership Card Sent","Membership Start Date","Membership Expiration Date","Nationality 1","Membership ID"

读取文件.java

public static void main(String[] args) {
    String fileName="C:/Users/Desktop/cvsFile.csv";
    try {
        BufferedReader br = new BufferedReader( new FileReader(fileName));
        //  String strLine = null;
        StringTokenizer st = null;
        int lineNumber = 0, tokenNumber = 0;
        while( (fileName = br.readLine()) != null)
        {
            lineNumber++;

            //break comma separated line using ","
            st = new StringTokenizer(fileName, "");
            while(st.hasMoreTokens())
            {
                //display csv values
                tokenNumber++;
                System.out.println("Line # " + lineNumber + " Token : "+     st.nextToken());//.replaceAll(",", ",|,") replaceAll("..(?!$)", "$0 ")

            }
            //reset token number
            tokenNumber = 0;
        }
    }

    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
4

2 回答 2

1

参考这段代码:

public static void main(String[] args) {
    String fileName="/home/Csvfile.csv";
    try {
      BufferedReader br = new BufferedReader( new FileReader(fileName));
      String strLine = null;
      StringTokenizer st = null;
      int lineNumber = 0, tokenNumber = 0;
      while( (fileName = br.readLine()) != null)
      {
        lineNumber++;
        //break comma separated line using ","
        st = new StringTokenizer(fileName, ",");
    System.out.print("\n" + lineNumber + " | ");    
        while(st.hasMoreTokens())
        {
          //display csv values
          tokenNumber++;
          System.out.print(st.nextToken()+" | ");
        }
        //reset token number
        tokenNumber = 0;
      }
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
于 2012-09-04T07:15:41.513 回答
1

这是我的做法

public static void main(String[] args) {
    String fileName = "cvsFile.csv";
    ArrayList<Integer> margins = new ArrayList<Integer>();
    BufferedReader br;
    String line, token;
    int i;
    try {
        br = new BufferedReader(new FileReader(fileName));
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, ",\"");
            i = 0;
            while (st.hasMoreTokens()) {
                token = st.nextToken();
                if (margins.size() <= i) {
                    margins.add((Integer) token.length());
                } else {
                    margins.set(
                            i,
                            Math.max(margins.get(i),
                                    (Integer) token.length()));
                }
                i++;
            }
        }
        br = new BufferedReader(new FileReader(fileName));
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, ",\"");
            i = 0;
            while (st.hasMoreTokens()) {
                token = st.nextToken();
                System.out.print(token);
                for (int j = 0; j < margins.get(i) - token.length(); j++) {
                    System.out.print(" ");
                }
                System.out.print("|");
                i++;
            }
            System.out.println();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
于 2012-09-04T08:00:49.123 回答