List of usage examples for org.apache.commons.io IOUtils readLines
public static List readLines(Reader input) throws IOException
Reader
as a list of Strings, one entry per line. From source file:com.github.genium_framework.appium.support.command.CommandManager.java
/** * Execute a command on the operating system using Java's built-in Process * class/* w w w .ja v a 2 s .c om*/ * * @param command A string array representation of the command to execute. * @param getOutput Whether or not to get the output/error streams of the * process you forked. This is helpful for debugging reasons. * @return A string representation of output/error streams of the process. */ public static String executeCommandUsingJavaRuntime(String[] command, boolean getOutput) { String output = ""; try { Process p = Runtime.getRuntime().exec(command); // read the output from the command if requested by the user if (getOutput) { List<String> outErrStr = IOUtils.readLines(p.getInputStream()); output = StringUtils.toString(outErrStr.toArray(new String[outErrStr.size()]), "\n"); } } catch (Exception ex) { LOGGER.log(Level.SEVERE, "An exception was thrown while executing the command (" + command + ")", ex); } return output; }
From source file:lshw.types.jaxb.test.JaxbXmlParserTest.java
@Before public void setUp() throws IOException, JAXBException { StringBuilder sb = new StringBuilder(); try (InputStream is = getDefaultClassLoader().getResourceAsStream("example-network-output.xml")) { for (Object line : IOUtils.readLines(is)) { sb.append(line).append("\n"); }/*from w w w . j av a 2 s. c o m*/ } xmlText = sb.toString(); parser = new JaxbXmlParser<>(Nodes.class); }
From source file:gr.xe.conf.ConfReader.java
/** * Reads a legacy .conf Perl file/*from ww w .jav a2 s. c o m*/ * @param in the file * @throws IOException something was wrong during reading. */ public void readFile(InputStream in) throws IOException { List<String> lines = IOUtils.readLines(in); for (String line : lines) { if (StringUtils.isBlank(line)) { continue; } boolean ok = process(line); if (!ok) { logger.warn("Unknown line {}", line); } } }
From source file:com.firewallid.classification.TrainingData.java
public JavaPairRDD<String, String> load(JavaSparkContext jsc) throws IOException { int partitions = conf.getInt(PARTITIONS, 48); String docDelimiter = StringEscapeUtils.unescapeJava(conf.get(DOC_DELIMITER)); List<String> resource = IOUtils .readLines(getClass().getClassLoader().getResourceAsStream(conf.get(TRAININGDATA_SOURCE))); Broadcast<List<String>> broadcastRes = jsc.broadcast(resource); JavaPairRDD<String, String> doc = jsc.parallelize(broadcastRes.value()) .persist(StorageLevel.MEMORY_AND_DISK_SER_2()).repartition(partitions) .filter(line -> line.split(docDelimiter).length >= 2) .mapToPair(line -> new Tuple2<>(line.split(docDelimiter)[0], line.split(docDelimiter)[1])); return doc;//from w w w .j av a 2 s . c o m }
From source file:module.geography.domain.task.CountryCodesImport.java
@Override public void runTask() { InputStream stream = getClass().getResourceAsStream(ISO3166_FILE); try {//from w w w . j ava2 s . c o m List<String> lines = IOUtils.readLines(stream); Planet planet = Magrathea.buildEarth(); for (String line : lines) { String[] parts = line.split(";"); String shortCode = parts[0]; String longCode = parts[1]; String numericCode = parts[2]; String countryNameEn = parts[3]; String countryNamePt = parts.length > 5 && Strings.isNullOrEmpty(parts[5]) ? null : parts[5]; String nationalityEn = parts.length > 6 && Strings.isNullOrEmpty(parts[6]) ? null : parts[6]; String nationalityPt = parts.length > 7 && Strings.isNullOrEmpty(parts[7]) ? null : parts[7]; Country country = planet.getChildByAcronym(longCode); if (country == null) { country = new Country(planet, shortCode, longCode, Integer.parseInt(numericCode), makeName(countryNamePt, countryNameEn), makeName(nationalityPt, nationalityEn), AddressPrinter.class); } else { country.update(planet, shortCode, longCode, Integer.parseInt(numericCode), makeName(countryNamePt, countryNameEn), makeName(nationalityPt, nationalityEn), AddressPrinter.class); } } } catch (IOException e) { e.printStackTrace(); } finally { try { stream.close(); } catch (IOException e) { } } }
From source file:com.firewalld.sentimentanalysis.IDSentiWordNet.java
private Map<String, Double> createDictionary() throws IOException { Map<String, Double> dict = IOUtils .readLines(getClass().getClassLoader().getResourceAsStream(firewallConf.get(SWN_FILE))) .parallelStream()//from w ww .ja v a 2s .c o m /* If it's a comment, skip this line */ .filter(line -> !line.trim().startsWith("#")).flatMap(line -> { String[] data = line.split("\t"); String wordTypeMarker = data[0]; // Example line: // POS ID PosS NegS SynsetTerm#sensenumber Desc // a 00009618 0.5 0.25 spartan#4 austere#3 ascetical#2 ascetic#2 practicing great self-denial;...etc // Is it a valid line? Otherwise, through exception. if (data.length != 6) { throw new IllegalArgumentException( String.format("Incorrect tabulation format in file, line: %s", line)); } // Calculate synset score as score = PosS - NegS Double synsetScore = Double.parseDouble(data[2]) - Double.parseDouble(data[3]); // Get all Synset terms String[] synTermsSplit = data[4].split(" "); // Go through all terms of current synset. Stream<Tuple2<String, Tuple2<Double, Double>>> synSets = Arrays.asList(synTermsSplit) .parallelStream().map(synTermSplit -> { // Get synterm and synterm rank String[] synTermAndRank = synTermSplit.split("#"); String synTerm = synTermAndRank[0] + "#" + wordTypeMarker; double synTermRank = Double.parseDouble(synTermAndRank[1]); // What we get here is a (term, (rank, score)) return new Tuple2<>(synTerm, new Tuple2<>(synTermRank, synsetScore)); }); return synSets; }) // What we get here is a map of the type: // term -> {score of synset#1, score of synset#2...} .collect(Collectors.groupingBy(synSet -> synSet._1, Collectors.mapping(synSet -> synSet._2, Collectors.toList()))) .entrySet().parallelStream().map(synSet -> { String word = synSet.getKey(); List<Tuple2<Double, Double>> synSetScoreList = synSet.getValue(); // Calculate weighted average. Weigh the synsets according to // their rank. // Score= 1/2*first + 1/3*second + 1/4*third ..... etc. // Sum = 1/1 + 1/2 + 1/3 ... Tuple2<Double, Double> scoreSum = synSetScoreList.parallelStream() .reduce(new Tuple2<>(0.0, 0.0), (s1, s2) -> new Tuple2<>( ((s1._1 == 0.0) ? 0.0 : s1._2 / s1._1) + ((s2._1 == 0.0) ? 0.0 : s2._2 / s2._1), ((s1._1 == 0.0) ? 0.0 : 1 / s1._1) + ((s2._1 == 0.0) ? 0.0 : 1 / s2._1))); double score = scoreSum._1 / scoreSum._2; return new Tuple2<>(word, score); }).collect(Collectors.toMap(synSet -> synSet._1, synSet -> synSet._2)); return dict; }
From source file:com.taobao.diamond.domain.Aggregation.java
@SuppressWarnings("unchecked") public static List<ConfigInfo> parse(String aggregation) { List<ConfigInfo> result = new ArrayList<ConfigInfo>(); StringReader reader = null;//from ww w .jav a 2 s . c o m try { reader = new StringReader(aggregation); List<String> lines = IOUtils.readLines(reader); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (line.startsWith(LINE_HEAD)) { ConfigInfo configInfo = new ConfigInfo(); if (++i < lines.size()) { configInfo.setDataId(lines.get(i)); } if (++i < lines.size()) { configInfo.setGroup(lines.get(i)); } if (++i < lines.size()) { if (!lines.get(i).equals("")) configInfo.setMd5(lines.get(i)); } String content = ""; int innerIndex = 0; while (++i < lines.size()) { line = lines.get(i); if (line.equals(LINE_END)) break; if (innerIndex++ > 0) content += lineSeparator; content += line; } if (!content.equals("")) { configInfo.setContent(content); } result.add(configInfo); } } } catch (IOException e) { return parseAfterException(aggregation); } finally { reader.close(); } return result; }
From source file:com.streamsets.datacollector.restapi.TestAdminResource.java
@Test public void testUpdateAppToken() throws IOException { Response response = target("/v1/system/appToken").request().header("X-Requested-By", "SDC") .post(Entity.text("ola")); Assert.assertEquals(200, response.getStatus()); // read contents of application-token.txt File appTokenFIle = new File(confDir, "application-token.txt"); Assert.assertTrue(appTokenFIle.exists()); List<String> strings = IOUtils.readLines(new FileInputStream(appTokenFIle)); Assert.assertEquals(1, strings.size()); Assert.assertEquals("ola", strings.get(0)); response = target("/v1/system/appToken").request().header("X-Requested-By", "SDC") .post(Entity.text("como esta")); Assert.assertEquals(200, response.getStatus()); // read contents of application-token.txt appTokenFIle = new File(confDir, "application-token.txt"); strings = IOUtils.readLines(new FileInputStream(appTokenFIle)); Assert.assertEquals(1, strings.size()); Assert.assertEquals("como esta", strings.get(0)); }
From source file:com.github.DataLoader.java
public static DataSet loadWatchings() throws IOException { final DataSet ret = new DataSet(); for (final Object o : IOUtils .readLines(DataLoader.class.getClassLoader().getResourceAsStream("data/data.txt"))) { final String line = (String) o; final String[] mainParts = line.trim().split(":"); final String user_id = mainParts[0]; final String repo_id = mainParts[1]; ret.add(new Watching(user_id, repo_id)); }//from www.j av a 2s .c om return ret; }
From source file:com.taobao.tanggong.DataFilePersisterImpl.java
private void loadUris() { if (null != this.dataDirectory) { File url2HashMapFile = new File(this.dataDirectory, "index.txt"); if (url2HashMapFile.exists()) { List<String> dburis = new ArrayList<String>(); InputStream inputStream = null; try { inputStream = new FileInputStream(url2HashMapFile); List<String> uris = IOUtils.readLines(inputStream); if (null != uris) { for (String uri : uris) { dburis.add(uri); }// www . java2s . c om } for (String uri : dburis) { String[] kv = uri.split("="); if (kv.length == 2) { String[] vs = kv[1].split(","); for (String v : vs) { this.uri2HashCodes.put(kv[0], v); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { IOUtils.closeQuietly(inputStream); } } } }