List of usage examples for org.apache.commons.io FileUtils readLines
public static List readLines(File file) throws IOException
From source file:com.mythesis.userbehaviouranalysis.Utils.java
/** * a method that reads a txt file with the necessary parameters * @param input the file that includes the necessary parameters *//* w w w . j av a2 s . c o m*/ public void readInput(File input) { FileInputStream inputStream = null; Scanner sc = null; wordvectors = new HashMap<>(); crawlerOutputPaths = new HashMap<>(); HashMap<String, String> wordvectorsPaths = new HashMap<>(); try { inputStream = new FileInputStream(input); sc = new Scanner(inputStream); if (sc.hasNextLine()) { int numProfiles = Integer.parseInt(sc.nextLine().split(";")[1].trim().toLowerCase()); int j = 0; while (j < numProfiles) { String profile = ""; if (sc.hasNextLine()) { profile = sc.nextLine().split(";")[1].trim(); } if (sc.hasNextLine()) { String path = sc.nextLine().split(";")[1].trim(); crawlerOutputPaths.put(profile, path); } if (sc.hasNextLine()) { String path = sc.nextLine().split(";")[1].trim(); wordvectorsPaths.put(profile, path); } j++; } } for (String profile : wordvectorsPaths.keySet()) { File file = new File(wordvectorsPaths.get(profile)); try { List<String> wordvector = FileUtils.readLines(file); wordvectors.put(profile, (ArrayList<String>) wordvector); } catch (IOException ex) { Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); } } } catch (FileNotFoundException ex) { Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); } } if (sc != null) { sc.close(); } } }
From source file:eu.udig.catalog.jgrass.core.JGTtmsGeoResource.java
private JGTtmsProperties getTmsProperties(File tmsPropertiesFile) { JGTtmsProperties tmsProperties = new JGTtmsProperties(); List<String> fileLines = new ArrayList<String>(); try {/*www . ja v a 2 s. c om*/ fileLines = FileUtils.readLines(tmsPropertiesFile); } catch (IOException e1) { msg = e1; e1.printStackTrace(); } for (String line : fileLines) { line = line.trim(); if (line.length() == 0) { continue; } int split = line.indexOf('='); if (split != -1) { String value = line.substring(split + 1).trim(); if (line.startsWith("url")) { int indexOfZ = value.indexOf("ZZZ"); tmsProperties.HOST_NAME = value.substring(0, indexOfZ); tmsProperties.tilePart = value.substring(indexOfZ); if (value.startsWith("http")) { // remove http tmsProperties.HOST_NAME = tmsProperties.HOST_NAME.substring(7); } else { tmsProperties.PROTOCOL = "file"; tmsProperties.HOST_NAME = tmsPropertiesFile.getParent() + File.separator + tmsProperties.HOST_NAME; tmsProperties.isFile = true; } } if (line.startsWith("minzoom")) { try { tmsProperties.ZOOM_MIN = Byte.valueOf(value); } catch (Exception e) { // use default: handle exception } } if (line.startsWith("maxzoom")) { try { tmsProperties.ZOOM_MAX = Byte.valueOf(value); } catch (Exception e) { // use default: handle exception } } if (line.startsWith("center")) { try { String[] coord = value.split("\\s+"); //$NON-NLS-1$ double x = Double.parseDouble(coord[0]); double y = Double.parseDouble(coord[1]); tmsProperties.centerPoint = new Coordinate(x, y); } catch (NumberFormatException e) { // use default } } if (line.startsWith("type")) { if (value.equals(JGTtmsProperties.TILESCHEMA.tms.toString())) { tmsProperties.type = JGTtmsProperties.TILESCHEMA.tms; } } } } return tmsProperties; }
From source file:mattmc.mankini.commands.CommandQuote.java
private void pickRandomQuote(MessageEvent<PircBotX> event) { try {//from w ww . j ava2 s.co m if (!file.exists()) { System.out.println(file.createNewFile()); } FileReader fw = new FileReader(file); BufferedReader reader = new BufferedReader(fw); String line; Random random = new Random(); int i = 0; while ((line = reader.readLine()) != null) { if (line != null) { i++; } } i = random.nextInt(i - 1); if (FileUtils.readLines(file).get(i) != null) { MessageSending.sendNormalMessage(FileUtils.readLines(file).get(i).toString(), event); } } catch (IOException e) { e.printStackTrace(); } }
From source file:com.splunk.shuttl.archiver.metastore.FlatFileStorage.java
private String getFirstLineFromFile(File file) throws IOException { return FileUtils.readLines(file).get(0); }
From source file:com.gargoylesoftware.htmlunit.general.HostTestsTest.java
private void collectionObjectNames(final File dir, final Set<String> set) throws IOException { for (final File file : dir.listFiles()) { if (file.isDirectory() && !".svn".equals(file.getName())) { collectionObjectNames(file, set); } else if (file.getName().endsWith(".java")) { final List<String> lines = FileUtils.readLines(file); for (final String line : lines) { final Matcher matcher = pattern_.matcher(line); while (matcher.find()) { set.add(matcher.group(1)); }//ww w.j a v a2 s . com } } } }
From source file:com.beaconhill.yqd.ProcessorCommand.java
List<String> readSymbolFile(String fileName) { List<String> lines = null; if (fileName != null && fileName.length() > 0) { try {//from w w w . j a va 2 s . co m lines = FileUtils.readLines(new File(fileName)); } catch (IOException ioEx) { ioEx.printStackTrace(); } return lines; } return lines; }
From source file:de.tudarmstadt.ukp.dkpro.tc.examples.io.STSReader.java
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); fileOffset = 0;//from w w w . j a v a 2 s . c o m texts1 = new ArrayList<String>(); texts2 = new ArrayList<String>(); golds = new ArrayList<Double>(); try { for (String line : FileUtils.readLines(inputFile)) { String parts[] = line.split("\t"); if (parts.length != 2) { throw new ResourceInitializationException(new Throwable("Wrong file format: " + line)); } texts1.add(parts[0]); texts2.add(parts[1]); } for (String line : FileUtils.readLines(goldFile)) { try { double goldValue = Double.parseDouble(line); golds.add(goldValue); } catch (NumberFormatException e) { throw new ResourceInitializationException(e); } } if (texts1.size() != golds.size()) { throw new ResourceInitializationException( new Throwable("Size of text list does not match size of gold list.")); } } catch (IOException e) { throw new ResourceInitializationException(e); } }
From source file:de.joinout.criztovyl.tools.files.MultiFileGrep.java
/** * Greps from all files and returns a {@link Map} with the matched file as a key and the matching lines as value. * @return a {@link HashMap} with a {@link Path} as key and a {@link String} {@link List} as value. *//*from ww w . jav a 2 s .c o m*/ public HashMap<Path, ArrayList<String>> grep() { //Create map HashMap<Path, ArrayList<String>> map = new HashMap<>(); //Iterate over paths for (final Path file : paths) { try { //Try reading all lines from file //Iterate over lines for (final String line : FileUtils.readLines(file.getFile())) { //Check if line matches if (line.matches(regex)) //Add file to map if not present if (!map.containsKey(file)) map.put(file, new ArrayList<String>()); //Add line map.get(file).add(line); } } catch (final FileNotFoundException e) { // Catch if file not found logger.warn("File {} not found!", file); logger.debug(e); } catch (final IOException e) { // Catch general IOException logger.error("IOException!", e); } } //Return return map; }
From source file:es.uvigo.ei.sing.adops.datatypes.BatchProjectOutput.java
private void load() throws IOException { final File statusFile = new File(this.project.getFolder(), BatchProjectOutput.STATUS_FILE); if (!statusFile.exists()) return;/* ww w . j a va2 s .c om*/ synchronized (this.projects) { for (String line : FileUtils.readLines(statusFile)) { final String projectName = line.substring(0, line.indexOf('=')); final File projectDir = new File(this.project.getFolder(), projectName); final String status = line.substring(line.indexOf('=') + 1); if (projectDir.exists()) { final Project project = new Project(projectDir); this.projects.put(this.project.getFastaFiles()[this.getProjectIndex(project)], project); if (status.startsWith("Executed")) { this.projectOutputs.put(project, project.getExperiments().get(0).getResult()); } else if (status.startsWith("Error")) { this.projectErrors.put(project, new Exception(status.substring(6, status.length() - 1))); } } } } }
From source file:ca.weblite.xmlvm.VtableHelper.java
/** * Fixes the vTable references for a .c (or .m) source file. * @param file// w w w . j a v a2 s . c o m * @param cFilesDirectory * @throws IOException */ public static void fixVtableReferences(Project project, File file, File cFilesDirectory) throws IOException { if (file.isDirectory()) { for (File child : file.listFiles()) { fixVtableReferences(project, child, cFilesDirectory); } return; } if (file.getName().endsWith(".m") || file.getName().endsWith(".c")) { } else { return; } List<String> lines = FileUtils.readLines(file); List<String> out = new ArrayList<String>(); // Pattern to match vtable error comments. group(1) will contain function name String pattern = "^//([a-zA-Z][a-zA-Z0-9_]+)\\[-1]$"; Pattern regex = Pattern.compile(pattern); // Pattern to match class name on a line that has a missing vtable entry // group(1) will contain the c prefix for the class name String clsPattern = "\\(\\(([a-zA-Z][a-zA-Z0-9_]*)\\*\\) _r\\d+\\.o\\)->tib->vtable"; Pattern clsRegex = Pattern.compile(clsPattern); String vtableLinePattern = "\\(\\*\\(.*\\)->tib->vtable\\[-1\\]\\)"; Pattern vtableLineRegex = Pattern.compile(vtableLinePattern); boolean inErrorSection = false; String functionName = null; for (String line : lines) { if (line.startsWith("XMLVM_ERROR(\"Missing @vtable-index\"")) { inErrorSection = true; line = "//" + line; } else if (inErrorSection) { if (functionName == null) { Matcher m = regex.matcher(line.trim()); if (m.find()) { functionName = m.group(1); } } else { if (line.indexOf("->tib->vtable[-1]") > 0) { //line = line.replaceAll("->tib->vtable\\[-1\\]", "->tib->vtable["+) //String vtableIdxPattern = "XMLVM_VTABLE_IDX_"+functionName; boolean foundVtableEntry = false; // Need to find the class name because we need to check // if the class has a vtable entry defined for this method. Matcher m = clsRegex.matcher(line); if (m.find()) { String clsName = m.group(1); String mangledMethodName = functionName.substring(clsName.length() + 1); File clsHeaderFile = new File(cFilesDirectory, clsName + ".h"); if (clsHeaderFile.exists()) { Path srcPaths = new Path(project); srcPaths.add(new Path(project, file.getAbsolutePath())); srcPaths.add(new Path(project, cFilesDirectory.getAbsolutePath())); String resolvedFunction = resolveVirtualMethod(project, srcPaths, clsHeaderFile, mangledMethodName); if (resolvedFunction.startsWith("XMLVM_VTABLE_IDX")) { line = line.replaceAll("->tib->vtable\\[-1\\]", "->tib->vtable[" + resolvedFunction + "]"); foundVtableEntry = true; } else { line = line.replaceAll(vtableLinePattern, resolvedFunction); } } else { throw new RuntimeException("No class header found for class " + clsName); } } else { throw new RuntimeException( "Found missing vtable entry but regex failed to find class name: " + line); } functionName = null; inErrorSection = false; } } } out.add(line); } FileUtils.writeLines(file, out); }