文件复制
文件复制:BufferedInputStream/BufferedOutputStream
文件内容读写:BufferedReader/PrintWriter
FileReader/FileWriter
对象读写:ObjectInputStream/ObjectOutputStream
属性集: Properties, 仅支持String类型的属性映射
extends Hashtable implements Map,key - value
方法:void setProperty(String key, String value)
String getProperty(String key)
加载属性集::void load(Reader)
void load(InputStream)
下面展示一些 属性集代码片
。
public void test01() throws IOException {
Properties p = new Properties();
String path = test.class.getClassLoader().getResource("properties/a.txt").getPath();
p.load(new FileInputStream(path));
p.setProperty("url","http://www.baidu.com");
String url = p.getProperty("url");
System.out.println(url);
p.setProperty("name","zangsan");
String name = p.getProperty("name");
System.out.println(name);
}
函数式编程
函数式编程:Lambda表达式(函数式接口作为方法的参数)
函数式接口:接口中只有一个抽象方法
常用函数式接口:Supplier Consumer Predicate Function
Supplier: 生产者 - T get();
下面展示一些 Supplier
。
public class SupplierTest {
public static String getInstance(Supplier<String> sup){
return sup.get();
}
public static void main(String[] args) {
String str = getInstance(()->{
return "hello";
});
System.out.println(str);
}
}
Consumer: 消费者 - void accept(T t);
默认方法 - andThen(Consumer)
将两个消费方式组合在一块
下面展示一些 Consumer代码片
。
public class ConsumerTest {
public static void test(String str, Consumer<String> first,Consumer<String> second){
first.andThen(second).accept(str);
}
public static void main(String[] args) {
test("PythonGood", s-> {
System.out.println(s.toUpperCase());
}, s-> {
System.out.println(s.toLowerCase());
});
}
}
Predicate: 对对象做判断 - boolean test(T t);
默认方法 - or(||) and(&&) negate(!)
下面展示一些 Predicate
。
public class PredicateTest {
public static void test(String str, Predicate<String> pre1, Predicate<String> pre2){
boolean b = pre1.and(pre2).test(str);
System.out.println(b);
}
public static void main(String[] args) {
test("tongyao", (s)-> {
return s.length() > 1;
}
, (s)-> {
return s.startsWith("tong");
});
}
}
Function<T, R>: 类型转换 - R apply(T t);
默认方法 - andThen(Function)
连续做两种类型转换
下面展示一些 Function代码片
。
public class PredicateTest {
public static void test(String str, Predicate<String> pre1, Predicate<String> pre2){
boolean b = pre1.and(pre2).test(str);
System.out.println(b);
}
public static void main(String[] args) {
test("tongyao", (s)-> {
return s.length() > 1;
}
, (s)-> {
return s.startsWith("tong");
});
}
}
Stream流
获取流: 集合 Collection Map,数组
常用得API: void forEach(Consumer) - 终结方法
Stream filter(Predicate) - 延迟方法
Predicate中test返回true, 是保留在流中的
Stream map(Function<T, R>) - 延迟方法
将 流中的 T类型的数据, 转成 R类型数据, 并且存入新的流中
static Stream concat(Stream, Stream)
将两个流拼接成一个