美文网首页小卜java
JAVA基础知识String

JAVA基础知识String

作者: 汤太咸啊 | 来源:发表于2021-11-01 22:46 被阅读0次

    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
    
    

    相关文章

      网友评论

        本文标题:JAVA基础知识String

        本文链接:https://www.haomeiwen.com/subject/dewtzltx.html