au.com.bytecode.opencsv.CSVReader.readNext()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.5k)|赞(0)|评价(0)|浏览(275)

本文整理了Java中au.com.bytecode.opencsv.CSVReader.readNext()方法的一些代码示例,展示了CSVReader.readNext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CSVReader.readNext()方法的具体详情如下:
包路径:au.com.bytecode.opencsv.CSVReader
类名称:CSVReader
方法名:readNext

CSVReader.readNext介绍

[英]Reads the next line from the buffer and converts to a string array.
[中]从缓冲区读取下一行并转换为字符串数组。

代码示例

代码示例来源:origin: databricks/learning-spark

public Tuple2<Integer, String[]> call(String line) throws Exception {
  CSVReader reader = new CSVReader(new StringReader(line));
  String[] elements = reader.readNext();
  Integer key = Integer.parseInt(elements[0]);
  return new Tuple2(key, elements);
 }
}

代码示例来源:origin: stackoverflow.com

FileInputStream fis = new FileInputStream("awesomefile.csv"); 
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
CSVReader reader = new CSVReader(isr);

for (String[] row; (row = reader.readNext()) != null;) {
  System.out.println(Arrays.toString(row));
}

reader.close();
isr.close();
fis.close();

代码示例来源:origin: stackoverflow.com

CSVReader reader=new CSVReader(
  new InputStreamReader(new FileInputStream("d:\\a.csv"), "UTF-8"), 
  ',', '\'', 1);
String[] line;
while ((line = reader.readNext()) != null) {
  StringBuilder stb = new StringBuilder(400);
  for (int i = 0; i < line.length; i++) {
     stb.append(line[i]);
     stb.append(";");
  }
  System.out.println(stb);
}

代码示例来源:origin: stackoverflow.com

String next[] = {};
   List<String[]> list = new ArrayList<String[]>();
   try {
     CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("test.csv")));
     while(true) {
       next = reader.readNext();
       if(next != null) {
         list.add(next);
       } else {
         break;
       }
     }
   } catch (IOException e) {
     e.printStackTrace();
   }

代码示例来源:origin: stackoverflow.com

public class ParseCSV {
 public static void main(String[] args) {
  try {
   //csv file containing data
   String strFile = "C:\\Users\\rsaluja\\CMS_Evaluation\\Drupal_12_08_27.csv";
   CSVReader reader = new CSVReader(new FileReader(strFile));
   String [] nextLine;
   int lineNumber = 0;
   while ((nextLine = reader.readNext()) != null) {
    lineNumber++;
    System.out.println("Line # " + lineNumber);

    // nextLine[] is an array of values from the line
    System.out.println(nextLine[4] + "etc...");
   }
  }
 }
}

代码示例来源:origin: stackoverflow.com

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import au.com.bytecode.opencsv.CSVReader;

class Test {
  public static void main(String[] args) throws IOException {
    Workbook wb = new HSSFWorkbook();
    CreationHelper helper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");

    CSVReader reader = new CSVReader(new FileReader("data.csv"));
    String[] line;
    int r = 0;
    while ((line = reader.readNext()) != null) {
      Row row = sheet.createRow((short) r++);

      for (int i = 0; i < line.length; i++)
        row.createCell(i)
          .setCellValue(helper.createRichTextString(line[i]));
    }

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
  }
}

代码示例来源:origin: Graylog2/graylog2-server

