本文整理汇总了Java中com.opencsv.CSVReader.close方法的典型用法代码示例。如果您正苦于以下问题:Java CSVReader.close方法的具体用法?Java CSVReader.close怎么用?Java CSVReader.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.opencsv.CSVReader
的用法示例。
在下文中一共展示了CSVReader.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initColumns
import com.opencsv.CSVReader; //导入方法依赖的package包/类
private void initColumns() throws EmptyDataFileContentException, WrongDataFileFormatException, IOException {
columns = new ArrayList<>();
CSVReader reader = openInputData();
String[] headers = reader.readNext();
for (String header : headers) {
if (!"".equals(header)) {
columns.add(header);
}
}
reader.close();
if (columns.size() < 2) {
throw new EmptyDataFileContentException(Messages.getMessage(EmptyDataFileContentException.EMPTY_DATA_FILE_CONTENT_ERROR_MESSAGE));
}
resultColumnName = columns.get(columns.size() - 1);
if (!isResultColumnNameAuthorized(resultColumnName)) {
throw new WrongDataFileFormatException(String.format(Messages.getMessage(WrongDataFileFormatException.WRONG_RESULT_COLUMN_NAME_ERROR_MESSAGE), ResultColumnNames.getAuthorizedNames()));
}
}
示例2: addTotalQuestions
import com.opencsv.CSVReader; //导入方法依赖的package包/类
public static void addTotalQuestions(Context context, List<Question> questions)
throws IOException {
//Reset inner references
CSVReader reader = new CSVReader(
new InputStreamReader(context.openFileInput(QUESTIONS_CSV)),
SEPARATOR, QUOTECHAR);
String[] line;
while ((line = reader.readNext()) != null) {
for (Question question : questions) {
if (question.getUid().equals(line[5])) {
question.setTotalQuestions(Integer.valueOf(line[13]));
question.save();
break;
}
}
}
reader.close();
}
示例3: addImagePathQuestions
import com.opencsv.CSVReader; //导入方法依赖的package包/类
public static void addImagePathQuestions(Context context) throws IOException {
//Reset inner references,
List<Question> questions = Question.getAllQuestions();
CSVReader reader = new CSVReader(
new InputStreamReader(context.openFileInput(QUESTIONS_CSV)),
SEPARATOR, QUOTECHAR);
String[] line;
while ((line = reader.readNext()) != null) {
for (Question question : questions) {
if (question.getUid().equals(line[5])) {
if (line.length > 15 && !line[15].equals("")) {
question.setPath(line[15]);
question.save();
}
break;
}
}
}
reader.close();
}
示例4: addVisibleQuestions
import com.opencsv.CSVReader; //导入方法依赖的package包/类
public static void addVisibleQuestions(Context context, List<Question> questions)
throws IOException {
//Reset inner references
CSVReader reader = new CSVReader(
new InputStreamReader(context.openFileInput(QUESTIONS_CSV)),
SEPARATOR, QUOTECHAR);
String[] line;
while ((line = reader.readNext()) != null) {
for (Question question : questions) {
if (question.getUid().equals(line[5])) {
question.setVisible(Integer.valueOf(line[14]));
question.save();
break;
}
}
}
reader.close();
}
示例5: loadIris
import com.opencsv.CSVReader; //导入方法依赖的package包/类
public void loadIris() throws IOException {
train = new ArrayList<Item>();
CSVReader reader = new CSVReader(new FileReader(this.getClass().getResource("/iris.csv").getFile()),',');
String [] nextLine;
String[] cnames = reader.readNext();
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
Item item = new Item(counter);
for(int i=0;i<nextLine.length;i++){
item.put(cnames[i], nextLine[i]);
}
train.add(item);
counter++;
}
reader.close();
}
示例6: writeValue
import com.opencsv.CSVReader; //导入方法依赖的package包/类
private void writeValue(String column, int line, String value) {
logger.debug("Writing: [{}] at line [{}] in column [{}]", value, line, column);
int colIndex = columns.indexOf(column);
CSVReader reader;
try {
reader = openOutputData();
List<String[]> csvBody = reader.readAll();
csvBody.get(line)[colIndex] = value;
reader.close();
writeValue(column, line, value, csvBody);
} catch (IOException e1) {
logger.error(Messages.getMessage(CSV_DATA_PROVIDER_WRITING_IN_CSV_ERROR_MESSAGE), column, line, value, e1);
}
}
示例7: CSVFileParser
import com.opencsv.CSVReader; //导入方法依赖的package包/类
public CSVFileParser(InputStream stream, String encoding, char delimiter,
final String mandatory_columns[],
final String mandatory_column_values[]) throws Exception {
this.lines = new HashSet<String>();
this.mandatory_cols = mandatory_column_values;
this.data = new LinkedHashMap<String, String>();
reader = new CSVReader(new InputStreamReader(stream, encoding),
delimiter);
try {
mappings = reader.readNext();
if (mappings == null) {
throw new IOException("Empty CSV file given.");
}
line = 1;
List<String> checkCols = Arrays.asList(mappings);
for (String reqCol : mandatory_columns) {
if (!checkCols.contains(reqCol)) {
throw new Exception(
"Missing mandatory column '" + reqCol + "'.");
}
}
} catch (Exception e) {
reader.close();
throw e;
}
}
示例8: readNestedMapFromFile
import com.opencsv.CSVReader; //导入方法依赖的package包/类
public static Map<String, Map<String, String>> readNestedMapFromFile(String fileName, boolean ignoreFirstLine) throws IOException {
Map<String, Map<String, String>> map = new HashMap<>();
CSVReader reader = new CSVReader(new FileReader(fileName));
if(ignoreFirstLine) reader.readNext();
String[] line = reader.readNext();
while(line != null) {
MapUtils.getMap(line[0], map).put(line[1], line[2]);
line = reader.readNext();
}
reader.close();
return map;
}
示例9: loadStops
import com.opencsv.CSVReader; //导入方法依赖的package包/类
/**
* Reads all stops and puts them in {@link #stops}
* <p/>
* <br/><br/>
* stops.txt <i>[https://developers.google.com/transit/gtfs/reference]</i><br/>
* Individual locations where vehicles pick up or drop off passengers.
*
* @throws IOException
*/
private void loadStops() throws IOException {
log.info("Loading stops.txt");
try {
CSVReader reader = createCSVReader(root + GtfsDefinitions.Files.STOPS.fileName);
String[] header = reader.readNext(); // read header
Map<String, Integer> col = getIndices(header, GtfsDefinitions.Files.STOPS.columns, GtfsDefinitions.Files.STOPS.optionalColumns); // get column numbers for required fields
String[] line = reader.readNext();
while(line != null) {
String stopId = line[col.get(GtfsDefinitions.STOP_ID)];
Stop stop = new StopImpl(stopId, line[col.get(GtfsDefinitions.STOP_NAME)], Double.parseDouble(line[col.get(GtfsDefinitions.STOP_LON)]), Double.parseDouble(line[col.get(GtfsDefinitions.STOP_LAT)]));
stops.put(stopId, stop);
// location type
if(col.get(GtfsDefinitions.LOCATION_TYPE) != null) {
if(line[col.get(GtfsDefinitions.LOCATION_TYPE)].equals("0")) {
((StopImpl) stop).setLocationType(GtfsDefinitions.LocationType.STOP);
}
if(line[col.get(GtfsDefinitions.LOCATION_TYPE)].equals("1")) {
((StopImpl) stop).setLocationType(GtfsDefinitions.LocationType.STATION);
}
}
// parent station
if(col.get(GtfsDefinitions.PARENT_STATION) != null && !line[col.get(GtfsDefinitions.PARENT_STATION)].isEmpty()) {
((StopImpl) stop).setParentStation(line[col.get(GtfsDefinitions.PARENT_STATION)]);
}
line = reader.readNext();
}
reader.close();
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Emtpy line found in stops.txt");
}
log.info("... stops.txt loaded");
}
示例10: loadCalendar
import com.opencsv.CSVReader; //导入方法依赖的package包/类
/**
* Reads all services and puts them in {@link #services}
* <p/>
* <br/><br/>
* calendar.txt <i>[https://developers.google.com/transit/gtfs/reference]</i><br/>
* Dates for service IDs using a weekly schedule. Specify when service starts and ends,
* as well as days of the week where service is available.
*
* @throws IOException
*/
private void loadCalendar() throws IOException {
log.info("Loading calendar.txt");
try {
CSVReader reader = createCSVReader(root + GtfsDefinitions.Files.CALENDAR.fileName);
String[] header = reader.readNext();
Map<String, Integer> col = getIndices(header, GtfsDefinitions.Files.CALENDAR.columns, GtfsDefinitions.Files.CALENDAR.optionalColumns);
// assuming all days really do follow monday in the file
int indexMonday = col.get(GtfsDefinitions.MONDAY);
String[] line = reader.readNext();
while(line != null) {
// if(i == Math.pow(2, c)) { log.info(" # " + i); c++; } i++;
boolean[] days = new boolean[7];
for(int d = 0; d < 7; d++) {
days[d] = line[indexMonday + d].equals("1");
}
services.put(line[col.get(GtfsDefinitions.SERVICE_ID)], new ServiceImpl(line[col.get(GtfsDefinitions.SERVICE_ID)], days, line[col.get(GtfsDefinitions.START_DATE)], line[col.get(GtfsDefinitions.END_DATE)]));
line = reader.readNext();
}
reader.close();
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Emtpy line found in calendar.txt");
}
log.info("... calendar.txt loaded");
}
示例11: loadShapes
import com.opencsv.CSVReader; //导入方法依赖的package包/类
/**
* Loads shapes (if available) and puts them in {@link #shapes}. A shape is a sequence of points, i.e. a line.
* <p/>
* <br/><br/>
* shapes.txt <i>[https://developers.google.com/transit/gtfs/reference]</i><br/>
* Rules for drawing lines on a map to represent a transit organization's routes.
*/
private void loadShapes() {
// shapes are optional
log.info("Looking for shapes.txt");
try {
CSVReader reader = createCSVReader(root + GtfsDefinitions.Files.SHAPES.fileName);
String[] header = reader.readNext();
Map<String, Integer> col = getIndices(header, GtfsDefinitions.Files.SHAPES.columns, GtfsDefinitions.Files.SHAPES.optionalColumns);
String[] line = reader.readNext();
while(line != null) {
usesShapes = true; // shape file might exists but could be empty
Id<RouteShape> shapeId = Id.create(line[col.get(GtfsDefinitions.SHAPE_ID)], RouteShape.class);
RouteShape currentShape = shapes.get(shapeId);
if(currentShape == null) {
currentShape = new GtfsShape(line[col.get(GtfsDefinitions.SHAPE_ID)]);
shapes.put(shapeId, currentShape);
}
Coord point = new Coord(Double.parseDouble(line[col.get(GtfsDefinitions.SHAPE_PT_LON)]), Double.parseDouble(line[col.get(GtfsDefinitions.SHAPE_PT_LAT)]));
currentShape.addPoint(point, Integer.parseInt(line[col.get(GtfsDefinitions.SHAPE_PT_SEQUENCE)]));
line = reader.readNext();
}
reader.close();
log.info("... shapes.txt loaded");
} catch (IOException e) {
log.info("... no shapes file found.");
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Emtpy line found in shapes.txt");
}
}
示例12: loadRoutes
import com.opencsv.CSVReader; //导入方法依赖的package包/类
/**
* Basically just reads all routeIds and their corresponding names and types and puts them in {@link #routes}.
* <p/>
* <br/><br/>
* routes.txt <i>[https://developers.google.com/transit/gtfs/reference]</i><br/>
* Transit routes. A route is a group of trips that are displayed to riders as a single service.
*
* @throws IOException
*/
private void loadRoutes() throws IOException {
log.info("Loading routes.txt");
try {
CSVReader reader = createCSVReader(root + GtfsDefinitions.Files.ROUTES.fileName);
String[] header = reader.readNext();
Map<String, Integer> col = getIndices(header, GtfsDefinitions.Files.ROUTES.columns, GtfsDefinitions.Files.ROUTES.optionalColumns);
String[] line = reader.readNext();
while(line != null) {
int routeTypeNr = Integer.parseInt(line[col.get(GtfsDefinitions.ROUTE_TYPE)]);
RouteType routeType = RouteType.getRouteType(routeTypeNr);
if (routeType == null) {
log.warn("Route " + line[col.get(GtfsDefinitions.ROUTE_ID)] + " of type " + routeTypeNr + " will be ignored");
ignoredRoutes.add(line[col.get(GtfsDefinitions.ROUTE_ID)]);
} else {
String routeId = line[col.get(GtfsDefinitions.ROUTE_ID)];
String shortName = line[col.get(GtfsDefinitions.ROUTE_SHORT_NAME)];
String longName = line[col.get(GtfsDefinitions.ROUTE_LONG_NAME)];
Route newGtfsRoute = new RouteImpl(routeId, shortName, longName, routeType);
routes.put(line[col.get(GtfsDefinitions.ROUTE_ID)], newGtfsRoute);
}
line = reader.readNext();
}
reader.close();
} catch (ArrayIndexOutOfBoundsException i) {
throw new RuntimeException("Emtpy line found in routes.txt");
}
log.info("... routes.txt loaded");
}
示例13: parseCommandCsv
import com.opencsv.CSVReader; //导入方法依赖的package包/类
/**
* Parses a command file (csv) and runs the commands specified
*/
@Override
public void parseCommandCsv(String filePath) throws IOException {
CSVReader reader = new CSVReader(new FileReader(filePath), ';');
String[] line = reader.readNext();
while(line != null) {
log.info(CollectionUtils.arrayToString(line));
executeCmdLine(line);
line = reader.readNext();
}
reader.close();
}
示例14: getRecords
import com.opencsv.CSVReader; //导入方法依赖的package包/类
/**
* Parses a csv file.
* @param inputStreamReader The browscap.csv file as a InputStreamReader object.
* @return a List of String arrays,where a String array represents one csv record.
* @throws IOException
*/
private List<String[]> getRecords(final InputStreamReader inputStreamReader) throws IOException {
final CSVReader csvReader = new CSVReader(inputStreamReader);
final List<String[]> records = csvReader.readAll();
csvReader.close();
//Different versions of the Csv file have different headers.
//We consider that each record is a String array ,where each array contains atleast 43 records.
return records.stream()
.filter( record -> record.length > 43 )
.collect(Collectors.toList());
}
示例15: importTrajectoryDataFromCSV
import com.opencsv.CSVReader; //导入方法依赖的package包/类
public ArrayList<Trajectory> importTrajectoryDataFromCSV(String path){
ArrayList<Trajectory> tracks = new ArrayList<Trajectory>();
try {
CSVReader reader = new CSVReader(new FileReader(path));
String[] nextLine;
reader.readNext(); //READ HEADER!
Trajectory t =null;
int lastID = -1;
while ((nextLine = reader.readNext()) != null) {
int nextID = Integer.parseInt(nextLine[0]) ;
double nextX = Double.parseDouble(nextLine[1]);
double nextY = Double.parseDouble(nextLine[2]);
String nextClass = nextLine[3];
if(nextID==lastID){
System.out.println();
t.add(nextX, nextY, 0);
lastID=nextID;
}else{
if(t!=null){
tracks.add(t);
}
t = new Trajectory(2);
t.setID(nextID);
t.setType(nextClass);
t.add(nextX, nextY, 0);
lastID = nextID;
}
}
tracks.add(t);
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tracks;
}