java新特型

 

缓存流

Buffered修饰有字节流和字符流常用方法于fileinputStram类似减少内存于磁盘的操作,而通过缓存于内存操作,缓存于磁盘操作,

BufferedinputStream中常用的方法也为read()方法,读入字节数组长度的大小

BufferRead中有一个readline方法当读到该行没有字符的时候为null可以通过该条件进行循环

随机访问文件

RandomAccessFilefilemode)读取文件中的随机mode中只能为“r,”rw”,等随机读入该文件中的,常用方法为seek把指针移到该位置,getFilePorint得到该位置的位置值达到断电续传的目的,setlength设置文件的大小

package cn.jiedada.radomaccessfile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Test {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("hello.txt");
        //Exception in thread "main" java.lang.IllegalArgumentException:
        //Illegal mode "2" must be one of "r", "rw", "rws", or "rwd"
        String mode="r";
        RandomAccessFile raf = new RandomAccessFile(file, mode);
        System.out.println(raf.length());
        //偏移量
        raf.seek(3);
        raf.getFilePointer();
        System.out.println((char)raf.read());
    }

}
View Code

Java新特性

Lambda表达式,方法引用,默认方法,Stream,Optional

函数式接口

定义:只有一个普通方法,可以有默认方法和静态方法,静态方法通过接口名.方法使用,静态方法通过实现类对象.方法调用,通过@Functionlainterface标识判断是不是函数式接口

定义为 接口名 对象=(数据类型 变量)->{};主题结构

当可以省略数据类型,当只有一个变量的时候可以省略(),当只有{}只有一个输出语句的时候可以省略{}当时返回语句的时候可以省略return

Lambda表达式中在Java不会生产独立的字节码文件,当有变量的时候为final修饰,和接口中差不多

package cn.jiedada.lamda;

public class Test {
    /*参数1,参数2…)表示参数列表;->表示连接符;{}内部是方法体 
    1、=右边的类型会根据左边的函数式接口类型自动推断; 
    2、如果形参列表为空,只需保留(); 
    3、如果形参只有1个,()可以省略,只需要参数的名称即可; 
    4、如果执行语句只有1句,且无返回值,{}可以省略,若有返回值,则若想省去{},
    则必须同时省略return,且执行语句也保证只有1句; 
    5、形参列表的数据类型会自动推断; 
    6、lambda不会生成一个单独的内部类文件; 
    7、lambda表达式若访问了局部变量,则局部变量必须是final的,
    若是局部变量没有加final关键字,系统会自动添加,此后在修改该局部变量,会报错;*/
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /*Testfuntion test1=name->System.out.println(name+"在吃东西");
        test1.print("杰大大");*/
        
        Testfuntion t2=(a,b)->a+b;
        System.out.println(t2.sum(5, 7));
        StudentInterface studentInterface=()->{
            System.out.println("在学习中,请不要打扰");
        };
        studentInterface.student();
        PlayInterface playInterface=(String name)->{
            System.out.println(name+"在玩");
        };
        playInterface.play("杰帅");
        StudentInterface.eat();
        studentInterface.walk();
        
    }

}
View Code

引用方法

类与于lambda表达式的简写分为

构造方法:类名::new;

静态方法:类名::静态方法名;

对象方法:对象名::方法名;

package cn.jiedada.methods;

import java.util.function.Function;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //静态引用,把一个字符串转化为一个interger
        //左边是接口,右边是引用类名::静态方法
        ParaseInteger integer=Integer::valueOf;
        Integer parase = integer.parase("10");
        System.out.println(parase);
        //实列引用,传入一个String看是否以什么方式结尾
        String s="hello.txt";
        Function<String, Boolean> f1=s::endsWith;
        System.out.println(f1.apply("txt"));
    }

}
View Code

Stream

通过类似流水线生成,每个方法做一件事情,返回值以最后一个方法的使用为返回;

分为

Stream串行流和paralleStream并行流

可以对数组,集合,io流,产生器操作

常用方法为

Foreach()遍历,sorted()排序,limlt(),map()规定,filter()过滤,Collectors().toList()把数据存入集合中

package cn.jiedada.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamTest {
    /*
     * stream流
     * 使用范围为数组,集合,io流,创造器
     * */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /*List<Integer> asList = Arrays.asList(1,4,7,8,4,9,7,8,2);
        //使用流遍历和打印出
        asList.stream().sorted().forEach(System.out::print);
        //获取平方数然后去重
        asList.stream().map(i->i*i).distinct().forEach(System.out::println);*/
        List<String> asList2 = Arrays.asList("sda","","happy","","unhappy");
        long count = asList2.stream().filter(s->!s.isEmpty()).count();
        System.out.println(count);
        //collectors中的toList非常重要,能够把选出来的集合collect
        List<String> collect = asList2.stream().filter(s->!s.isEmpty()).collect(Collectors.toList());
        System.out.println(collect);
        
    }

}
View Code

Optional

使代码高端大气上档次()没什么用

Optional.ofnullLable(对象).map(lambda).orElse();

期间API

jdk1.8以后可以直接调用LocalDateTime可以直接调出日期等等,还有getDay等方法

package cn.jiedada.LocalDateTime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDate localDate = now.toLocalDate();
        System.out.println(localDate);
        int dayOfMonth = now.getDayOfMonth();
        int second = now.getSecond();
        System.out.println(dayOfMonth);
        System.out.println(second);
        int minute = now.getMinute();
        int hour = now.getHour();
        /*ZoneId of = ZoneId.of("Europe/Paris");
        System.out.println(LocalDateTime.now(of));*/
    }

}
View Code
原文地址:https://www.cnblogs.com/xiaoruirui/p/11301014.html