JAVA基础知识String以及文件目录读取

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 stream = bf.lines();
    stream.filter(line -> line.contains("Spring"))
            .findFirst()
            .ifPresent(System.out::println);
}catch(Exception e){

}
//输出
    Demo project for Spring Boot

另一种读取

Path path = Paths.get("/Users","/username/Documents/git_code/course/","pom.xml");
try(Stream stream = Files.lines(path)){
    stream.filter(line -> line.contains("Spring"))
            .findFirst()
            .ifPresent(System.out::println);
}catch(Exception e){

}
//输出
   Demo project for Spring Boot

读取文件目录Files.list

Path path = Paths.get("/Users","/username/Documents/git_code/course/");
try(Stream 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 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 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

你可能感兴趣的:(javastring)