多线程
public class MyCallable implements Callable { @Override public Object call() throws Exception { //返回值表示多线程运行结果 int sum=0; for (int i = 0; i < 100; i++) { sum+=i; } return sum; } } ------------------- public class MyRun implements Runnable{ @Override public void run() { for (int i = 0; i < 100; i++) { ...
IO
public class Test1 { public static void main(String[] args) throws IOException { /* 练习:文件夹拷贝 */ File src=new File("C:\\aaa\\ccc"); File dest=new File("C:\\aaa\\ddd"); //调用方法 copy(src,dest);//复制文件夹 } public static void copy(File src,File dest) throws IOException { //创建dest的文件夹,便于src复制 dest.mkdirs(); //进入数据源 File[] files = src.listFiles(); if...
IO(字符流)
public class CharSetDemo1 { public static void main(String[] args) throws UnsupportedEncodingException { /* java编码方式: byte[] getBytes() 使用默认方式编码 byte[] getBytes(String charsetName) 使用指定方式编码 java解码方式: String(byte[] bytes) 使用默认方式解码 String(byte[] bytes,String charsetName) 使用指定方式解码 */ //使用默认方式编码 String str="香香"; byte[] bytes = str.getBytes(); ...
IO(字节流)
public class ByteStreamDemo1 { public static void main(String[] args) throws IOException { /* 字节输出流 FileOutputStream 需求:写出一段文字到指定文件中 步骤:1.创建对象 细节1:参数是字符串表示的路径或者File对象 细节2:如果文件不存在,会自动创建,但是父级路径必须存在 细节3:如果文件存在,会清空文件 2.写出数据 细节:参数是整数但是写入的是对应的字符 3.释放资源 每次使用完流之后都要释放资源 */ //1.创建对象 FileOutputStream fos=new FileOutputStream("day31-code\\src\\a.txt"); //2.写出数据 ...
File
```java public class FileDemo1 { public static void main(String[] args) { /* public File(String pathname) //根据路径构建一个新File实例 public File(String parent, String child) //根据父路径名字符串和子路径名字符串创建新的File实例 public File(File parent, String child) //根据父抽象路径名和子路径名字符串创建新的File实例 */ //1.根据字符串路径创建File对象 String str="c:\\Users\\Administrator\\Desktop\\1.txt"; File file=new File(str); System.out.println(file); ...
异常
1.主动抛出异常public class Student { private String name; private int age; public Student(String name, int age) { if (age<0 || age>120){ throw new RuntimeException(); ........->作为一种特殊的返回值,来抛出异常 } this.name = name; this.age = age; } ----------- new Student("haha",180); --- Exception in thread "main" java.lang.RuntimeException at a02.Student.<init>(Student.java:9) at...
方法引用
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; public class FunctionDemo1 { public static void main(String[] args) { //需求:创建一个数组,倒序排列 Integer[] arr={1,2,3,4,5,6}; //1.匿名内部类 Arrays.sort(arr, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2-o1; } }); //2.lambda表达式 ...
Stream练习
public class StreamTest1 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, "haha,18", "xiangxiang,17", "baby,111", "xixi,666"); //转成map集合 Map<String, String> map = list.stream().collect(Collectors.toMap(str -> str.split(",")[0], str -> str.split(",")[1])); System.out.println(map); ...
Stream
public class StreamDemo3 { public static void main(String[] args) { /* Stream流的中间方法: filter 过滤 limit 取用前几个 skip 跳过前几个 distinct 去重,依赖(hashCode和equals方法) concat 合并a流和b流为一个流 //静态方法,用Stream接口名调用 map 类型转换 map() 是一个中间操作方法,作用是把流中的每个元素按照指定的函数进行转换,最终返回一个包含转换后元素的新流。 */ ArrayList<String> list=new ArrayList<>(); ...
不可变集合(.of)
public class ImmutableDemo1 { public static void main(String[] args) { /* 创建不可变集合 */ List<String> list= List.of("haha","xiangxiang","xixi"); System.out.println(list); //list.add("xixi"); for(String s:list){ System.out.println(s); } System.out.println("-----------------"); Iterator<String> it = list.iterator(); ...
