java特,java新特型

缓存流

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

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

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

随机访问文件

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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修饰,和接口中差不多

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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;

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

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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 f1=s::endsWith;

System.out.println(f1.apply("txt"));

}

}

View Code

Stream

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

分为

Stream串行流和paralleStream并行流

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

常用方法为

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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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 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 asList2 = Arrays.asList("sda","","happy","","unhappy");

long count = asList2.stream().filter(s->!s.isEmpty()).count();

System.out.println(count);

//collectors中的toList非常重要,能够把选出来的集合collect

List 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等方法

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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

标签:特型,java,System,println,import,public,out

来源: https://www.cnblogs.com/xiaoruirui/p/11301014.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值