String 根底
String s = "Hello World!";
s.chars().mapToObj(letter -> (char)letter)
.map(Character::toUpperCase)
.forEach(System.out::print);
// 输入
HELLO WORLD!
StringBuffer 是同步的,StringBuilder 是不同步的,因而 StringBuilder 执行速度更快
JDK7 当前的 String 的 + 操作曾经通过了 StringBuilder 编译,因而执行速度也很快,不必再必须应用 StringBuilder 或者 StringBuffer 了
// 读取文件并输入发现的第一个 Spring 的行
try(BufferedReader bf = new BufferedReader(new FileReader(new File("/Users/username/Documents/git_code/course/pom.xml")))){Stream<String> stream = bf.lines();
stream.filter(line -> line.contains("Spring"))
.findFirst()
.ifPresent(System.out::println);
}catch(Exception e){
}
// 输入
<description>Demo project for Spring Boot</description>
另一种读取
Path path = Paths.get("/Users","/username/Documents/git_code/course/","pom.xml");
try(Stream<String> stream = Files.lines(path)){stream.filter(line -> line.contains("Spring"))
.findFirst()
.ifPresent(System.out::println);
}catch(Exception e){
}
// 输入
<description>Demo project for Spring Boot</description>
读取文件目录 Files.list
Path path = Paths.get("/Users","/username/Documents/git_code/course/");
try(Stream<Path> stream = Files.list(path)){stream.filter(p -> p.toFile().isDirectory())
.forEach(System.out::println);
}catch(Exception e){
}
// 输入
/Users/username/Documents/git_code/course/target
/Users/username/Documents/git_code/course/.mvn
/Users/username/Documents/git_code/course/.idea
/Users/username/Documents/git_code/course/src
遍历文件目录 Files.walk
Path path = Paths.get("/Users","/username/Documents/git_code/course/");
try(Stream<Path> stream = Files.walk(path)){stream.filter(p -> p.toFile().isDirectory())
.forEach(System.out::println);
}catch(Exception e){
}
// 输入
/Users/username/Documents/git_code/course
/Users/username/Documents/git_code/course/target
/Users/username/Documents/git_code/course/target/generated-sources
/Users/username/Documents/git_code/course/target/generated-sources/annotations
/Users/username/Documents/git_code/course/target/classes
/Users/username/Documents/git_code/course/target/classes/static
/Users/username/Documents/git_code/course/target/classes/templates
/Users/username/Documents/git_code/course/target/classes/templates/pdf
/Users/username/Documents/git_code/course/.mvn
/Users/username/Documents/git_code/course/.mvn/wrapper
/Users/username/Documents/git_code/course/.idea
/Users/username/Documents/git_code/course/.idea/libraries
/Users/username/Documents/git_code/course/src
遍历指定层级目录 Files.walk
Path path = Paths.get("/Users","/username/Documents/git_code/course/");
try(Stream<Path> stream = Files.walk(path, 2)){stream.filter(p -> p.toFile().isDirectory())
.forEach(System.out::println);
}catch(Exception e){
}
// 输入
/Users/username/Documents/git_code/course
/Users/username/Documents/git_code/course/target
/Users/username/Documents/git_code/course/target/generated-sources
/Users/username/Documents/git_code/course/target/classes
/Users/username/Documents/git_code/course/.mvn
/Users/username/Documents/git_code/course/.mvn/wrapper
/Users/username/Documents/git_code/course/.idea
/Users/username/Documents/git_code/course/.idea/libraries
/Users/username/Documents/git_code/course/src
/Users/username/Documents/git_code/course/src/test
/Users/username/Documents/git_code/course/src/main