⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csv.java

📁 21天学通java的示例程序源代码
💻 JAVA
字号:
package com.wrox.text;

import java.io.*;
import java.net.*;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/* Content handler parsing Comma Separated Values (CSV) of
 *  the MIME type "text/csv".
 * <p>
 * This content handler handles two data types: 
 * <ul>
 * <li> Vector : Each element in the Vector is a line in the CSV stream.
 *      The elements are also of type Vector and contain String
 *      String objects representing the values in the line sequence.
 * <li> javax.swing.table.TableModel 
 * </ul> */
public class csv extends ContentHandler {
  /** Parses the CSV stream of the URLConnection stream and returns
   * a Vector object. */
  public Object getContent(URLConnection urlc) throws IOException {
    BufferedReader reader =
      new BufferedReader(new InputStreamReader(urlc.getInputStream()));

    Vector rows = new Vector();
    Vector columns = new Vector();
    
    StringBuffer buffer = new StringBuffer();

    String line = reader.readLine();
    while (line != null) {
      boolean inString = false;

      for(int i=0; i<line.length(); i++) {
        char c = line.charAt(i);
        if ( (c == ',') && (!inString) ) {
          columns.add(buffer.toString());
          buffer.setLength(0);
        } else if ( (c == '"') && (!inString) ) {
          inString = true;
        } else if ( (c == '"') &&
                    ( (i+1==line.length()) || (line.charAt(i+1) != '"') )) {
          inString = false;
        } else if (c == '"') {
          buffer.append(c);
          i++; // skip second string character
        } else {
          buffer.append(c);
        }
      }
      
      columns.add(buffer.toString());
      buffer.setLength(0);

      rows.add(columns);
      columns = new Vector();

      line = reader.readLine();
    }
    
    return(rows);
  }

  /* Parses the CSV stream of the URLConnection stream and returns
   * it encapsulated in one of the following object types:
   * <ul>
   * <li> Vector
   * <li> javax.swing.table.TableModel
   * </ul>
   * Returns null if none of the formats stored in the classes
   * array is supported. */
  public Object getContent(URLConnection urlc, Class[] classes)
    throws IOException {

    if (classes == null) {
      return(getContent(urlc));
    }
    
    for(int c=0; c<classes.length; c++) {
      if (classes[c].equals(Vector.class)) {
        return(getContent(urlc));
      } else if (classes[c].equals(TableModel.class)) {
        Vector vector = (Vector) getContent(urlc);
        return(getContentAsTableModel(vector));
      }
    }
    return(null);
  }

  // Returns a Swing TableModel representation of the data 
  protected TableModel getContentAsTableModel(Vector vector) {
    // Figure out maximum number of columns in a line
    int maxColumn = 0;
    for(Iterator iter=vector.iterator(); iter.hasNext(); ) {
      Vector line = (Vector) iter.next();
      maxColumn = Math.max(maxColumn, line.size());
    }

    // Expand all lines to the maximum number of columns
    for(Iterator iter=vector.iterator(); iter.hasNext(); ) {
      Vector line = (Vector) iter.next();
      while(line.size() < maxColumn) {
        line.add("");
      }
    }
    
    // Create names for the headers
    Vector headers = new Vector(maxColumn);
    for(int i=0; i<maxColumn; i++) {
      headers.add(Integer.toString(i));
    }
    
    // Return a mutable model backed by the values read
    return(new DefaultTableModel(vector, headers));
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -