List of usage examples for org.apache.commons.io IOUtils readLines
public static List readLines(InputStream input, String encoding) throws IOException
InputStream
as a list of Strings, one entry per line, using the specified character encoding. From source file:com.opengamma.component.AbstractComponentConfigLoader.java
/** * Reads lines from the resource.// w ww . jav a2s. c o m * * @param resource the resource to read, not null * @return the lines, not null */ protected List<String> readLines(Resource resource) { try { return IOUtils.readLines(resource.getInputStream(), "UTF8"); } catch (IOException ex) { throw new OpenGammaRuntimeException("Unable to read resource: " + resource, ex); } }
From source file:edu.smc.mediacommons.panels.SecurityPanel.java
public SecurityPanel() { setLayout(null);//from w w w. j a va 2s. co m FILE_CHOOSER = new JFileChooser(); JButton openFile = Utils.createButton("Open File", 10, 20, 100, 30, null); add(openFile); JButton saveFile = Utils.createButton("Save File", 110, 20, 100, 30, null); add(saveFile); JButton decrypt = Utils.createButton("Decrypt", 210, 20, 100, 30, null); add(decrypt); JButton encrypt = Utils.createButton("Encrypt", 310, 20, 100, 30, null); add(encrypt); JTextField path = Utils.createTextField(10, 30, 300, 20); path.setText("No file selected"); final JTextArea viewer = new JTextArea(); JScrollPane pastePane = new JScrollPane(viewer); pastePane.setBounds(15, 60, 400, 200); add(pastePane); openFile.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (FILE_CHOOSER.showOpenDialog(getParent()) == JFileChooser.APPROVE_OPTION) { File toRead = FILE_CHOOSER.getSelectedFile(); if (toRead == null) { JOptionPane.showMessageDialog(getParent(), "The input file does not exist!", "Opening Failed...", JOptionPane.WARNING_MESSAGE); } else { try { List<String> lines = IOUtils.readLines(new FileInputStream(toRead), "UTF-8"); viewer.setText(""); for (String line : lines) { viewer.append(line); } } catch (IOException ex) { } } } } }); saveFile.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (FILE_CHOOSER.showSaveDialog(getParent()) == JFileChooser.APPROVE_OPTION) { File toWrite = FILE_CHOOSER.getSelectedFile(); Utils.writeToFile(viewer.getText(), toWrite); JOptionPane.showMessageDialog(getParent(), "The file has now been saved to\n" + toWrite.getPath()); } } }); encrypt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String password = Utils.getPasswordInput(getParent()); if (password != null) { try { BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor(); basicTextEncryptor.setPassword(password); String text = basicTextEncryptor.encrypt(viewer.getText()); viewer.setText(text); } catch (Exception ex) { JOptionPane.showMessageDialog(getParent(), "Could not encrypt the text, an unexpected error occurred.", "Encryption Failed...", JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showMessageDialog(getParent(), "Could not encrypt the text, as no\npassword has been specified.", "Encryption Failed...", JOptionPane.WARNING_MESSAGE); } } }); decrypt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String password = Utils.getPasswordInput(getParent()); if (password != null) { try { BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor(); basicTextEncryptor.setPassword(password); String text = basicTextEncryptor.decrypt(viewer.getText()); viewer.setText(text); } catch (Exception ex) { JOptionPane.showMessageDialog(getParent(), "Could not decrypt the text, an unexpected error occurred.", "Decryption Failed...", JOptionPane.WARNING_MESSAGE); } } else { JOptionPane.showMessageDialog(getParent(), "Could not decrypt the text, as no\npassword has been specified.", "Decryption Failed...", JOptionPane.WARNING_MESSAGE); } } }); }
From source file:com.opengamma.master.region.impl.UnLocodeRegionFileReader.java
private Set<String> parseRequired() { InputStream stream = getClass().getResourceAsStream(LOAD_RESOURCE); if (stream == null) { throw new OpenGammaRuntimeException("Unable to find UnLocode.txt defining the UN/LOCODEs"); }//from w w w . j av a 2 s .c o m try { Set<String> lines = new HashSet<String>(IOUtils.readLines(stream, "UTF-8")); Set<String> required = new HashSet<String>(); for (String line : lines) { line = StringUtils.trimToNull(line); if (line != null) { required.add(line); } } return required; } catch (Exception ex) { throw new OpenGammaRuntimeException("Unable to read UnLocode.txt defining the UN/LOCODEs"); } finally { IOUtils.closeQuietly(stream); } }
From source file:de.tudarmstadt.ukp.dkpro.tc.examples.io.SimpleDkproTCReader.java
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); // read file with gold labels golds = new ArrayList<String>(); try {/*from w w w . j av a 2s . c o m*/ URL resourceUrl = ResourceUtils.resolveLocation(goldLabelFile, this, context); InputStream is = resourceUrl.openStream(); for (String label : IOUtils.readLines(is, encoding)) { golds.add(label); } is.close(); } catch (IOException e) { throw new ResourceInitializationException(e); } texts = new ArrayList<String>(); try { URL resourceUrl = ResourceUtils.resolveLocation(sentencesFile, this, context); InputStream is = resourceUrl.openStream(); for (String sentence : IOUtils.readLines(is, encoding)) { texts.add(sentence); } is.close(); } catch (IOException e) { throw new ResourceInitializationException(e); } offset = 0; }
From source file:eu.annocultor.data.destinations.SolrServer.java
public SolrServer(String datasetId, Environment environment, String datasetModifier, String objectType, String propertyType, String... comment) { super(datasetId, environment, datasetModifier, objectType, propertyType, "txt", comment); solrUrl = comment[0];// w ww . j av a 2s .c o m try { String location = comment[1]; InputStream is = location.startsWith("http://") ? new URL(location).openStream() : new FileInputStream(location); for (String line : IOUtils.readLines(is, "UTF-8")) { String trimmedLine = StringUtils.trim(line); if (trimmedLine.startsWith("<field name=") || trimmedLine.startsWith("<dynamicField name=")) { final FieldDefinition fieldDefinition = new FieldDefinition(line); fieldDefinitions.put(fieldDefinition.getName(), fieldDefinition); } } } catch (IOException e) { e.printStackTrace(); } }
From source file:com.github.frapontillo.pulse.crowd.sentiment.sentiwordnet.MultiWordNet.java
/** * Loads a dictionary for a given language. * * @param language A {@link String} representing the language. * * @return a {@link HashMap}<String, String[]> where keys are lemmas and values are synsets. *///w w w. j a v a 2 s.co m private HashMap<String, String[]> loadDictionary(String language) { HashMap<String, String[]> dict = new HashMap<>(); InputStream model = getClass().getClassLoader().getResourceAsStream(language + "_index"); if (model != null) { try { List<String> lines = IOUtils.readLines(model, Charset.forName("UTF-8")); lines.forEach(s -> { String[] components = mainDivider.split(s); if (components.length == 2) { String lemma = components[0]; String[] synsets = subDivider.split(components[1]); dict.put(lemma, synsets); } }); } catch (IOException e) { e.printStackTrace(); } } return dict; }
From source file:io.github.retz.cli.CommandRun.java
@Override public int handle(ClientCLIConfig fileConfig, boolean verbose) throws Throwable { Properties envProps = SubCommand.parseKeyValuePairs(envs); if (ports < 0 || 1000 < ports) { LOG.error("--ports must be within 0 to 1000: {} given.", ports); return -1; }/*from w w w. ja v a 2s. co m*/ if ("-".equals(remoteCmd)) { List<String> lines = IOUtils.readLines(System.in, StandardCharsets.UTF_8); remoteCmd = String.join("\n", lines); } Job job = new Job(appName, remoteCmd, envProps, cpu, mem, disk, gpu, ports); job.setPriority(priority); job.setName(name); job.addTags(tags); job.setAttributes(attributes); if (verbose) { LOG.info("Job: {}", job); } try (Client webClient = Client.newBuilder(fileConfig.getUri()) .setAuthenticator(fileConfig.getAuthenticator()).checkCert(!fileConfig.insecure()) .setVerboseLog(verbose).build()) { if (verbose) { LOG.info("Sending job {} to App {}", job.cmd(), job.appid()); } Response res = webClient.schedule(job); if (!(res instanceof ScheduleResponse)) { LOG.error(res.status()); return -1; } Date start = Calendar.getInstance().getTime(); Callable<Boolean> timedout; if (timeout > 0) { LOG.info("Timeout = {} minutes", timeout); timedout = () -> { Date now = Calendar.getInstance().getTime(); long diff = now.getTime() - start.getTime(); return diff / 1000 > timeout * 60; }; } else { timedout = () -> false; } Job scheduled = ((ScheduleResponse) res).job(); if (verbose) { LOG.info("job {} scheduled", scheduled.id(), scheduled.state()); } try { Job running = ClientHelper.waitForStart(scheduled, webClient, timedout); if (verbose) { LOG.info("job {} started: {}", running.id(), running.state()); } LOG.info("============ stdout of job {} sandbox start ===========", running.id()); Optional<Job> finished = ClientHelper.getWholeFileWithTerminator(webClient, running.id(), "stdout", true, System.out, timedout); LOG.info("============ stdout of job {} sandbox end ===========", running.id()); if (stderr) { LOG.info("============ stderr of job {} sandbox start ===========", running.id()); Optional<Job> j = ClientHelper.getWholeFileWithTerminator(webClient, running.id(), "stderr", false, System.err, null); LOG.info("============ stderr of job {} sandbox end ===========", running.id()); } if (finished.isPresent()) { LOG.debug(finished.get().toString()); LOG.info("Job(id={}, cmd='{}') finished in {} seconds. status: {}", running.id(), job.cmd(), TimestampHelper.diffMillisec(finished.get().finished(), finished.get().started()) / 1000.0, finished.get().state()); return finished.get().result(); } else { LOG.error("Failed to fetch last state of job id={}", running.id()); } } catch (TimeoutException e) { webClient.kill(scheduled.id()); LOG.error("Job(id={}) has been killed due to timeout after {} minute(s)", scheduled.id(), timeout); } } return -1; }
From source file:com.alibaba.zonda.logger.server.LogMonitor.java
private void tail() throws IOException { logger.debug("current log file: {}", logFile); logger.debug("current line no: {}", lineNo); if (null != logFile && null != stream) { List<String> logs = IOUtils.readLines(stream, "utf-8"); for (String log : logs) { if (!log.trim().equals("") && logFilter.match(log)) { this.out.write(log.getBytes()); this.out.write("\n".getBytes()); }//from www. j av a 2 s . co m } this.lineNo += logs.size(); this.out.flush(); } }
From source file:de.uni_freiburg.informatik.ultimate.licence_manager.authors.SvnAuthorProvider.java
private boolean isUnderVersionControl(String absolutePath) { final ProcessBuilder pbuilder = new ProcessBuilder("svn", "info", absolutePath); try {//from w w w .j a v a 2 s . c om final Process process = pbuilder.start(); if (process.waitFor(1000, TimeUnit.MILLISECONDS)) { final List<String> lines = IOUtils.readLines(process.getInputStream(), Charset.defaultCharset()); if (lines == null || lines.isEmpty()) { return false; } return !lines.get(0).contains("is not a working copy"); } } catch (IOException | InterruptedException e) { System.err.println("Could not find 'svn' executable, disabling author provider"); return false; } return false; }
From source file:com.github.frapontillo.pulse.crowd.lemmatize.morphit.MorphITLemmatizer.java
/** * Build the MorphIT dictionary as <key:(word), value:(morphit-tag,lemma)>. * Values are read from the resource file "morphit". * * @return A {@link HashMap} where the key is a MorphIT tag {@link String} and values are {@link * List} of {@link String} arrays where each element of the list represent a different * lemmatization form for the key; the array specifies the POS tag in the first position and the * lemma in the second one.//from w w w. j av a 2 s .c om */ public HashMap<String, List<String[]>> getMorphITDictionary() { if (morphITDictionary == null) { InputStream dictStream = MorphITLemmatizer.class.getClassLoader().getResourceAsStream("morphit"); morphITDictionary = new HashMap<>(); try { List<String> mapLines = IOUtils.readLines(dictStream, Charset.forName("UTF-8")); mapLines.forEach(s -> { // for each line, split using spaces String[] values = spacePattern.split(s); if (values.length == 3) { // the key is made of first and third token // the value is the second token List<String[]> candidates = morphITDictionary.get(values[0]); if (candidates == null) { candidates = new ArrayList<>(); } candidates.add(new String[] { values[2], values[1] }); morphITDictionary.put(values[0], candidates); } }); } catch (IOException e) { e.printStackTrace(); } } return morphITDictionary; }