本文整理汇总了Java中java.nio.file.Files.lines方法的典型用法代码示例。如果您正苦于以下问题:Java Files.lines方法的具体用法?Java Files.lines怎么用?Java Files.lines使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.Files
的用法示例。
在下文中一共展示了Files.lines方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: PrintUsageInspector
import java.nio.file.Files; //导入方法依赖的package包/类
PrintUsageInspector(Path printUsageFile) throws IOException {
printedUsage = new HashMap<>();
try (Stream<String> lines = Files.lines(printUsageFile)) {
lines.forEach(line -> {
if (line.startsWith(" ")) {
if (line.contains("(") && line.contains(")")) {
readMethod(line);
} else {
readField(line);
}
} else {
readClazz(line);
}
});
}
}
示例2: generatePointCloud
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Helper method for creating a large amount of points.
*
* @return Large amount of points.
*/
private List<Vector> generatePointCloud() throws URISyntaxException {
List<Vector> pointCloud = new ArrayList<Vector>(2000);
try (Stream<String> stream = Files.lines(Paths.get(
PointCloudCrustImplTest.class.getResource(RES_POINTS).toURI()
))) {
stream.forEach(it -> {
String[] s = it.split("\\s+");
pointCloud.add(new Vector(
Float.valueOf(s[8]),
Float.valueOf(s[9]),
Float.valueOf(s[10]),
s[0]
));
});
} catch (IOException e) {
e.printStackTrace();
}
return pointCloud;
}
示例3: parseArtistLinks
import java.nio.file.Files; //导入方法依赖的package包/类
private List<Artist> parseArtistLinks(List<Artist> artists) {
Map<String, List<Artist>> artistMap = artists.stream()
.filter(album -> !StringUtils.isEmpty(album.getPk()))
.collect(Collectors.groupingBy(Artist::getPk));
//read file into stream, try-with-resources
try (Stream<String> stream = Files.lines(Paths.get(artistUrlsPath))) {
stream.map(strRow -> strRow.split(TAB_SYMBOL))
.forEach(row -> {
List<Artist> artistList = artistMap.get(row[2]);
if (artistList != null) {
artistList.forEach(artist -> artist.getLinks().add(new ArtistUrlLink(row[1], row[3])));
}
});
} catch (IOException e) {
e.printStackTrace();
}
return artists;
}
示例4: main
import java.nio.file.Files; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
Path input = Paths.get(args[0]);
Path output = Paths.get(args[1]);
Map<String, Set<String>> indexMap = indexMap();
try(Stream<String> lines = Files.lines(input)) {
Stream<String> stream = lines
.flatMap(tokens -> findClassAndMethodName(tokens).stream())
.flatMap(name -> {
Set<String> sigs = indexMap.get(name);
//System.out.println("sigs " + sigs + " name " + name);
return (sigs == null)? Stream.of(name): sigs.stream();
})
.map(methodName -> "compileOnly " + methodName);
Files.write(output, (Iterable<String>)stream.sorted().distinct()::iterator);
}
}
示例5: read
import java.nio.file.Files; //导入方法依赖的package包/类
static RunManifest read(Path manifestPath) throws IOException {
final Stream<String> lines = Files.lines(manifestPath);
final RunManifestBuilder builder = new RunManifestBuilder();
lines.forEachOrdered(line -> {
if (line.trim().isEmpty()) {
return;
}
final String[] split = line.trim().split(" ", 2);
if (split.length != 2) {
throw new IllegalArgumentException("Malformed manifest line '" + line + "'");
}
switch (split[0].charAt(0)) {
case LAMBDA:
builder.continuation(split[1]);
break;
case CLASSPATH_FILE:
builder.addClassPathFile(split[1]);
break;
case REGULAR_FILE:
builder.addFile(split[1]);
break;
default:
LOG.warn("Unrecognized manifest entry '" + line + "'");
}
});
return builder.build();
}
示例6: loadTextFile
import java.nio.file.Files; //导入方法依赖的package包/类
private void loadTextFile(String filename, Consumer<String> processor) {
try (Stream<String> stream = Files.lines(Paths.get(filename))) {
stream.filter(line -> {return !line.startsWith("#") &&
!line.trim().equals("");})
.forEach(processor);
} catch (IOException e) {
e.printStackTrace();
}
}
示例7: fileLines
import java.nio.file.Files; //导入方法依赖的package包/类
private Stream<String> fileLines(File file) {
try {
return Files.lines(file.toPath());
} catch (IOException io) {
throw new PluginException("Couldn't read file");
}
}
示例8: readCitiesFromFile
import java.nio.file.Files; //导入方法依赖的package包/类
private List<Point2D> readCitiesFromFile() throws IOException {
List<Point2D> cities = new ArrayList<>(8092);
String citiesFile = (Main.class.getResource("cities.txt").toExternalForm()).replace("file:", "");
Stream<String> lines = Files.lines(Paths.get(citiesFile));
lines.forEach(line -> {
String city[] = line.split(",");
double[] xy = World.latLonToXY(Double.parseDouble(city[1]), Double.parseDouble(city[2]));
cities.add(new Point2D(xy[0], xy[1]));
});
lines.close();
return cities;
}
示例9: testConstructException
import java.nio.file.Files; //导入方法依赖的package包/类
public void testConstructException() {
try (Stream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
s.forEach(l -> fail("File is not even exist!"));
} catch (IOException ioe) {
assertTrue(ioe instanceof NoSuchFileException);
}
}
示例10: load
import java.nio.file.Files; //导入方法依赖的package包/类
public void load() throws IOException {
// Get metric name and put it in a map and assign it a number.
// Add ts and value to metric store with that number.
try (Stream<String> lines = Files.lines(filePath, Charset.defaultCharset())) {
lines.forEachOrdered(line -> {
if (line == null || line.isEmpty()) { // Ignore empty lines
return;
}
try {
String[] metricParts = line.split(" ");
if (metricParts.length > 1 && metricParts[0].equals("put")) {
String metricName = metricParts[1].trim();
List<String> rawTags = Arrays.asList(metricParts).subList(4, metricParts.length);
Metric metric = new Metric(metricName, rawTags);
long ts = Long.parseLong(metricParts[2].trim());
double value = Double.parseDouble(metricParts[3].trim());
// System.out.println(metric.fullMetricName);
int id = -1;
if (metricidHashMap.containsKey(metric.fullMetricName)) {
id = metricidHashMap.get(metric.fullMetricName);
} else {
metricCounter[0] = metricCounter[0] + 1;
metricidHashMap.put(metric.fullMetricName, metricCounter[0]);
id = metricCounter[0];
}
store.addPoint(id, ts, value);
}
} catch (Exception e) {
// System.out.println("Error ingesting line " + line + " with exception " + e.getMessage());
}
});
}
metricidHashMap.clear();
// System.out.println("Metric counter size: " + metricCounter[0]);
}
示例11: elideStartUpFromSave
import java.nio.file.Files; //导入方法依赖的package包/类
public void elideStartUpFromSave() throws IOException {
Compiler compiler = new Compiler();
Path path = compiler.getPath("myfile");
test(
(a) -> assertCommandOutputContains(a, "123", "==> 123"),
(a) -> assertCommand(a, "/save " + path.toString(), "")
);
try (Stream<String> lines = Files.lines(path)) {
assertEquals(lines.count(), 1, "Expected only one saved line");
}
}
示例12: setName
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Chooses a random name for the entity from a file.
*
* @param names The file containing the potential name choices
* @param numOfNames The number of names in the file
*/
public void setName(File names, int numOfNames)
{
Stream<String> lines = null;
try
{
lines = Files.lines(names.toPath());
}
catch (IOException e)
{
e.printStackTrace();
}
name = lines.skip((int) (Math.random() * numOfNames)).findFirst().get();
}
示例13: load
import java.nio.file.Files; //导入方法依赖的package包/类
private Stream<String> load(String filename) {
try {
return Files.lines(Paths.get(
new PathMatchingResourcePatternResolver()
.getResource(filename)
.getURI()
));
} catch (final IOException ex) {
throw new RuntimeException("Could not find required file '" +
filename + "' on classpath.");
}
}
示例14: parseUrls
import java.nio.file.Files; //导入方法依赖的package包/类
private List<Artist> parseUrls(List<Artist> artists) {
Map<String, List<Artist>> urlArtistMap = new HashMap<>();
artists.stream()
.filter(artist -> !artist.getLinks().isEmpty())
.forEach(artist -> {
artist.getLinks().forEach(artistUrlLink -> {
if (!urlArtistMap.containsKey(artistUrlLink.getUrlId())) {
urlArtistMap.put(artistUrlLink.getUrlId(), new ArrayList<>());
}
urlArtistMap.get(artistUrlLink.getUrlId()).add(artist);
});
});
//read file into stream, try-with-resources
try (Stream<String> stream = Files.lines(Paths.get(urlsPath))) {
stream.map(strRow -> strRow.split(TAB_SYMBOL))
.forEach(row -> {
List<Artist> artistsWithSuchUrlList = urlArtistMap.get(row[0]);
if (artistsWithSuchUrlList != null) {
String pictureUrl = (row[2].contains("commons.wikimedia")) ? getActualWikiImageURL(row[2]) : row[2];
artistsWithSuchUrlList.forEach(artist -> {
if (artist.getProfileImageUrl() == null || artist.getProfileImageUrl().isEmpty()) {
artist.setProfileImageUrl(pictureUrl);
} else {
artist.addImageUrl(pictureUrl);
}
});
}
});
} catch (IOException e) {
e.printStackTrace();
}
return artists;
}
示例15: main
import java.nio.file.Files; //导入方法依赖的package包/类
public static void main(String[] args) throws URISyntaxException, IOException {
Path p = Paths.get(ClassLoader.getSystemResource(SRC).toURI());
Stream<String> lines = Files.lines(p);
int[] temperatures = parseTemperature(lines).toArray();
// out.println(streamMax(lines));
out.println(intStreamMax(stream(temperatures)));
for (int i = 0; i < 10; i++) {
// out.println(streamAverage1(stream(temperatures)));
out.println(streamAverage2(stream(temperatures)));
// out.println(streamAverage3(stream(temperatures)));
}
}