/** * Reads the contents of a file line by line to a List of Strings using the default encoding for the VM. * The file is always closed. * * @param file the file to read, must not be <code>null</code> * @return the list of Strings representing each line in the file, never <code>null</code> * @throws IOException in case of an I/O error * @since Commons IO 1.3 */ public static List readLines(File file) throws IOException { return readLines(file, null); }
/** * Reads the contents of a file line by line to a List of Strings using the default encoding for the VM. * The file is always closed. * * @param file the file to read, must not be {@code null} * @return the list of Strings representing each line in the file, never {@code null} * @throws IOException in case of an I/O error * @since 1.3 * @deprecated 2.5 use {@link #readLines(File, Charset)} instead (and specify the appropriate encoding) */ @Deprecated public static List<String> readLines(final File file) throws IOException { return readLines(file, Charset.defaultCharset()); }
/** * Reads the contents of a file line by line to a List of Strings. The file is always closed. * * @param file the file to read, must not be {@code null} * @param encoding the encoding to use, {@code null} means platform default * @return the list of Strings representing each line in the file, never {@code null} * @throws IOException in case of an I/O error * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported. * @since 1.1 */ public static List<String> readLines(final File file, final String encoding) throws IOException { return readLines(file, Charsets.toCharset(encoding)); }
/** * @return whether there was a file to load */ private boolean load(File dir) { File f = new File(dir, MAP_FILE); if (!f.isFile()) { return false; } if (f.length() == 0) { return true; } idToNumber = new TreeMap<String,Integer>(); try { for (String line : FileUtils.readLines(f)) { int i = line.indexOf(' '); idToNumber.put(line.substring(0, i), Integer.parseInt(line.substring(i + 1))); } } catch (Exception x) { // IOException, IndexOutOfBoundsException, NumberFormatException LOGGER.log(WARNING, "could not read from " + f, x); } return true; }
List<String> lines = FileUtils.readLines(deobfMapFile, MAP_FILE_CHARSET); for (String l : lines) { l = l.trim();
FileUtils.readLines(file, StandardCharsets.UTF_8), whitelistSignaturesFromUserControlledList, blacklistSignaturesFromUserControlledList
private String[] readLines(File filename) throws IOException { return FileUtils .readLines(filename, StandardCharsets.UTF_8) .toArray(new String[0]); }
public Iterable<DockerfileStatement> getStatements() throws IOException { Collection<String> dockerFileContent = FileUtils.readLines(dockerFile); if (dockerFileContent.size() <= 0) { throw new DockerClientException(String.format("Dockerfile %s is empty", dockerFile)); } Collection<Optional<? extends DockerfileStatement>> optionals = Collections2.transform(dockerFileContent, new LineTransformer()); return Optional.presentInstances(optionals); }
private void parse(final File file) throws IOException { final List<String> lines = FileUtils.readLines(file); final List<String> rejectedLines = new ArrayList<String>(); for (String line : lines) { try { parseLine(line); } catch (Exception e) { logger.error("Rejected line \n" + line + "\nReason is: " + e); rejectedLines.add(line); } } if (!rejectedLines.isEmpty()) { writeRejectedLinesToFile(toRejectedFilename(file), rejectedLines); } }
private void processFileMeasures(InputComponent component, File measureFile, SensorContext context) { if (measureFile.exists()) { LOG.debug("Processing " + measureFile.getAbsolutePath()); try { List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name()); int lineNumber = 0; for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processMeasure(component, context, measureFile, lineNumber, line); } } catch (IOException e) { throw new IllegalStateException(e); } } }
private void assertMessageInLog(File pluginLogFile, String expectedLoggingLevel, String loggerName, String expectedLogMessage) throws Exception { List linesInLog = FileUtils.readLines(pluginLogFile, Charset.defaultCharset()); for (Object line : linesInLog) { if (((String) line).matches(String.format("^.*%s\\s+\\[%s\\] %s:.* - %s$", expectedLoggingLevel, Thread.currentThread().getName(), loggerName, expectedLogMessage))) { return; } } fail(String.format("None of the lines matched level:%s message:'%s'. Lines were: %s", expectedLoggingLevel, expectedLogMessage, linesInLog)); }
private void processFileMeasures(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION); if (measureFile.exists()) { LOG.debug("Processing " + measureFile.getAbsolutePath()); try { FileLinesContext linesContext = contextFactory.createFor(inputFile); List<String> lines = FileUtils.readLines(measureFile, context.fileSystem().encoding().name()); int lineNumber = 0; for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processMeasure(linesContext, measureFile, lineNumber, line); } linesContext.save(); } catch (IOException e) { throw new IllegalStateException(e); } } }
private void assertNumberOfMessagesInLog(File pluginLogFile, int size) throws Exception { assertThat(FileUtils.readLines(pluginLogFile, Charset.defaultCharset()).size(), is(size)); }
@Override public void execute(SensorContext context) { File f = new File(context.settings().getString(SONAR_XOO_RANDOM_ACCESS_ISSUE_PATHS)); FileSystem fs = context.fileSystem(); FilePredicates p = fs.predicates(); try { for (String path : FileUtils.readLines(f)) { createIssues(fs.inputFile(p.and(p.hasPath(path), p.hasType(Type.MAIN), p.hasLanguage(Xoo.KEY))), context); } } catch (IOException e) { throw new IllegalStateException(e); } }
@Test public void testReadLines() throws Exception { final File file = TestUtils.newFile(getTestDirectory(), "lines.txt"); try { final String[] data = new String[]{"hello", "/u1234", "", "this is", "some text"}; TestUtils.createLineBasedFile(file, data); final List<String> lines = FileUtils.readLines(file, "UTF-8"); assertEquals(Arrays.asList(data), lines); } finally { TestUtils.deleteFile(file); } }
@Test public void createFileFromResource() throws Exception { File file = ResourceUtils.createFileFromResource(resource, temporaryFolder.getRoot(), resourceName); assertThat(file).isNotNull().exists(); List<String> content = FileUtils.readLines(file, Charset.defaultCharset()); assertThat(content).contains(RESOURCE_CONTENT); } }
@Test public final void testXmlOutput() throws IOException { final CheckstyleAntTask antTask = getCheckstyleAntTask(); antTask.setFile(new File(getPath(VIOLATED_INPUT))); antTask.setFailOnViolation(false); final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter(); final File outputFile = new File("target/log.xml"); formatter.setTofile(outputFile); final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType(); formatterType.setValue("xml"); formatter.setType(formatterType); antTask.addFormatter(formatter); antTask.execute(); final List<String> expected = FileUtils.readLines( new File(getPath("ExpectedCheckstyleAntTaskXmlOutput.xml")), StandardCharsets.UTF_8); final List<String> actual = FileUtils.readLines(outputFile, StandardCharsets.UTF_8); for (int i = 0; i < expected.size(); i++) { final String line = expected.get(i); if (!line.startsWith("<checkstyle version") && !line.startsWith("<file")) { assertEquals("Content of file with violations differs from expected", line, actual.get(i)); } } }
@Test public void shouldWriteIndex() throws IOException { InstalledPlugin javaPlugin = newInstalledPlugin("java", true); InstalledPlugin gitPlugin = newInstalledPlugin("scmgit", false); when(pluginFileSystem.getInstalledFiles()).thenReturn(asList(javaPlugin, gitPlugin)); GeneratePluginIndex underTest = new GeneratePluginIndex(serverFileSystem, pluginFileSystem); underTest.start(); List<String> lines = FileUtils.readLines(index); assertThat(lines).containsExactly( "java,true," + javaPlugin.getLoadedJar().getFile().getName() + "|" + javaPlugin.getLoadedJar().getMd5(), "scmgit,false," + gitPlugin.getLoadedJar().getFile().getName() + "|" + gitPlugin.getLoadedJar().getMd5()); underTest.stop(); }
@Test public final void testConfigurationByUrl() throws IOException { final CheckstyleAntTask antTask = new CheckstyleAntTask(); antTask.setProject(new Project()); final URL url = new File(getPath(CONFIG_FILE)).toURI().toURL(); antTask.setConfig(url.toString()); antTask.setFile(new File(getPath(FLAWLESS_INPUT))); final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter(); final File outputFile = new File("target/ant_task_config_by_url.txt"); formatter.setTofile(outputFile); final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType(); formatterType.setValue("plain"); formatter.setType(formatterType); formatter.createListener(null); antTask.addFormatter(formatter); antTask.execute(); final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8); final int sizeOfOutputWithNoViolations = 2; assertEquals("No violations expected", sizeOfOutputWithNoViolations, output.size()); }
@Test public final void testConfigurationByResource() throws IOException { final CheckstyleAntTask antTask = new CheckstyleAntTask(); antTask.setProject(new Project()); antTask.setConfig(getPath(CONFIG_FILE)); antTask.setFile(new File(getPath(FLAWLESS_INPUT))); final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter(); final File outputFile = new File("target/ant_task_config_by_url.txt"); formatter.setTofile(outputFile); final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType(); formatterType.setValue("plain"); formatter.setType(formatterType); formatter.createListener(null); antTask.addFormatter(formatter); antTask.execute(); final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8); final int sizeOfOutputWithNoViolations = 2; assertEquals("No violations expected", sizeOfOutputWithNoViolations, output.size()); }