Java Code Examples for com.google.common.io.Files#newReader()

The following examples show how to use com.google.common.io.Files#newReader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheBootstrap.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private Map<String, Set<String>> readConfigurationAliases() {
  URL aliasesResource = getClass().getClassLoader().getResource(PROPERTIES_ALIASES_CONFIG_FILE);
  Map<String, Set<String>> aliases = new HashMap<>();
  if (aliasesResource != null) {
    Properties properties = new Properties();
    File aliasesFile = new File(aliasesResource.getFile());
    try (Reader reader = Files.newReader(aliasesFile, Charset.forName("UTF-8"))) {
      properties.load(reader);
    } catch (IOException e) {
      throw new IllegalStateException(
          format("Unable to read configuration aliases from file %s", aliasesFile), e);
    }
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
      String value = (String) entry.getValue();
      aliases.put(
          (String) entry.getKey(),
          Splitter.on(',').splitToList(value).stream().map(String::trim).collect(toSet()));
    }
  }
  return aliases;
}
 
Example 2
Source File: CheBootstrap.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
protected void bindConf(File confDir) {
  final File[] files = confDir.listFiles();
  if (files != null) {
    for (File file : files) {
      if (!file.isDirectory()) {
        if ("properties".equals(Files.getFileExtension(file.getName()))) {
          Properties properties = new Properties();
          try (Reader reader = Files.newReader(file, Charset.forName("UTF-8"))) {
            properties.load(reader);
          } catch (IOException e) {
            throw new IllegalStateException(
                format("Unable to read configuration file %s", file), e);
          }
          bindProperties(properties);
        }
      }
    }
  }
}
 
Example 3
Source File: FileCollectionBackedTextResource.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Reader asReader() {
    try {
        return Files.newReader(asFile(), charset);
    } catch (FileNotFoundException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 4
Source File: TwillContainerMain.java    From twill with Apache License 2.0 5 votes vote down vote up
private static Map<String, Map<String, String>> loadLogLevels() throws IOException {
  File file = new File(Constants.Files.LOG_LEVELS);
  if (file.exists()) {
    try (Reader reader = Files.newReader(file, Charsets.UTF_8)) {
      Gson gson = new GsonBuilder().serializeNulls().create();
      return gson.fromJson(reader, new TypeToken<Map<String, Map<String, String>>>() { }.getType());
    }
  }
  return new HashMap<>();
}
 
Example 5
Source File: SceneReader.java    From flashback with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Read scene from file and construct Scene object
 * @param name scene name
 * @return scene object de-serialized from file
 *
 * */
public Scene readScene(String rootPath, String name)
    throws IOException {
  File file = new File(rootPath, name);
  if (file.isFile()) {
    if (file.length() == 0) {
      return new Scene(name, null, rootPath, new ArrayList<>());
    }
    BufferedReader reader = Files.newReader(file, Charset.forName(SceneSerializationConstant.FILE_CHARSET));
    SceneDeserializer sceneDeserializer = new SceneDeserializer();
    return sceneDeserializer.deserialize(reader);
  }
  return null;
}
 
Example 6
Source File: FileCollectionBackedTextResource.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Reader asReader() {
    try {
        return Files.newReader(asFile(), charset);
    } catch (FileNotFoundException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 7
Source File: SeleniumTestConfiguration.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
void addFile(File file) {
  if (!file.isDirectory()) {
    if ("properties".equals(Files.getFileExtension(file.getName()))) {
      Properties properties = new Properties();
      try (Reader reader = Files.newReader(file, Charset.forName("UTF-8"))) {
        properties.load(reader);
      } catch (IOException e) {
        throw new IllegalStateException(
            String.format("Unable to read configuration file %s", file), e);
      }
      addAll(Maps.fromProperties(properties));
    }
  }
}
 
Example 8
Source File: Options.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void addManifest(String manifestFile) throws IOException {
  BufferedReader in = Files.newReader(new File(manifestFile), Charset.forName(fileEncoding));
  try {
    for (String line = in.readLine(); line != null; line = in.readLine()) {
      if (!Strings.isNullOrEmpty(line)) {
        sourceFiles.add(line.trim());
      }
    }
  } finally {
    in.close();
  }
}
 
Example 9
Source File: ScenarioJsonReader.java    From JGiven with Apache License 2.0 5 votes vote down vote up
/**
 * @throws JsonReaderException in case there was an error while reading the file.
 */
@Override
public ReportModel apply( File file ) {
    Reader reader = null;
    try {
        reader = Files.newReader( file, Charsets.UTF_8 );
        return new Gson().fromJson( reader, ReportModel.class );
    } catch( Exception e ) {
        throw new JsonReaderException( file, e );
    } finally {
        ResourceUtil.close( reader );
    }
}
 
Example 10
Source File: CsvFilesTest.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCsvDataArray_fromReader() throws IOException {
  Reader reader = Files.newReader(csvStringFile, StandardCharsets.UTF_8);
  List<String[]> actualDataArray = CsvFiles.getCsvDataArray(reader, headerPresent);
  assertEquals(dataList.size(), actualDataArray.size());
  for (int i = 0; i < actualDataArray.size(); i++) {
    assertArrayEquals("Row " + i + " does not match", dataList.get(i), actualDataArray.get(i));
  }

  // Reader should be closed (not ready).
  thrown.expect(IOException.class);
  thrown.expectMessage("Stream closed");
  reader.ready();
}
 
Example 11
Source File: FileUtil.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
/**
 * 获取File的BufferedReader
 */
public static BufferedReader asBufferedReader(String fileName) throws FileNotFoundException {
	return Files.newReader(getFileByPath(fileName), Charsets.UTF_8);
}
 
Example 12
Source File: TwillRuntimeSpecificationAdapter.java    From twill with Apache License 2.0 4 votes vote down vote up
public TwillRuntimeSpecification fromJson(File file) throws IOException {
  try (Reader reader = Files.newReader(file, Charsets.UTF_8)) {
    return fromJson(reader);
  }
}
 
Example 13
Source File: Replay.java    From scheduler with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Replay from a given path.
 *
 * @param cstrs the constraint catalog
 * @param path  the file containing the jsons.
 * @throws IOException if an error occurred while reading the file
 */
public Replay(List<Constraint> cstrs, Path path) throws IOException {
    in = Files.newReader(path.toFile(), Charset.defaultCharset());
    constraints = cstrs;
}