final ImmutableMap.Builder<String, String> newLookupBuilder = ImmutableMap.builder();
try (final CSVReader csvReader = new CSVReader(fileReader, config.separatorAsChar(), config.quotecharAsChar())) {
  int line = 0;
  int keyColumn = -1;
    final String[] next = csvReader.readNext();
    if (next == null) {
      break;

代码示例来源:origin: stackoverflow.com

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
  // nextLine[] is an array of values from the line
  System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

代码示例来源:origin: nodebox/nodebox

Character quot = separators.get(quotationCharacter);
if (quot == null) quot = '"';
CSVReader reader = new CSVReader(in, sep, quot);
ImmutableList.Builder<Map<String, Object>> b = ImmutableList.builder();
String[] headers = reader.readNext();
while ((row = reader.readNext()) != null) {
  ImmutableMap.Builder<String, Object> mb = ImmutableMap.builder();
  for (int i = 0; i < row.length; i++) {

代码示例来源:origin: forcedotcom/phoenix

@Override
public void map(LongWritable key, Text line, Context context) throws IOException, InterruptedException{
  CSVReader reader = new CSVReader(new InputStreamReader(new ByteArrayInputStream(line.toString().getBytes())), ',');			
  try {
    String[] tokens = reader.readNext();

代码示例来源:origin: forcedotcom/phoenix

@Test
public void testCSVUpsert() throws Exception {
  // Create table
  String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
  PhoenixConnection conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
  PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
  
  // Upsert CSV file
  CSVLoader csvUtil = new CSVLoader(conn, STOCK_TABLE, Collections.<String>emptyList(), true);
  CSVReader reader = new CSVReader(new StringReader(STOCK_CSV_VALUES_WITH_HEADER));
  csvUtil.upsert(reader);
  // Compare Phoenix ResultSet with CSV file content
  PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, COMPANY FROM " + STOCK_TABLE);
  ResultSet phoenixResultSet = statement.executeQuery();
  reader = new CSVReader(new StringReader(STOCK_CSV_VALUES_WITH_HEADER));
  reader.readNext();
  String[] csvData;
  while ((csvData = reader.readNext()) != null) {
    assertTrue (phoenixResultSet.next());
    for (int i=0; i<csvData.length; i++) {
      assertEquals(csvData[i], phoenixResultSet.getString(i+1));
    }
  }
  
  assertFalse(phoenixResultSet.next());
  conn.close();
}

代码示例来源:origin: forcedotcom/phoenix

@Test
public void testCSVUpsertWithCustomDelimiters() throws Exception {
 // Create table
  String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
  PhoenixConnection conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
  PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
  // Upsert CSV file
  CSVLoader csvUtil = new CSVLoader(conn, STOCK_TABLE, Arrays.<String>asList(STOCK_COLUMNS), true, Arrays.asList("1","2","3"));
  CSVReader reader = new CSVReader(new StringReader(STOCK_CSV_VALUES_WITH_DELIMITER),'\u0001','\u0002','\u0003');
  csvUtil.upsert(reader);
  // Compare Phoenix ResultSet with CSV file content
  PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, COMPANY FROM " + STOCK_TABLE);
  ResultSet phoenixResultSet = statement.executeQuery();
  reader = new CSVReader(new StringReader(STOCK_CSV_VALUES_WITH_DELIMITER),'\u0001','\u0002','\u0003');
  String[] csvData;
  while ((csvData = reader.readNext()) != null) {
    assertTrue (phoenixResultSet.next());
    for (int i=0; i<csvData.length; i++) {
      assertEquals(csvData[i], phoenixResultSet.getString(i+1));
    }
  }
  assertFalse(phoenixResultSet.next());
  conn.close();
}

代码示例来源:origin: forcedotcom/phoenix

@Test
public void testCSVUpsertWithColumns() throws Exception {
  // Create table
  String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
  PhoenixConnection conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
  PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
  
  // Upsert CSV file
  CSVLoader csvUtil = new CSVLoader(conn, STOCK_TABLE, Arrays.<String>asList(STOCK_COLUMNS), true);
  CSVReader reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
  csvUtil.upsert(reader);
  // Compare Phoenix ResultSet with CSV file content
  PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, COMPANY FROM " + STOCK_TABLE);
  ResultSet phoenixResultSet = statement.executeQuery();
  reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
  String[] csvData;
  while ((csvData = reader.readNext()) != null) {
    assertTrue (phoenixResultSet.next());
    for (int i=0; i<csvData.length; i++) {
      assertEquals(csvData[i], phoenixResultSet.getString(i+1));
    }
  }
  
  assertFalse(phoenixResultSet.next());
  conn.close();
}

代码示例来源:origin: forcedotcom/phoenix

@Test
public void testCSVUpsertWithBogusColumn() throws Exception {
  // Create table
  String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
  PhoenixConnection conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
  PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
  
  // Upsert CSV file
  CSVLoader csvUtil = new CSVLoader(conn, STOCK_TABLE, Arrays.asList(STOCK_COLUMNS_WITH_BOGUS), false);
  CSVReader reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
  csvUtil.upsert(reader);
  // Compare Phoenix ResultSet with CSV file content
  PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, COMPANY FROM " + STOCK_TABLE);
  ResultSet phoenixResultSet = statement.executeQuery();
  reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
  String[] csvData;
  while ((csvData = reader.readNext()) != null) {
    assertTrue (phoenixResultSet.next());
    assertEquals(csvData[0], phoenixResultSet.getString(1));
    assertNull(phoenixResultSet.getString(2));
  }
  
  assertFalse(phoenixResultSet.next());
  conn.close();
}

代码示例来源:origin: forcedotcom/phoenix

@Test
public void testCSVUpsertWithNoColumns() throws Exception {
  // Create table
  String statements = "CREATE TABLE IF NOT EXISTS " + STOCK_TABLE + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY VARCHAR);";
  PhoenixConnection conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
  PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
  
  // Upsert CSV file
  CSVLoader csvUtil = new CSVLoader(conn, STOCK_TABLE, null, true);
  CSVReader reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
  csvUtil.upsert(reader);
  // Compare Phoenix ResultSet with CSV file content
  PreparedStatement statement = conn.prepareStatement("SELECT SYMBOL, COMPANY FROM " + STOCK_TABLE);
  ResultSet phoenixResultSet = statement.executeQuery();
  reader = new CSVReader(new StringReader(STOCK_CSV_VALUES));
  String[] csvData;
  while ((csvData = reader.readNext()) != null) {
    assertTrue (phoenixResultSet.next());
    for (int i=0; i<csvData.length; i++) {
      assertEquals(csvData[i], phoenixResultSet.getString(i+1));
    }
  }
  
  assertFalse(phoenixResultSet.next());
  conn.close();
}

代码示例来源:origin: forcedotcom/phoenix

CSVReader reader = new CSVReader(new StringReader(DATATYPES_CSV_VALUES));
csvUtil.upsert(reader);
        + DATATYPE_TABLE);
ResultSet phoenixResultSet = statement.executeQuery();
reader = new CSVReader(new StringReader(DATATYPES_CSV_VALUES));
reader.readNext();
String[] csvData;
while ((csvData = reader.readNext()) != null) {
  assertTrue (phoenixResultSet.next());
  for (int i=0; i<csvData.length - 2; i++) {

代码示例来源:origin: apache/tika

@Test
public void testSimpleWord() throws Exception {
  Response response = WebClient
      .create(endPoint + META_PATH)
      .type("application/msword")
      .accept("text/csv")
      .put(ClassLoader
          .getSystemResourceAsStream(TikaResourceTest.TEST_DOC));
  Reader reader = new InputStreamReader((InputStream) response.getEntity(), UTF_8);
  CSVReader csvReader = new CSVReader(reader);
  Map<String, String> metadata = new HashMap<String, String>();
  String[] nextLine;
  while ((nextLine = csvReader.readNext()) != null) {
    metadata.put(nextLine[0], nextLine[1]);
  }
  csvReader.close();
  assertNotNull(metadata.get(TikaCoreProperties.CREATOR.getName()));
  assertEquals("Maxim Valyanskiy", metadata.get(TikaCoreProperties.CREATOR.getName()));
  assertEquals("X-TIKA:digest:MD5", "f8be45c34e8919eedba48cc8d207fbf0",
      metadata.get("X-TIKA:digest:MD5"));
}

代码示例来源:origin: aws-samples/aws-big-data-blog

try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
  String[] nextLine;
  while ((nextLine = reader.readNext()) != null) {
    TitanVertex vertex = graph.addVertexWithLabel("restaurant");
    vertex.addProperty(RESTAURANT_ID, nextLine[0]);
try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
  String[] nextLine;
  while ((nextLine = reader.readNext()) != null) {
    TitanVertex vertex = graph.addVertexWithLabel("genre");
    vertex.addProperty(GENRE, nextLine[0]);
try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
  String[] nextLine;
  while ((nextLine = reader.readNext()) != null) {
    TitanVertex vertex = graph.addVertexWithLabel("user");
    vertex.addProperty(USER_ID, nextLine[0]);
try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
  String[] nextLine;
  while ((nextLine = reader.readNext()) != null) {
try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
  String[] nextLine;
  while ((nextLine = reader.readNext()) != null) {
try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
  String[] nextLine;
  while ((nextLine = reader.readNext()) != null) {

代码示例来源:origin: apache/tika

CSVReader csvReader = new CSVReader(reader);
while ((nextLine = csvReader.readNext()) != null) {
  metadata.put(nextLine[0], nextLine[1]);

代码示例来源:origin: stackoverflow.com

CSVReader reader = new CSVReader(new FileReader("C:/Users/Thanushiya/Desktop/mobios/internship/csvfile/csvfile/Master.csv"), ',', '\'', 17);
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
 // put the code from your for loop here
}

相关文章