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(); ...
随机点名器_带概率,不重复
public class PokerGame2 { //只要牌的对象和只能对应就好了,不用treemap static HashMap<Integer,String> poker=new HashMap<>(); static ArrayList<Integer> list=new ArrayList<>(); static { String [] colors={"♦","♣","♥","♠"}; String []...
collections
public class CollectionsDemo1 { public static void main(String[] args) { //collections是集合的工具类,不是集合 /* public static <T> boolean addAll(Collection<T> c,T...element) 批量添加元素 public static void shuffle(List<?> list) 打乱list集合元素的顺序 */ //1.创建集合 ArrayList<String> list = new ArrayList<>(); //2.批量添加元素 Collections.addAll(list, "haha", "xiangxiang", "baby",...