public static void main(String[] args) { if (args.length < 1 || args.length > 2) { System.out.println("Usage: PosTagger <file_in> [<file_out]"); System.exit(0); } try { CSVReader reader = new CSVReader(new FileReader(args[0]), ';'); // List<String> lstSemanticSkeletons = // BisonAdapter.getStrSemanticSkeletons(Files.lines(Paths.get(args[0]))); List<String> lstSemanticSkeletons = BisonAdapter.getStrSemanticSkeletons(reader.readAll().stream()); if (args.length == 2) { Files.write(Paths.get(args[1]), lstSemanticSkeletons); } else { lstSemanticSkeletons.forEach(System.out::println); } } catch (IOException e) { System.err.println("Problem opening file: " + args[0]); } }
@RequestMapping(value = "/events/{eventName}/pending-payments/bulk-confirmation", method = POST) public List<Triple<Boolean, String, String>> bulkConfirmation( @PathVariable("eventName") String eventName, Principal principal, @RequestBody UploadBase64FileModification file) throws IOException { try (InputStreamReader isr = new InputStreamReader(file.getInputStream()); CSVReader reader = new CSVReader(isr)) { Event event = loadEvent(eventName, principal); return reader .readAll() .stream() .map( line -> { String reservationID = null; try { Validate.isTrue(line.length >= 2); reservationID = line[0]; ticketReservationManager.validateAndConfirmOfflinePayment( reservationID, event, new BigDecimal(line[1])); return Triple.of(Boolean.TRUE, reservationID, ""); } catch (Exception e) { return Triple.of( Boolean.FALSE, Optional.ofNullable(reservationID).orElse(""), e.getMessage()); } }) .collect(Collectors.toList()); } }
public static void readFromFile(KoohiiReader kr, String file) { try (CSVReader reader = new CSVReader(new FileReader(file))) { String[] fields; while ((fields = reader.readNext()) != null) { if (fields.length <= 1) continue; if (fields.length != 6) { System.out.println( "Warning: wrong number of elements in line " + Arrays.toString(fields)); continue; } KoohiiEntry ke = new KoohiiEntry(); ke.index = fields[0]; ke.character = fields[1]; ke.keyword = fields[2]; ke.story = fields[5]; kr.parseStory(ke); kr.add(ke); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * Method use OpenCsv Library for * * @param fileInputCsv the File CSV to parse. * @param separator the char separator. * @return the List of Bean parsed from the CSV file. */ public static List<String[]> parseCSVFileAsList(File fileInputCsv, char separator) { try { List<String[]> records; // read all lines at once try ( // create CSVReader object CSVReader reader = new CSVReader(new FileReader(fileInputCsv), separator)) { // read all lines at once records = reader.readAll(); Iterator<String[]> iterator = records.iterator(); records.clear(); // skip header row iterator.next(); while (iterator.hasNext()) { String[] record = iterator.next(); records.add(record); } } return records; } catch (IOException e) { logger.error( "Can't parse the CSV file:" + fileInputCsv.getAbsolutePath() + " -> " + e.getMessage(), e); return new ArrayList<>(); } }
public static void generateAppEagleFile( String appEagleProducts, String excludeFile, String outfile) throws Exception { try { Map<String, String[]> aeMap = getAppEagleProducts(appEagleProducts); String inventoryInfo = "invInfo.csv"; Map<String, String[]> excludes = SalesInfo.getExcludes(excludeFile); SalesInfo.setConnectionString(getDBPath()); SalesInfo.getRestockingInfo(inventoryInfo, excludes); CSVReader inv_reader = new CSVReader(new FileReader(inventoryInfo)); String[] nextLine; nextLine = inv_reader.readNext(); // read header line Map<String, String[]> invMap = new Hashtable<String, String[]>(); while ((nextLine = inv_reader.readNext()) != null) { invMap.put(nextLine[0], nextLine); } Map<String, String> aeOutput = new Hashtable<String, String>(); for (String SKU : aeMap.keySet()) { try { String[] pr = invMap.get(SKU); aeOutput.put(SKU, pr[3]); } catch (Exception ex) { out.println(ex.getMessage() + ": " + SKU); } } writeAEFile(aeOutput, outfile); } catch (Exception ex) { out.println(ex.getMessage() + " in generateAppEagleFile"); throw (ex); } }
public void loadGridNet(String mapGrid) throws IOException { Preconditions.checkArgument(!Strings.isNullOrEmpty(mapGrid)); logger.debug("loading {}...", mapGrid); CSVReader reader = new CSVReader(new FileReader(mapGrid), ',', '"', 1); String[] row; while ((row = reader.readNext()) != null) { String gridId = row[1].trim(); String dmRoads = row[2].trim(); String gjRoads = row[3].trim(); Set<String> x = Sets.newHashSet(Splitter.on('|').trimResults().omitEmptyStrings().split(dmRoads)); Set<String> y = Sets.newHashSet(Splitter.on('|').trimResults().omitEmptyStrings().split(gjRoads)); if (x.size() > 0 || y.size() > 0) { MapGrid grid = new MapGrid(); grid.dmRoads = x; grid.gjRoads = y; gridNet.put(gridId, grid); // logger.debug("{},{},{}", gridId, x, y); } } reader.close(); }
/** * Tests if the tweet is already in the base csv * * @param id * @return index where the tweet was found, otherwise -1 */ public int alreadyIn(long id) { CSVReader reader = null; String[] nextLine; try { reader = new CSVReader(new FileReader(AppliSettings.filename), ';'); } catch (FileNotFoundException e) { System.out.println("alreadyIn:fileNotFound"); System.out.println(e.getMessage()); } try { int i = 0; while ((nextLine = reader.readNext()) != null) { if (nextLine[0].equals(Long.toString(id))) { reader.close(); return i; } i++; } } catch (IOException e) { System.out.println("alreadyIn:IOExc"); System.out.println(e.getMessage()); } return -1; }
public void insertGrandprizePoint() throws IOException, SQLException { recordCounter = 0; System.out.println("Starting inserting Grand Prize point records into points' table.."); LogLoader.setInfo( PointUpdaterDaemon.class.getSimpleName(), "[INSERT GRANDPRIZE] Started: " + getCurrentTimeStamp()); // INSERT MONTHLY - Supposed to be executed on INIT w/CREATE TABLE function // Binding a single input stream CSV reader & its buffer CSVReader grandprizeReader = new CSVReader( new FileReader(workingDir + grandprizeFile), recSeparator, escChar, headerLine); String[] grandprize; String insertGrandprizeQuery; if (con == null) { db_object.openConnection(); con = db_object.getConnection(); } try { stm = con.createStatement(); while ((grandprize = grandprizeReader.readNext()) != null) { // System.out.println(monthly[0] + monthly[1] + monthly[2]); insertGrandprizeQuery = "INSERT INTO tbl_points_grandprize (point_cardno, point_grandprize) VALUES ('" + grandprize[1].trim() + "', " + grandprize[2].trim() + ");"; stm.addBatch(insertGrandprizeQuery); // stm = con.createStatement(); // affectedRow = stm.executeUpdate(updateGrandprizeQuery); recordCounter++; } stm.executeBatch(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { stm.close(); if (con != null) { try { db_object.closeConnection(); } catch (SQLException e) { e.printStackTrace(); } finally { con = null; } } } statGrandprize = true; System.out.println("Grand Prize Records Inserted:" + recordCounter); System.out.println("Grand Prize Records Finished Time: " + getCurrentTimeStamp()); LogLoader.setInfo( PointUpdaterDaemon.class.getSimpleName(), "[INSERT GRANDPRIZE] Finished: " + getCurrentTimeStamp()); }
public static List<String[]> getAllEntries(String db) throws DatabaseException { checkDb(db); try (CSVReader reader = new CSVReader(new FileReader(getDbFullPath(db)))) { return reader.readAll(); } catch (IOException ex) { throw new DatabaseException("can't get all entries from database!"); } }
public void updateMonthlyPoint() throws IOException, SQLException { recordCounter = 0; System.out.println("Starting updating monthly point records into points' table.."); LogLoader.setInfo( PointUpdaterDaemon.class.getSimpleName(), "[UPDATE MONTHLY] Started: " + getCurrentTimeStamp()); // UPDATE MONTHLY // Binding a single input stream CSV reader & its buffer CSVReader monthlyReader = new CSVReader(new FileReader(workingDir + monthlyFile), recSeparator, escChar, headerLine); String[] monthly; String updateMonthlyQuery; if (con == null) { db_object.openConnection(); con = db_object.getConnection(); } try { stm = con.createStatement(); while ((monthly = monthlyReader.readNext()) != null) { // System.out.println(monthly[0] + monthly[1] + monthly[2]); updateMonthlyQuery = "UPDATE tbl_points SET point_monthly = " + monthly[2].trim() + " WHERE point_cardno = '" + monthly[1].trim() + "';"; stm.addBatch(updateMonthlyQuery); recordCounter++; } stm.executeBatch(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { stm.close(); if (con != null) { try { db_object.closeConnection(); } catch (SQLException e) { e.printStackTrace(); } finally { con = null; } } } statMonthly = true; System.out.println("Updated Monthly Records:" + recordCounter); System.out.println("Monthly Records Update Finished Time: " + getCurrentTimeStamp()); LogLoader.setInfo( PointUpdaterDaemon.class.getSimpleName(), "[UPDATE MONTHLY] Finished: " + getCurrentTimeStamp()); }
public void updateGrandprizePoint() throws IOException, SQLException { recordCounter = 0; System.out.println("Starting updating grand prize point records into points' table.."); LogLoader.setInfo( PointUpdaterDaemon.class.getSimpleName(), "[UPDATE GRANDPRIZE] Started: " + getCurrentTimeStamp()); // UPDATE GRANDPRIZE CSVReader grandprizeReader = new CSVReader( new FileReader(workingDir + grandprizeFile), recSeparator, escChar, headerLine); String[] grandprize; String updateGrandprizeQuery; if (con == null) { db_object.openConnection(); con = db_object.getConnection(); } try { stm = con.createStatement(); while ((grandprize = grandprizeReader.readNext()) != null) { // System.out.println(grandprize[0] + grandprize[1] + grandprize[2]); updateGrandprizeQuery = "UPDATE tbl_points SET point_grandprize = " + grandprize[2].trim() + " WHERE point_cardno = '" + grandprize[1].trim() + "';"; stm.addBatch(updateGrandprizeQuery); recordCounter++; } stm.executeBatch(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { stm.close(); if (con != null) { try { db_object.closeConnection(); } catch (SQLException e) { e.printStackTrace(); } finally { con = null; } } } statGrandprize = true; System.out.println("Updated Grand Prize Records: " + recordCounter); System.out.println("Grand Prize Records Finished Time: " + getCurrentTimeStamp()); LogLoader.setInfo( PointUpdaterDaemon.class.getSimpleName(), "[UPDATE GRANDPRIZE] Finished: " + getCurrentTimeStamp()); }
/** * Update the ranks in the csv file when the tweet is already in the base but saved with another * mark * * @param mark : note of the tweet * @param row : line of the tweet in the base * @throws IOException */ public static void updateCSV(int mark, int row) throws IOException { CSVReader reader = new CSVReader(new FileReader(AppliSettings.filename), ';'); List<String[]> csvBody = reader.readAll(); csvBody.get(row)[4] = Integer.toString(mark); reader.close(); CSVWriter writer = new CSVWriter(new FileWriter(AppliSettings.filename), ';'); writer.writeAll(csvBody); writer.flush(); writer.close(); }
/** * reads a csv file * * @param filepath filepath to the file */ public void readCSV(String filepath) { try { CSVReader reader = new CSVReader(new FileReader(filepath)); String[] nextLine; while ((nextLine = reader.readNext()) != null) { // nextLine[] is an array of values from the line System.out.println(nextLine[0] + nextLine[1] + "etc..."); } } catch (Exception ex) { System.out.println("Read csv error with " + filepath + " : " + ex.getMessage()); } }
public LinePlotTest() { super(new BorderLayout()); setPreferredSize(new Dimension(800, 600)); setBackground(Color.WHITE); // load data from file ---------------------------------------------- // basemean, log2foldchange DataTable nonSigData = new DataTable(Double.class, Double.class); DataTable sigData = new DataTable(Double.class, Double.class); try (CSVReader reader = new CSVReader(new FileReader("rnaSeqExample.csv"))) { String[] nextLine; int row = 0; while ((nextLine = reader.readNext()) != null) { if (row != 0) { // needs to have a p-value if (nextLine[2].equals("NA")) continue; double baseMean = Double.parseDouble(nextLine[1]); double log2Change = Double.parseDouble(nextLine[2]); if (nextLine[6].equals("NA") || Double.parseDouble(nextLine[6]) > 0.05) { nonSigData.add(Math.log10(baseMean), log2Change); } else { sigData.add(Math.log10(baseMean), log2Change); } } row++; } } catch (IOException e) { e.printStackTrace(); } System.out.println("Non-significant: " + nonSigData.getRowCount()); System.out.println("Significant: " + sigData.getRowCount()); XYPlot plot = new XYPlot(nonSigData, sigData); // set point renderer for data PointRenderer points = new DefaultPointRenderer2D(); points.setShape(new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0)); points.setColor(NONSIG_COLOR); plot.setPointRenderer(nonSigData, points); PointRenderer pointsRed = new DefaultPointRenderer2D(); pointsRed.setShape(new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0)); pointsRed.setColor(SIG_COLOR); plot.setPointRenderer(sigData, pointsRed); // Display on screen add(new InteractivePanel(plot), BorderLayout.CENTER); }
public static Map<String, String[]> getAppEagleProducts(String filename) throws Exception { try { Map<String, String[]> aeMap = new Hashtable<String, String[]>(); CSVReader ae_reader = new CSVReader(new FileReader(filename)); String[] nextLine; nextLine = ae_reader.readNext(); // read header while ((nextLine = ae_reader.readNext()) != null) { aeMap.put(nextLine[0], nextLine); } return aeMap; } catch (Exception ex) { out.println(ex.getMessage() + " in getAppEagleProducts"); throw (ex); } }
/** Load the base tweet, and initialize the tweet list and the classification list */ public static void loadBaseTweet() { baseTweets = new ArrayList<Tweet>(); listCleanTweets = new ArrayList<Tweet>(); listNegTweets = new ArrayList<Tweet>(); listNeuTweets = new ArrayList<Tweet>(); listPosTweets = new ArrayList<Tweet>(); CSVReader reader = null; String[] nextLine; try { reader = new CSVReader(new FileReader(AppliSettings.filename), ';'); try { while ((nextLine = reader.readNext()) != null) { Tweet t = new Tweet( Long.parseLong(nextLine[0]), nextLine[1], nextLine[2], nextLine[3], Integer.parseInt(nextLine[4])); baseTweets.add(t); listCleanTweets.add(t); switch (t.getNote()) { case 0: listNegTweets.add(t); break; case 2: listNeuTweets.add(t); break; case 4: listPosTweets.add(t); break; } } } catch (NumberFormatException | IOException e) { System.out.println("load:numFormatExc"); System.out.println(e.getMessage()); } } catch (FileNotFoundException e) { System.out.println("load:fileExc"); System.out.println(e.getMessage()); } }
/** * Method to get the String array of the columns of a CSV File. * * @param fileCSV the File CSV. * @param hasFirstLine if true the first line of CSV File contains the columns name. * @return a String Array of the columns. */ public static String[] getHeaders(File fileCSV, boolean hasFirstLine) { String[] columns = new String[0]; try { CSVReader reader = new CSVReader(new FileReader(fileCSV)); columns = reader.readNext(); // assuming first read if (!hasFirstLine) { int columnCount = 0; if (columns != null) columnCount = columns.length; columns = new String[columnCount]; for (int i = 0; i < columnCount; i++) { columns[i] = "Column#" + i; } } } catch (IOException e) { logger.error("Can't find the Headers on the CSV File", e); } return columns; }
@org.junit.Test public void testgetRestockingInfo() throws Exception { Map<String, String[]> excludes = SalesInfo.getExcludes(getExcludeFilename()); String testOutputFile = "reOrderInfo.csv"; SalesInfo.setConnectionString(getDBPath()); SalesInfo.getRestockingInfo(testOutputFile, excludes); CSVReader inv_reader = new CSVReader(new FileReader(testOutputFile)); String[] nextLine; nextLine = inv_reader.readNext(); // read header line Map<String, String[]> invMap = new Hashtable<String, String[]>(); while ((nextLine = inv_reader.readNext()) != null) { invMap.put(nextLine[0], nextLine); } String[] oo = invMap.get("A220"); assertTrue(oo[2].equals("Lie-Nielsen No.102 Low Angle Block Plane - Bronze")); }
public static void main(String[] args) throws Exception { // Login to Google GAService service = new GAService("*****@*****.**", "9nHBRrzC&vu5"); service.doLogin(); // Read in CSV to an Array called myEntries CSVReader reader = new CSVReader(new FileReader("alerts_update.csv")); List<String[]> CompanyList = reader.readAll(); String CommonQuery = " AND (Appointment OR Appointments OR Expands OR Expansion OR Executive OR EMEA OR APAC)"; // Use try so that we can close 'reader' try { // Loop through the array columns to get the values in each row for (String[] Companies : CompanyList) { // Loop through the row to get the values in each column for (String Company : Companies) { // Append the common query on to the company name String Query = "\\\"" + Company + "\\\"" + CommonQuery; // Create the Google Alert Alert alert = new Alert(); alert.setHowMany(HowMany.ONLY_THE_BEST_RESULTS); alert.setHowOften(HowOften.ONCE_A_DAY); alert.setRegion(Region.Any_Region); alert.setLanguage(Language.English); alert.setSources(new String[] {Sources.NEWS, Sources.BLOGS, Sources.WEB}); alert.setSearchQuery(Query); alert.setDeliveryTo(DeliveryTo.EMAIL); String id = service.createAlert(alert); System.out.println(Query); } } } finally { reader.close(); } }
@Test public void firstDDMethod() throws IOException { String temp1 = null, temp2 = null; wait = new WebDriverWait(driver, 10); driver.manage().deleteAllCookies(); driver.navigate().to("http://www.gmail.com"); String path = "D://Badari/Learning/dd1.csv"; CSVReader reader = new CSVReader(new FileReader(path)); String[] data; while ((data = reader.readNext()) != null) { temp1 = data[0]; temp2 = data[1]; } System.out.println(temp1); System.out.println(temp2); }
private List<List<String>> readContentFromFile() { try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); ) { CSVReader csvReader = new CSVReaderBuilder(br) .withCSVParser(new CSVParser(getFieldSeparator())) .withSkipLines(1) .build(); List<List<String>> result = new ArrayList<>(); // TODO use java8 lambda magic for (String[] row : csvReader.readAll()) { result.add(new ArrayList<>(Arrays.asList(row))); } csvReader.close(); return result; } catch (Exception e) { throw new RuntimeException("Something went wrong during reading a datasource.", e); } }
@org.junit.Test public void testOnOrderFiles() throws Exception { Map<String, String[]> excludes = SalesInfo.getExcludes(getExcludeFilename()); String testOnOrderFile = "OnOrderInfo.csv"; SalesInfo.setConnectionString(getDBPath()); SalesInfo.getOnOrderInfo(testOnOrderFile, excludes); CSVReader onOrder_reader = new CSVReader(new FileReader(testOnOrderFile)); String[] nextLine; nextLine = onOrder_reader.readNext(); // read header line Map<String, String[]> onOrderMap = new Hashtable<String, String[]>(); while ((nextLine = onOrder_reader.readNext()) != null) { onOrderMap.put(nextLine[0], nextLine); } String[] oo = onOrderMap.get("V05M0901"); assertTrue(oo[1].equals("14")); }
/** * Method to get the content of a comma separated file (.csv,.input,.txt) * * @param CSV the File comma separated. * @param noHeaders if true jump the first line of the content. * @return the List of Array of the content of the File comma separated. */ public static List<String[]> parseCSVFileAsList(File CSV, boolean noHeaders) { List<String[]> content; try { CSVReader reader1 = new CSVReader(new FileReader(CSV)); content = reader1.readAll(); /* List<String[]> myDatas = reader1.readAll(); String[] lineI = myDatas.get(i); for (String[] line : myDatas) { for (String value : line) { //do stuff with value } }*/ if (noHeaders) content.remove(0); if (content.get(0).length <= 1) { logger.warn( "Attention: You haven't parsed correctly the CSV file with OpenCSV API try with Univocity Method"); } return content; } catch (IOException e) { logger.error("Can't find the CSV File:" + e.getMessage(), e); return null; } }
/** * Method use OpenCsv Library for * * @param clazz the Class of the Bean. * @param fileInputCsv the File CSV to parse. * @param separator the char separator. * @param <T> the generic variable. * @return the List of Bean parsed from the CSV file. */ public static <T> List<T> parseCSVFileAsList(Class<T> clazz, File fileInputCsv, char separator) { try { List<T> beans; try ( // create CSVReader object CSVReader reader = new CSVReader(new FileReader(fileInputCsv), separator)) { beans = new ArrayList<>(); // read line by line String[] record; // skip header row String[] headers = reader.readNext(); // read content while ((record = reader.readNext()) != null) { T t = ReflectionUtilities.invokeConstructor(clazz); for (int i = 0; i < record.length; i++) { String nameMethod = "set" + org.apache.commons.lang3.StringUtils.capitalize(headers[i]); // invoke setter method if (ReflectionUtilities.checkMethod(clazz, nameMethod)) { ReflectionUtilities.invokeSetter(t, nameMethod, record[i]); } else { logger.warn( "Not exists the Method with name:" + nameMethod + " on the Bean:" + t.getClass().getName()); } } beans.add(t); } } return beans; } catch (IOException e) { logger.error( "Can't parse the CSV file:" + fileInputCsv.getAbsolutePath() + " -> " + e.getMessage(), e); return new ArrayList<>(); } }
private void LoadUserBaseInfo(Cache cache, int fileNumber) throws Exception { System.out.println("####### 开始 把 " + fileNumber + ".csv 数据放入缓存. "); long beginTime = new Date().getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); CSVReader reader = new CSVReader(new FileReader(CSV_FILE_PATH + fileNumber + ".csv")); String[] nextLine; int count = 0; while ((nextLine = reader.readNext()) != null) { if (count == 0) { } else { // nextLine[] is an array of values from the line String userId = nextLine[0]; String imsi = nextLine[1]; String msisdn = nextLine[2]; String homeCity = nextLine[3]; String homeCountry = nextLine[4]; Date inureTime = sdf.parse(nextLine[5]); Date expireTime = sdf.parse(nextLine[6]); String testFlag = nextLine[7]; cache.put( userId, new UserBaseInfo( userId, imsi, msisdn, homeCity, homeCountry, inureTime, expireTime, testFlag)); } count++; } long endTime = new Date().getTime(); long loadTime = endTime - beginTime; totalLoadTime += loadTime; int number = count - 1; totalCsvRowNumber += number; System.out.println("####### " + fileNumber + ".csv 中记录数是 " + number); System.out.println("####### userinfo 缓存中的记录数是 " + cache.size()); System.out.println("####### 结束 把 " + fileNumber + ".csv 数据放入缓存,耗时 " + loadTime + " ms"); }
public List<RoadLink> loadToDatabase(String mif, String mid, boolean saved) throws IOException { CSVReader reader = new CSVReader(new FileReader(mid), ',', '"', 0); String[] row; List<RoadLink> links = new ArrayList(); while ((row = reader.readNext()) != null) { RoadLink link = new RoadLink(); link.no = Integer.valueOf(row[0]); link.fromNode = Integer.valueOf(row[1]); link.toNode = Integer.valueOf(row[2]); link.name = row[3]; link.length = Double.valueOf(row[4]); link.ft = row[5]; link.tf = row[6]; link.combine = row[7]; link.overhead = row[8]; link.xmin = Double.valueOf(row[9]); link.ymin = Double.valueOf(row[10]); link.xmax = Double.valueOf(row[11]); link.ymax = Double.valueOf(row[12]); links.add(link); // logger.debug("{}",link); } Charset charset = Charset.forName("UTF-8"); BufferedReader mifReader = Files.newBufferedReader(Paths.get(mif), charset); String line; int index = -1; int pos = -1; int segs = -1; Point2D prePoint = null; while ((line = mifReader.readLine()) != null) { List<String> cols = Lists.newArrayList( Splitter.on(CharMatcher.BREAKING_WHITESPACE) .trimResults() .omitEmptyStrings() .split(line)); // logger.debug("{}", line); if (cols.get(0).compareTo("PLINE") == 0) { index++; segs = Integer.valueOf(cols.get(1)); pos = 0; prePoint = null; } else if (cols.get(0).compareTo("LINE") == 0) { index++; RoadLink link = links.get(index); Segment seg = new Segment(); seg.start = new Point2D.Double(Double.valueOf(cols.get(1)), Double.valueOf(cols.get(2))); seg.end = new Point2D.Double(Double.valueOf(cols.get(3)), Double.valueOf(cols.get(4))); seg.length = seg.start.distance(seg.end); link.segments.add(seg); } else if (index >= 0) { if (prePoint != null) { prePoint = new Point2D.Double(Double.valueOf(cols.get(0)), Double.valueOf(cols.get(1))); } else { Segment seg = new Segment(); seg.start = prePoint; prePoint = new Point2D.Double(Double.valueOf(cols.get(0)), Double.valueOf(cols.get(1))); seg.end = prePoint; seg.length = seg.start.distance(seg.end); RoadLink link = links.get(index); link.segments.add(seg); } } } if (saved) { for (final RoadLink link : links) { logger.debug("---->{}", link); jdbcTemplate.update( sql1, link.no, link.fromNode, link.toNode, link.name, link.length, link.ft, link.tf, link.combine, link.overhead, link.xmin, link.ymin, link.xmax, link.ymax); // jdbcTemplate.batchUpdate(sql2, // new BatchPreparedStatementSetter() { // @Override // public int getBatchSize() { // return link.segments.size(); // } // // @Override // public void setValues(PreparedStatement ps, int i) // throws SQLException { // Point2D segment = link.segments.get(i); // ps.setInt(1, link.no); // ps.setInt(2, i); // ps.setDouble(3, segment.getX()); // ps.setDouble(4, segment.getY()); // } // }); } } // logger.debug("=>{},{}", links.size(), index); return links; }
public List<RoadLink> loadToDatabase(String mif, String mid) throws IOException { logger.debug("loading {}...", mid); CSVReader reader = new CSVReader(new FileReader(mid), ',', '"', 0); String[] row; List<RoadLink> links = new ArrayList(); while ((row = reader.readNext()) != null) { RoadLink link = new RoadLink(); link.no = Integer.valueOf(row[0]); link.fromNode = Integer.valueOf(row[1]); link.toNode = Integer.valueOf(row[2]); link.name = row[3]; link.length = Double.valueOf(row[4]); link.ft = row[5]; link.tf = row[6]; link.combine = row[7]; link.overhead = row[8]; link.xmin = Double.valueOf(row[9]); link.ymin = Double.valueOf(row[10]); link.xmax = Double.valueOf(row[11]); link.ymax = Double.valueOf(row[12]); links.add(link); roadNet.put(link.no, link); } for (final RoadLink link : links) { jdbcTemplate.update( sql1, link.no, link.fromNode, link.toNode, link.name, link.length, link.ft, link.tf, link.combine, link.overhead, link.xmin, link.ymin, link.xmax, link.ymax); } Charset charset = Charset.forName("UTF-8"); BufferedReader mifReader = Files.newBufferedReader(Paths.get(mif), charset); String line; int index = -1; int pos = -1; int segs = -1; Point2D prePoint = null; while ((line = mifReader.readLine()) != null) { List<String> cols = Lists.newArrayList( Splitter.on(CharMatcher.BREAKING_WHITESPACE) .trimResults() .omitEmptyStrings() .split(line)); // logger.debug("{}", line); if (cols.get(0).compareTo("PLINE") == 0) { index++; segs = Integer.valueOf(cols.get(1)); pos = 0; prePoint = null; } else if (cols.get(0).compareTo("LINE") == 0) { index++; RoadLink link = links.get(index); // link.segments.add(new Point2D.Double(Double.valueOf(cols.get(1)), // Double.valueOf(cols.get(2)))); // link.segments.add(new Point2D.Double(Double.valueOf(cols.get(3)), // Double.valueOf(cols.get(4)))); Segment seg = new Segment(); seg.start = new Point2D.Double(Double.valueOf(cols.get(1)), Double.valueOf(cols.get(2))); seg.end = new Point2D.Double(Double.valueOf(cols.get(3)), Double.valueOf(cols.get(4))); seg.length = seg.start.distance(seg.end); link.segments.add(seg); } else if (index >= 0) { if (prePoint == null) { prePoint = new Point2D.Double(Double.valueOf(cols.get(0)), Double.valueOf(cols.get(1))); } else { Segment seg = new Segment(); seg.start = prePoint; prePoint = new Point2D.Double(Double.valueOf(cols.get(0)), Double.valueOf(cols.get(1))); seg.end = prePoint; seg.length = seg.start.distance(seg.end); RoadLink link = links.get(index); link.segments.add(seg); } // RoadLink link = links.get(index); // link.segments.add(new Point2D.Double(Double.valueOf(cols.get(0)), // Double.valueOf(cols.get(1)))); } } return links; }
// read all from cvs file private List<String[]> readCVSfile(String filename) throws IOException { CSVReader reader = new CSVReader(new FileReader(filename)); // ratings = reader.readAll(); List<String[]> list = reader.readAll(); return list; }
private static Stream<String[]> fileToStream(String filePath) throws FileNotFoundException { com.opencsv.CSVReader reader = new com.opencsv.CSVReader(new FileReader(filePath)); Iterator<String[]> iterator = reader.iterator(); return StreamSupport.stream( Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false); }
public void close() throws IOException { rdr.close(); }