Compare commits
115 Commits
7bdd904e54
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f9191f08cc | |||
| 815c057736 | |||
| 73eda83d9d | |||
| fb065125e1 | |||
| 0b31c10927 | |||
| 4e2f6f6f5d | |||
| d3bc46d2e0 | |||
| 8d80caa0b3 | |||
| 2fcec5ec75 | |||
| bd74563b27 | |||
| 435a8d85c3 | |||
| 168fb05eb3 | |||
| 547a9983ad | |||
| 3284b84f18 | |||
| 1e1927ec45 | |||
| 32499ad649 | |||
| 631909e68a | |||
| 61801feaa8 | |||
| 80ae06858e | |||
| 2fff7071b7 | |||
| fcd7b9e1b4 | |||
| 580ccafb2a | |||
| b6308288a4 | |||
| f726ca5078 | |||
| 4d6715e0ac | |||
| aab9f96208 | |||
| 5172c55034 | |||
| c38f63dd85 | |||
| 793f0d0ea2 | |||
| f2707af757 | |||
| 408933e7d4 | |||
| f423ecfc9a | |||
| 5e00739d68 | |||
| af3ed26f59 | |||
| 98e33949b1 | |||
| 32e12b4824 | |||
| ce94c2636e | |||
| c59f70cb24 | |||
| ab3a5598a7 | |||
| b6b5c8ab39 | |||
| 822f7b4810 | |||
| 3631315995 | |||
| 259a84c1ae | |||
| 64d2607397 | |||
| 870890f269 | |||
| cbb90a035e | |||
| eb74979f6e | |||
| cf0faa7163 | |||
| 933456c8f6 | |||
| 6c1c87969b | |||
| 6c725333fa | |||
| e03cf2327c | |||
| e50b2e7b8a | |||
| f64832ef33 | |||
| 077745e892 | |||
| 8b1e6f5b7e | |||
| 8f84f04700 | |||
| ab377f938e | |||
| 11d9b97656 | |||
| d1108fcf1f | |||
| 8f31c94ed8 | |||
| 6211e4db35 | |||
| 5f3a7ef347 | |||
| 54ea3ca515 | |||
| e1aab3d8e7 | |||
| a69efbd2c2 | |||
| 5303486ff5 | |||
| f8e67a3277 | |||
| 97a7740d27 | |||
| 0915fb7839 | |||
| 5228f223fd | |||
| 5f28b2f002 | |||
| c9d09a22c5 | |||
| 9aeea4f16e | |||
| 789dcc7aaf | |||
| eb7db2e925 | |||
| 1c98719664 | |||
| 5b8c59a0b9 | |||
| 2a5254f963 | |||
| 2f2bb884c5 | |||
| 9fd37dfd3a | |||
| b9af85ec24 | |||
| 68ead1ecdb | |||
| ac11386cde | |||
| d5297a0324 | |||
| 34acd3ec4f | |||
| ff9204a145 | |||
| 741e960e95 | |||
| c339fcf44e | |||
| 5b1726e5dc | |||
| 8bf20dcf3d | |||
| be4298f311 | |||
| 003606e8bc | |||
| a5a65edb80 | |||
| e31a7271ff | |||
| 64e5edb203 | |||
| 2725c1d6ec | |||
| a07cd63cc7 | |||
| c2452d0811 | |||
| 50eb7dcb2c | |||
| 7d6dee4aed | |||
| 1a273529c9 | |||
| 25c32a71e7 | |||
| 20932dcfcb | |||
| d336ba7708 | |||
| 80a577d473 | |||
| 016426e963 | |||
| 7f4b0cd01c | |||
| e0e87886c0 | |||
| 36aaf90ebe | |||
| 9bfa87fd2b | |||
| e5508aff39 | |||
| 73638684b3 | |||
| 11ac398bed | |||
| fb4d4f27a3 |
34
javaSE-day06/src/com/inmind/thread_pool10/Demo12.java
Normal file
34
javaSE-day06/src/com/inmind/thread_pool10/Demo12.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package com.inmind.thread_pool10;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
/*
|
||||||
|
线程池的创建--方式二Executors
|
||||||
|
Executors的静态方法:
|
||||||
|
1.static ExecutorService newFixedThreadPool(int nThreads) 创建一个固定线程的线程池
|
||||||
|
2.static ExecutorService newSingleThreadExecutor() 创建一个只有一个线程的线程池,如果该线程出现异常,那么线程池主动创建一个新的替代
|
||||||
|
3.static ExecutorService newCachedThreadPool() 创建出随任务增加而增加线程的线程池,如果一个线程执行任务之后空闲了60s,这个线程就会被回收
|
||||||
|
|
||||||
|
注意:在实际开发中,不推荐使用Executors来创建线程池,工具类创建线程的底层其实就是ThreadPoolExecutor构造方法,只不过参数不同。
|
||||||
|
咱们的线程池的参数,要根据不同的业务来动态调整(而不是写死)
|
||||||
|
*/
|
||||||
|
public class Demo12 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ExecutorService threadPool = Executors.newFixedThreadPool(3);
|
||||||
|
MyRunnable task = new MyRunnable();
|
||||||
|
//将任务交给线程池的核心线程处理
|
||||||
|
threadPool.execute(task);
|
||||||
|
threadPool.execute(task);
|
||||||
|
threadPool.execute(task);
|
||||||
|
//3个核心线程被占用,4个任务队列
|
||||||
|
threadPool.execute(task);
|
||||||
|
threadPool.execute(task);
|
||||||
|
threadPool.execute(task);
|
||||||
|
threadPool.execute(task);
|
||||||
|
//核心线程和任务队列都占满,创建临时线程
|
||||||
|
threadPool.execute(task);
|
||||||
|
threadPool.execute(task);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
28
javaSE-day06/src/com/inmind/thread_pool10/Demo13.java
Normal file
28
javaSE-day06/src/com/inmind/thread_pool10/Demo13.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package com.inmind.thread_pool10;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习任务:线程池:处理Callable任务
|
||||||
|
|
||||||
|
提交Callable任务的API:
|
||||||
|
<T> Future<T> submit(Callable<T> task) 提交值返回任务以执行,并返回代表任务待处理结果的Future。
|
||||||
|
|
||||||
|
注意:有返回的子线程操作,它的返回值会封装在Future中,通过get方法来获取
|
||||||
|
*/
|
||||||
|
public class Demo13 {
|
||||||
|
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||||
|
//创建线程池
|
||||||
|
ExecutorService threadPool = Executors.newFixedThreadPool(5);
|
||||||
|
Future<Integer> future1 = threadPool.submit(new MyCallable(5));
|
||||||
|
Future<Integer> future2 = threadPool.submit(new MyCallable(3));
|
||||||
|
Future<Integer> future3 = threadPool.submit(new MyCallable(10));
|
||||||
|
|
||||||
|
System.out.println("任务1返回的结果为:"+future1.get());
|
||||||
|
System.out.println("任务2返回的结果为:"+future2.get());
|
||||||
|
System.out.println("任务3返回的结果为:"+future3.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
25
javaSE-day06/src/com/inmind/thread_pool10/MyCallable.java
Normal file
25
javaSE-day06/src/com/inmind/thread_pool10/MyCallable.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package com.inmind.thread_pool10;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
|
public class MyCallable implements Callable<Integer> {
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
public MyCallable(int sum) {
|
||||||
|
this.sum = sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
//计算固定值的累和
|
||||||
|
@Override
|
||||||
|
public Integer call() throws Exception {
|
||||||
|
//计算sum的累和
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i <= sum; i++) {
|
||||||
|
result+= i;
|
||||||
|
}
|
||||||
|
Thread.sleep(1000);
|
||||||
|
System.out.println(Thread.currentThread().getName()+"执行完累加操作,结果为:"+result);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
/*
|
||||||
|
学习的内容:函数式接口的概念
|
||||||
|
函数式接口在Java中是指:有且仅有一个抽象方法的接口。(今后自定义的主要的情况)
|
||||||
|
函数式接口:有且仅有一个必须被重写的抽象方法的接口.
|
||||||
|
|
||||||
|
注意:在java使用一个注解@FunctionalInterface来验证指定的接口是否是函数式接口
|
||||||
|
|
||||||
|
扩展:为何在一个接口中某些抽象方法不需要被重写??比如Comparator接口
|
||||||
|
在一个类中如果单继承和多实现,单继承的方法优先级高于接口中的方法定义,导致父类方法中
|
||||||
|
如果有跟接口中重名的方法实现的话,那么就相当于在子类中默认重写了接口的方法
|
||||||
|
------------------------------------------------------------------
|
||||||
|
函数式接口的使用:
|
||||||
|
1.作为参数
|
||||||
|
2.作为返回值
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//匿名内部类的方式来调用getValue()方法
|
||||||
|
/*int value = getValue(new MyInterface() {
|
||||||
|
@Override
|
||||||
|
public int getSum(int a, int b) {
|
||||||
|
return a+b;
|
||||||
|
}
|
||||||
|
}, 10, 20);*/
|
||||||
|
// int value = getValue((int a, int b)->{return a+b;},10,20);
|
||||||
|
int value = getValue((a, b)-> a+b,10,20);
|
||||||
|
System.out.println("运行结果1:"+value);
|
||||||
|
//------------------------------------------
|
||||||
|
MyInterface instance = getInstance();
|
||||||
|
int result = instance.getSum(10, 20);
|
||||||
|
System.out.println("运行结果2:"+result);
|
||||||
|
}
|
||||||
|
//定义出一个函数式接口作为返回值类型,参数列表定义为无参的静态方法
|
||||||
|
public static MyInterface getInstance(){
|
||||||
|
/*MyInterface myInterface = new MyInterface() {
|
||||||
|
@Override
|
||||||
|
public int getSum(int a, int b) {
|
||||||
|
return a*b;
|
||||||
|
}
|
||||||
|
};*/
|
||||||
|
// MyInterface myInterface =(int a, int b)->{return a*b;};
|
||||||
|
// MyInterface myInterface =( a, b)-> a*b;
|
||||||
|
return (a, b)-> a*b;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//定义出一个有整数返回值,参数列表是函数式接口,2个整数的静态方法
|
||||||
|
public static int getValue(MyInterface myInterface, int a, int b) {
|
||||||
|
int sum = myInterface.getSum(a, b);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/*
|
||||||
|
在jdk1.8之后,java提供了一些常用的函数式接口供开发人员直接使用.
|
||||||
|
9.常用的函数式接口_Consumer
|
||||||
|
Interface Consumer<T>
|
||||||
|
抽象方法:
|
||||||
|
void accept(T t) 对给定的参数执行此操作。
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.Consumer表示一个消费者.只接受一个参数,没有返回值.
|
||||||
|
2.什么时候要使用Consumer接口??
|
||||||
|
当我们开发人员想自定义一个函数式接口,并且该接口中要定义一个接收一个参数,没有返回值的方法
|
||||||
|
|
||||||
|
需求:定义出一个使用常用函数式接口的方法,并对字符串进行消费操作(大写打印,小写打印)
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// print((String s)->{System.out.println(s.toLowerCase());},"aBcD");
|
||||||
|
print(s->System.out.println(s.toLowerCase()),"aBcD");
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义出一个使用常用函数式接口的方法
|
||||||
|
public static void print(Consumer<String> consumer,String str) {
|
||||||
|
consumer.accept(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习内容:
|
||||||
|
default Consumer<T> andThen(Consumer after) 返回一个组成的 Consumer ,依次执行此操作,然后执行 after操作
|
||||||
|
|
||||||
|
需求:我要使用消费者接口,对同一个字符串进行大写,和小写的消费操作
|
||||||
|
分析:
|
||||||
|
1.一个消费者的消费操作:对字符串进行小写打印
|
||||||
|
2.另一个消费者的消费操作:对字符串进行大写打印
|
||||||
|
*/
|
||||||
|
public class Demo06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str = "aBcD";
|
||||||
|
|
||||||
|
print(
|
||||||
|
s1-> System.out.println(s1.toLowerCase()),
|
||||||
|
s2-> System.out.println(s2.toUpperCase()),
|
||||||
|
str
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void print(Consumer<String> consumer1, Consumer<String> consumer2, String s) {
|
||||||
|
/*consumer1.accept(s);
|
||||||
|
consumer2.accept(s);*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
andThen的源码:
|
||||||
|
default Consumer<T> andThen(Consumer<? super T> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (T t) -> { accept(t); after.accept(t); };
|
||||||
|
}
|
||||||
|
|
||||||
|
andThen:让多个消费者按顺序去消费同一个参数
|
||||||
|
*/
|
||||||
|
consumer1.andThen(consumer2).andThen(consumer2).accept(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/*
|
||||||
|
JDK为了避免各个程序原定义一些重复的无效的函数式接口,所以,它就提供了一些常用的函数式接口
|
||||||
|
Supplier<T> :表示生产者
|
||||||
|
T:泛型,用来决定生产者要生产数据类型
|
||||||
|
|
||||||
|
Supplier<T> 提供了一个生产方法:T get() 获得结果。
|
||||||
|
|
||||||
|
总结:Supplier<T>的作用,当我们想要定义一个无参有返回值的函数式接口时,就使用Supplier
|
||||||
|
|
||||||
|
举例:想要定义一个无参有返回值(String)的函数式接口:Supplier<String> -->String get()
|
||||||
|
举例:想要定义一个无参有返回值(Student)的函数式接口:Supplier<Student> -->Student get()
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//我想要一个字符串
|
||||||
|
// method(()->{return "这是字符串结果";});
|
||||||
|
method(()->"这是字符串结果");
|
||||||
|
//我想要一个整数
|
||||||
|
method(()-> 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义出一个方法,接收Supplier接口作为参数,打印出我们想要的结果
|
||||||
|
public static void method(Supplier supplier) {
|
||||||
|
Object o = supplier.get();
|
||||||
|
System.out.println(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
/*
|
||||||
|
12.常用的函数式接口_Predicate
|
||||||
|
Interface Predicate<T>
|
||||||
|
boolean test(T t) 用来判断或者筛选数据
|
||||||
|
Predicate函数式接口的作用:用来进行数据筛选,判断指定的数据是否符合业务
|
||||||
|
|
||||||
|
什么时候要使用Predicat呢??
|
||||||
|
当我们要定义一个有一个参数,并且返回值类型为boolean的函数式接口时,直接使用Predicate
|
||||||
|
|
||||||
|
需求:使用Predicate接口判断指定的字符串长度是否>3 ,(包含java)
|
||||||
|
*/
|
||||||
|
public class Demo08 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/*boolean result = method(new Predicate<String>() {
|
||||||
|
@Override
|
||||||
|
public boolean test(String s) {
|
||||||
|
*//*if (s.length() > 3) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
} *//*
|
||||||
|
return s.length() > 3;
|
||||||
|
}
|
||||||
|
}, "woaijava");*/
|
||||||
|
|
||||||
|
//--------------------使用函数式编程思想优化代码,重点关注做什么,而不是怎么做---------------------
|
||||||
|
// boolean result = method((String s)->{return s.length()>3;},"woaijava");
|
||||||
|
boolean result = method(s-> s.length()>3,"woaijava");
|
||||||
|
|
||||||
|
boolean result1 = method((String s)->{return s.contains("java");},"woaijaa");
|
||||||
|
|
||||||
|
System.out.println("是否满足长度大于3条件:"+result);
|
||||||
|
System.out.println("是否满足包含java:"+result1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义出一个返回值为boolean,参数是Predicate接口,String的数据的方法
|
||||||
|
public static boolean method(Predicate<String> predicate, String s) {
|
||||||
|
boolean result = predicate.test(s);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Predicate的and方法
|
||||||
|
|
||||||
|
default Predicate<T> and(Predicate<? super T> other)
|
||||||
|
返回一个组合的谓词,表示该谓词与另一个谓词的短路逻辑AND &&。
|
||||||
|
|
||||||
|
需求:对一个字符串,使用2个predicate,判断它是否以h开头,它的长度是否大于3
|
||||||
|
|
||||||
|
boolean test(T)
|
||||||
|
*/
|
||||||
|
public class Demo09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/*method((String s)->{return s.startsWith("h");},
|
||||||
|
(String s)->{return s.length() >3;},
|
||||||
|
"helloworld");*/
|
||||||
|
|
||||||
|
method( s-> s.startsWith("h"),
|
||||||
|
(String s)->{return s.length() >3;},
|
||||||
|
"helloworld");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义一个方法,接收2个判断条件predicate,1个要判断的字符串,没有返回值
|
||||||
|
public static void method(Predicate<String> predicate1, Predicate<String> predicate2, String str) {
|
||||||
|
/*boolean result1 = predicate1.test(str);
|
||||||
|
boolean result2 = predicate2.test(str);
|
||||||
|
boolean result = result1&&result2;*/
|
||||||
|
|
||||||
|
// boolean result = predicate1.test(str)&&predicate2.test(str);
|
||||||
|
Predicate<String> newPredicate = predicate1.and(predicate2);
|
||||||
|
boolean result = newPredicate.test(str);
|
||||||
|
|
||||||
|
System.out.println("最终结果:"+result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;/*
|
||||||
|
Predicate的or方法
|
||||||
|
|
||||||
|
需求:对一个字符串,使用2个predicate,判断它是否以h开头或者它的长度是否大于3
|
||||||
|
|
||||||
|
boolean test(T)
|
||||||
|
*/
|
||||||
|
public class Demo10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
method( s-> s.startsWith("h"),
|
||||||
|
(String s)->{return s.length() >3;},
|
||||||
|
"el");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义一个方法,接收2个判断条件predicate,1个要判断的字符串,没有返回值
|
||||||
|
public static void method(Predicate<String> predicate1, Predicate<String> predicate2, String str) {
|
||||||
|
/*boolean result1 = predicate1.test(str);
|
||||||
|
boolean result2 = predicate2.test(str);
|
||||||
|
boolean result = result1||result2;*/
|
||||||
|
|
||||||
|
// boolean result = predicate1.test(str)||predicate2.test(str);
|
||||||
|
Predicate<String> newPredicate = predicate1.or(predicate2);
|
||||||
|
boolean result = newPredicate.test(str);
|
||||||
|
|
||||||
|
System.out.println("最终结果:"+result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Predicate的negate方法
|
||||||
|
|
||||||
|
需求:对一个字符串,使用1个predicate,判断它是否以h开头,结果取反
|
||||||
|
|
||||||
|
boolean test(T)
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Demo11 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
method( s-> s.startsWith("h"),
|
||||||
|
"el");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义一个方法,接收2个判断条件predicate,1个要判断的字符串,没有返回值
|
||||||
|
public static void method(Predicate<String> predicate1, String str) {
|
||||||
|
|
||||||
|
boolean result = predicate1.negate().test(str);
|
||||||
|
System.out.println("最终结果:"+result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
/*
|
||||||
|
数组当中有多条“姓名+性别”的信息如下,请通过Predicate接口的拼装将符合要求的字符串筛选到集合ArrayList中,需要同时满足两个条件:
|
||||||
|
|
||||||
|
1. 必须为女生;
|
||||||
|
2. 姓名为4个字
|
||||||
|
|
||||||
|
分析:
|
||||||
|
1.一个predicate判断是否是女生
|
||||||
|
2.一个predicate判断名字是否长度为4
|
||||||
|
3.创建集合,遍历数组中的字符串,满足条件的添加到集合中
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo12 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String[] array = { "迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女" };
|
||||||
|
//"迪丽热巴,女"-----["迪丽热巴","女"]
|
||||||
|
ArrayList<String> list = getList(
|
||||||
|
(String s)->{
|
||||||
|
return s.split(",")[1].equals("女");
|
||||||
|
},
|
||||||
|
(String s)->{
|
||||||
|
return s.split(",")[0].length() ==4;
|
||||||
|
},
|
||||||
|
array
|
||||||
|
);
|
||||||
|
System.out.println(list);
|
||||||
|
}
|
||||||
|
//定义一个方法,接收2个条件predicate,数组,返回一个集合
|
||||||
|
public static ArrayList<String> getList(Predicate<String> p1, Predicate<String> p2, String[] arr) {
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
|
//遍历数组,使用2个条件分别对指定的内容进行判断,满足则添加到集合中
|
||||||
|
for (String s : arr) {//"迪丽热巴,女"
|
||||||
|
if (p1.and(p2).test(s)) {
|
||||||
|
list.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习的内容:
|
||||||
|
6.常用的函数式接口-Function,Function就是一个数据工厂,用来进行数据转换
|
||||||
|
Interface Function<T,R>
|
||||||
|
参数类型
|
||||||
|
T - 原料的类型 - 参数
|
||||||
|
R - 产品的类型 - 返回值
|
||||||
|
抽象方法:
|
||||||
|
R apply(T t):表示进T类型的数据转换成R类型的数据
|
||||||
|
默认方法:
|
||||||
|
default <V> Function<T,V> andThen(Function after) 合并2个Function接口,生成一个新的,先执行this的apply方法,再执行after的apply方法
|
||||||
|
default <V> Function<V,R> compose(Function before) 与以上的方法完全相反
|
||||||
|
|
||||||
|
需求:"100"->100
|
||||||
|
*/
|
||||||
|
public class Demo13 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// method(str->Integer.parseInt(str),"100");
|
||||||
|
// method((String str)->{return Integer.parseInt(str);},"100");
|
||||||
|
//----------------------------------也可以这么做-----------------------------
|
||||||
|
//定义一个转换工厂
|
||||||
|
Function<String,Integer> function = str->Integer.parseInt(str);//接口的多态
|
||||||
|
Integer result = function.apply("10");
|
||||||
|
System.out.println(result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义出一个方法,接收的是一个数据工厂(String->Integer),字符串参数,没有返回值
|
||||||
|
public static void method(Function<String,Integer>function,String str) {
|
||||||
|
Integer result = function.apply(str);
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习内容:Function中的andThen方法
|
||||||
|
|
||||||
|
default <V> Function<T,V> andThen(Function after) 合并2个Function接口,生成一个新的,先执行this的apply方法,再执行after的apply方法
|
||||||
|
default <V> Function<V,R> compose(Function before) 与以上的方法完全相反
|
||||||
|
|
||||||
|
需求:
|
||||||
|
1."10"->10
|
||||||
|
2.10->100
|
||||||
|
分析:
|
||||||
|
1.一个Function 将"10" 转换成10
|
||||||
|
2.另一个Function 将10 转换成100
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo14 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
method( s->Integer.parseInt(s),
|
||||||
|
i->i*10
|
||||||
|
,"10"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义一个方法,接收2个数据工厂,1个数据原料,打印最终结果,不需要返回值
|
||||||
|
public static void method(Function<String,Integer> function1 ,Function<Integer,Integer> function2,String s) {
|
||||||
|
/*Integer result1 = function1.apply(s);
|
||||||
|
Integer result2 = function2.apply(result1);*/
|
||||||
|
/* Integer result2 = function2.apply(function1.apply(s));
|
||||||
|
System.out.println(result2);*/
|
||||||
|
|
||||||
|
/*default <V> Function<T,V> andThen(Function after) 合并2个Function接口,
|
||||||
|
生成一个新的,先执行this的apply方法,再执行after的apply方法*/
|
||||||
|
|
||||||
|
System.out.println(function1.andThen(function2).apply(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/*
|
||||||
|
请使用Function进行函数模型的拼接,按照顺序需要执行的多个函数操作为:
|
||||||
|
|
||||||
|
1. 将字符串截取数字年龄部分,得到字符串;
|
||||||
|
2. 将上一步的字符串转换成为int类型的数字;(String->integer)
|
||||||
|
3. 将上一步的int数字累加100,得到结果int数字。(integer->integer)
|
||||||
|
|
||||||
|
常见函数式接口总结:
|
||||||
|
1.consumer(消费者) :接收一个参数,没有返回值 p->{}
|
||||||
|
2.Supplier(生产者) :没有参数,有一个返回值 ()->{return ..}
|
||||||
|
3.predicate(数据筛选):接收一个参数,返回一个boolean p->{return ...}
|
||||||
|
4.function(数据转换工厂):接收一个参数,有一个返回值 p->{return ...}
|
||||||
|
*/
|
||||||
|
public class Demo15 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str = "赵丽颖,20";
|
||||||
|
//定义一个数据工厂String->integer
|
||||||
|
Function<String,Integer> function1 = s-> Integer.parseInt(s.split(",")[1]);
|
||||||
|
//定义一个数据工厂integer->integer
|
||||||
|
Function<Integer,Integer> function2 = i->i+100;
|
||||||
|
|
||||||
|
Integer result = function1.andThen(function2).apply(str);
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyInterface {
|
||||||
|
int getSum(int a,int b);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyInterface1 {
|
||||||
|
void method1(int i);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyInterface2 {
|
||||||
|
void method2(int a);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.inmind.functional_interface02;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface MyInterface3 {
|
||||||
|
void method3(int c);
|
||||||
|
}
|
||||||
53
javaSE-day07/src/com/inmind/lambda01/Demo01.java
Normal file
53
javaSE-day07/src/com/inmind/lambda01/Demo01.java
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package com.inmind.lambda01;
|
||||||
|
/*
|
||||||
|
使用匿名内部类实现多线程
|
||||||
|
|
||||||
|
匿名内部类的语法:
|
||||||
|
new 父类|接口(){
|
||||||
|
要重写的方法
|
||||||
|
}
|
||||||
|
|
||||||
|
线程的启动方式有2种:
|
||||||
|
1.继承Thread
|
||||||
|
2.实现Runnable(重点)
|
||||||
|
|
||||||
|
---------------------------------------
|
||||||
|
函数式编程思想:重点关注做什么,而不是怎么做。
|
||||||
|
以下启动线程代码,必须按照面向对象的语法,new Runnable(),才能写run()方法,其实我们最终想做的事情只是要编写出run方法的方法体而已
|
||||||
|
|
||||||
|
我们可以这么理解,函数式编程思想:做什么就是要重写接口中方法的方法体,而之前的创建对象,重写的语法都省略掉
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------------
|
||||||
|
Lambda的格式和前提条件
|
||||||
|
函数式编程思想:重点关注做什么,而不是怎么做,在这个概念中做什么实际上就是一个方法的方法体
|
||||||
|
针对一个方法而言,它有3个核心:1.参数列表 2.方法体 3.返回值
|
||||||
|
():参数列表
|
||||||
|
->:参数列表用来调用哪段代码
|
||||||
|
{}:方法体,根据是否有返回值,动态添加return
|
||||||
|
|
||||||
|
Lambda的格式:
|
||||||
|
(参数列表)->{
|
||||||
|
方法体;
|
||||||
|
[return]
|
||||||
|
}
|
||||||
|
|
||||||
|
lambda表达式的前提条件:
|
||||||
|
1.需要一个有且仅有一个抽象方法的接口(函数式接口)
|
||||||
|
2.接口作为参数或者变量类型
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println(Thread.currentThread().getName()+"线程启动了");
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
//-------------------------------以下使用函数式编程思想优化以上的代码(lambda)----------------------------------
|
||||||
|
new Thread(()->{ System.out.println(Thread.currentThread().getName()+"线程启动了");}).start();
|
||||||
|
//以下代码中方法体只有一条java语句,省略了大括号,分号
|
||||||
|
new Thread(()->System.out.println(Thread.currentThread().getName()+"线程启动了")).start();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
35
javaSE-day07/src/com/inmind/lambda01/Demo02.java
Normal file
35
javaSE-day07/src/com/inmind/lambda01/Demo02.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package com.inmind.lambda01;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习的内容:使用Lambda表达式简化有参数有返回值的方法(比较器)
|
||||||
|
|
||||||
|
使用比较器,对学生对象按成绩降序排序
|
||||||
|
*/
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ArrayList<Student> students = new ArrayList<>();
|
||||||
|
students.add(new Student("张三1", 95));
|
||||||
|
students.add(new Student("张三2", 90));
|
||||||
|
students.add(new Student("张三3", 98));
|
||||||
|
|
||||||
|
/*Collections.sort(students, new Comparator<Student>() {
|
||||||
|
*//*
|
||||||
|
我-它:升序
|
||||||
|
它-我:降序
|
||||||
|
*//*
|
||||||
|
@Override
|
||||||
|
public int compare(Student o1, Student o2) {
|
||||||
|
return o2.score - o1.score;
|
||||||
|
}
|
||||||
|
});*/
|
||||||
|
|
||||||
|
// Collections.sort(students,(Student o1, Student o2)->{ return o2.score - o1.score;});
|
||||||
|
Collections.sort(students,(o1, o2)-> o2.score - o1.score);
|
||||||
|
|
||||||
|
System.out.println(students);
|
||||||
|
}
|
||||||
|
}
|
||||||
12
javaSE-day07/src/com/inmind/lambda01/Demo03.java
Normal file
12
javaSE-day07/src/com/inmind/lambda01/Demo03.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.inmind.lambda01;
|
||||||
|
/*
|
||||||
|
lambda表达式的省略语法:
|
||||||
|
1.参数列表()中的数据类型可以省略
|
||||||
|
2.如果(),参数列表中只有一个参数,并且数据类型已经省略,那么小括号也可以省略
|
||||||
|
3.如果{}中只有一条java语句,那么大括号,return,分号可以同时省略
|
||||||
|
*/
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
19
javaSE-day07/src/com/inmind/lambda01/Student.java
Normal file
19
javaSE-day07/src/com/inmind/lambda01/Student.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.inmind.lambda01;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
String name;
|
||||||
|
int score;
|
||||||
|
|
||||||
|
public Student(String name, int score) {
|
||||||
|
this.name = name;
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Student{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", score=" + score +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
17
javaSE-day07/src/com/inmind/stream03/ConcatDemo23.java
Normal file
17
javaSE-day07/src/com/inmind/stream03/ConcatDemo23.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
19.Stream流中的常用方法_concat
|
||||||
|
static <T> Stream<T> concat(Stream a, Stream b) 合并2个流生成一个新的流
|
||||||
|
*/
|
||||||
|
public class ConcatDemo23 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Stream<Integer> stream1 = Stream.of(1, 2, 3);
|
||||||
|
Stream<Integer> stream2 = Stream.of(4, 5, 6);
|
||||||
|
|
||||||
|
Stream<Integer> stream = Stream.concat(stream1, stream2);
|
||||||
|
stream.forEach(i-> System.out.println(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
14
javaSE-day07/src/com/inmind/stream03/CountDemo20.java
Normal file
14
javaSE-day07/src/com/inmind/stream03/CountDemo20.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Stream流中的常用方法_count
|
||||||
|
获取流中元素的个数
|
||||||
|
*/
|
||||||
|
public class CountDemo20 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Stream<Integer> stream = Stream.of(1, 2, 4);
|
||||||
|
System.out.println(stream.count());//3
|
||||||
|
}
|
||||||
|
}
|
||||||
32
javaSE-day07/src/com/inmind/stream03/Demo26.java
Normal file
32
javaSE-day07/src/com/inmind/stream03/Demo26.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
25.把Stream中的数据收集到集合
|
||||||
|
Stream接口的一个方法collect
|
||||||
|
R collect(Collector collector) 使用 Collector对此流的元素执行 mutable reduction操作。
|
||||||
|
|
||||||
|
Collector:表示收集者是一个接口。它的实现类对象,我们直接使用工具类获取
|
||||||
|
Collectors:
|
||||||
|
toList():List集合的收集者
|
||||||
|
toSet():Set集合的收集者
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo26 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//获取流
|
||||||
|
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 5, 6, 6);
|
||||||
|
//转为List集合(数据可以重复)
|
||||||
|
List<Integer> list = stream.collect(Collectors.toList());
|
||||||
|
System.out.println(list);
|
||||||
|
//转为Set集合(数据不能重复)
|
||||||
|
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5, 5, 6, 6);
|
||||||
|
Set<Integer> set = stream1.collect(Collectors.toSet());//Stream流重复
|
||||||
|
System.out.println(set);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
javaSE-day07/src/com/inmind/stream03/FilterDemo19.java
Normal file
23
javaSE-day07/src/com/inmind/stream03/FilterDemo19.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
15.Stream流中的常用方法_filter
|
||||||
|
Stream<T> filter(Predicate predicate) 返回由与此给定谓词匹配的此流的元素组成的流。
|
||||||
|
|
||||||
|
知识点回顾:Predicate:对指定的数据进行筛选判断,符合条件的就返回true,反之
|
||||||
|
boolean test(T t) ---->对应的lambda表达式:t->{return ...}
|
||||||
|
|
||||||
|
filter方法的执行原理:
|
||||||
|
它会将stream流 中的每个元素作为以上lambda的参数执行方法体中的代码,如果返回true的数据会保留在新的Stream流中如果返回false那么就筛选掉
|
||||||
|
*/
|
||||||
|
//需求:将整数 > 6的元素保留,其他的不要
|
||||||
|
public class FilterDemo19 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//获取流
|
||||||
|
Stream<Integer> stream = Stream.of(1, 100, 2, 88, 11, 5);
|
||||||
|
//过滤流中的元素
|
||||||
|
stream.filter(i -> i > 6).forEach(i-> System.out.println(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
23
javaSE-day07/src/com/inmind/stream03/ForEachDemo18.java
Normal file
23
javaSE-day07/src/com/inmind/stream03/ForEachDemo18.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习内容:Stream流中的常用方法_forEach
|
||||||
|
void forEach(Consumer action) 对此流的每个元素执行消费操作。
|
||||||
|
|
||||||
|
知识点回顾: Consumer:接收一个参数用来操作,并没有返回值
|
||||||
|
抽象方法:void accept(T t) --->对应的lambda对应的格式:t->{}
|
||||||
|
foreach的执行原理:它会将stream流 中的每个元素作为以上lambda的参数执行方法体.
|
||||||
|
*/
|
||||||
|
//需求:将整数进行相加10,再打印操作
|
||||||
|
public class ForEachDemo18 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//获取流
|
||||||
|
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||||
|
//遍历操作
|
||||||
|
stream.forEach((Integer i)->{
|
||||||
|
System.out.println(i+10);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
14
javaSE-day07/src/com/inmind/stream03/LimitDemo21.java
Normal file
14
javaSE-day07/src/com/inmind/stream03/LimitDemo21.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Stream流中的常用方法_limit
|
||||||
|
将指定的流的前几个元素,组成一个新的流返回
|
||||||
|
*/
|
||||||
|
public class LimitDemo21 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
|
||||||
|
stream.limit(3).forEach(i-> System.out.println(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
32
javaSE-day07/src/com/inmind/stream03/MapDemo24.java
Normal file
32
javaSE-day07/src/com/inmind/stream03/MapDemo24.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
19.Stream中的map方法
|
||||||
|
将一种类型的流的数据,全部映射(转换)成另一种数据类型的流的时候,使用map方法
|
||||||
|
|
||||||
|
<R> Stream<R> map(Function mapper) 返回由给定函数应用于此流的元素的结果组成的流。
|
||||||
|
|
||||||
|
知识点回顾:
|
||||||
|
Function<T,R>:用来数据转换
|
||||||
|
T:参数
|
||||||
|
R:返回值
|
||||||
|
|
||||||
|
抽象方法: R apply(T t)
|
||||||
|
lambda表达式:(t)->{return R;}
|
||||||
|
|
||||||
|
map方法的执行过程:将流中的每个元素传入到以上的lambda表达式中,将转换后的每个结果都保存到新的流对象中
|
||||||
|
|
||||||
|
|
||||||
|
需求:Stream<String> -> Stream<Integer>
|
||||||
|
{"10","110","100"}->{10,110,100}
|
||||||
|
*/
|
||||||
|
public class MapDemo24 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//获取字符串流
|
||||||
|
Stream<String> stream = Stream.of("10", "110", "100");
|
||||||
|
Stream<Integer> stream1 = stream.map((s -> Integer.parseInt(s)));
|
||||||
|
stream1.forEach(i-> System.out.println(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
16
javaSE-day07/src/com/inmind/stream03/Person.java
Normal file
16
javaSE-day07/src/com/inmind/stream03/Person.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
String name;
|
||||||
|
|
||||||
|
public Person(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Person{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
13
javaSE-day07/src/com/inmind/stream03/SkipDemo22.java
Normal file
13
javaSE-day07/src/com/inmind/stream03/SkipDemo22.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
18.Stream流中的常用方法_skip
|
||||||
|
跳过指定的流的前几个元素,将剩下的元素组成一个新的流返回
|
||||||
|
*/
|
||||||
|
public class SkipDemo22 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Stream.of(1, 2, 3, 4, 5).skip(2).forEach(i-> System.out.println(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
48
javaSE-day07/src/com/inmind/stream03/StreamDemo16.java
Normal file
48
javaSE-day07/src/com/inmind/stream03/StreamDemo16.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习内容:传统操作集合和Stream流操作集合的对比
|
||||||
|
|
||||||
|
需求:
|
||||||
|
1. 首先筛选所有姓张的人;
|
||||||
|
2. 然后筛选名字有三个字的人;
|
||||||
|
3. 最后进行对结果进行打印输出。
|
||||||
|
*/
|
||||||
|
public class StreamDemo16 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
list.add("张无忌");
|
||||||
|
list.add("周芷若");
|
||||||
|
list.add("赵敏");
|
||||||
|
list.add("张强");
|
||||||
|
list.add("张三丰");
|
||||||
|
|
||||||
|
//1. 首先筛选所有姓张的人;
|
||||||
|
ArrayList<String> list1 = new ArrayList<>();
|
||||||
|
for (String s : list) {
|
||||||
|
if (s.startsWith("张")) {
|
||||||
|
list1.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//2. 然后筛选名字有三个字的人;
|
||||||
|
ArrayList<String> list2 = new ArrayList<>();
|
||||||
|
for (String s : list1) {
|
||||||
|
if (s.length() == 3) {
|
||||||
|
list2.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(list2);
|
||||||
|
//-----------------------------------以下使用Stream流优化以上的代码 -----------------------------
|
||||||
|
List<String> list3 = list.stream()
|
||||||
|
.filter(s -> s.startsWith("张"))
|
||||||
|
.filter(s -> s.length() == 3)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
System.out.println(list3);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
57
javaSE-day07/src/com/inmind/stream03/StreamDemo17.java
Normal file
57
javaSE-day07/src/com/inmind/stream03/StreamDemo17.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习内容:获取Stream流的两种方式(重点)
|
||||||
|
Stream流的获取方式:
|
||||||
|
1.单列集合Collection的stream方法,得到单列集合的stream流
|
||||||
|
Collection接口
|
||||||
|
default Stream<E> stream() 返回一个序列 Stream与此集合作为其来源。
|
||||||
|
2.Stream接口的of方法,将多个数据组成一个流
|
||||||
|
Stream接口
|
||||||
|
static <T> Stream<T> of(T... values) 返回其元素是指定值的顺序排序流。
|
||||||
|
*/
|
||||||
|
public class StreamDemo17 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//1.单列集合获取流
|
||||||
|
//list集合:ArrayList LinkedList
|
||||||
|
List<String> lists = new ArrayList<>();
|
||||||
|
lists.add("张无忌");
|
||||||
|
lists.add("周芷若");
|
||||||
|
lists.add("赵敏");
|
||||||
|
lists.add("张三丰");
|
||||||
|
Stream<String> stream = lists.stream();//多态
|
||||||
|
System.out.println(stream);
|
||||||
|
|
||||||
|
//set集合:HashSet LinkedHashSet TreeSet
|
||||||
|
Set<String> sets = new HashSet<>();
|
||||||
|
sets.add("张无忌");
|
||||||
|
sets.add("周芷若");
|
||||||
|
sets.add("赵敏");
|
||||||
|
sets.add("张三丰");
|
||||||
|
Stream<String> stream1 = sets.stream();
|
||||||
|
System.out.println(stream1);
|
||||||
|
|
||||||
|
//2.双列集合获取流
|
||||||
|
//Map双列集合没有定义stream方法,只能通过转为单列集合间接获取流
|
||||||
|
//间接获取流:1.所有的键的集合keySet() 2.所有的值的集合values() 3.所有的键值对对象的集合
|
||||||
|
//2.1 所有的键的集合keySet()
|
||||||
|
HashMap<String, String> map = new HashMap<>();
|
||||||
|
map.put("刘备", "孙尚香");
|
||||||
|
map.put("吕布", "貂蝉");
|
||||||
|
Set<String> set = map.keySet();
|
||||||
|
Stream<String> stream2 = set.stream();
|
||||||
|
|
||||||
|
//2.2 所有的值的集合values()
|
||||||
|
Collection<String> values = map.values();
|
||||||
|
Stream<String> stream3 = values.stream();
|
||||||
|
//2.3 所有的键值对对象的集合
|
||||||
|
Set<Map.Entry<String, String>> entries = map.entrySet();
|
||||||
|
Stream<Map.Entry<String, String>> stream4 = entries.stream();
|
||||||
|
|
||||||
|
//3.Stream接口的of方法
|
||||||
|
Stream<Integer> stream5 = Stream.of(1, 2, 3, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
70
javaSE-day07/src/com/inmind/stream03/StreamTest25.java
Normal file
70
javaSE-day07/src/com/inmind/stream03/StreamTest25.java
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package com.inmind.stream03;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
现在有两个ArrayList集合存储队伍当中的多个成员姓名,要求使用传统的for循环(或增强for循环)依次进行以下若干操作步骤:
|
||||||
|
|
||||||
|
1. 第一个队伍只要名字为3个字的成员姓名;
|
||||||
|
2. 第一个队伍筛选之后只要前3个人;
|
||||||
|
3. 第二个队伍只要姓张的成员姓名;
|
||||||
|
4. 第二个队伍筛选之后不要前2个人;
|
||||||
|
5. 将两个队伍合并为一个队伍;
|
||||||
|
6.根据姓名创建Person对象
|
||||||
|
7. 打印整个队伍的Person信息。
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class StreamTest25 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<String> one = new ArrayList<>();
|
||||||
|
one.add("迪丽热巴");
|
||||||
|
one.add("宋远桥");
|
||||||
|
one.add("苏星河");
|
||||||
|
one.add("老子");
|
||||||
|
one.add("庄子");
|
||||||
|
one.add("孙子");
|
||||||
|
one.add("洪七公");
|
||||||
|
one.add("洪八公");
|
||||||
|
|
||||||
|
List<String> two = new ArrayList<>();
|
||||||
|
two.add("古力娜扎");
|
||||||
|
two.add("张无忌");
|
||||||
|
two.add("张三丰");
|
||||||
|
two.add("赵丽颖");
|
||||||
|
two.add("张二狗");
|
||||||
|
two.add("张天爱");
|
||||||
|
two.add("张三");
|
||||||
|
|
||||||
|
/*
|
||||||
|
1. 第一个队伍只要名字为3个字的成员姓名;
|
||||||
|
2. 第一个队伍筛选之后只要前3个人;
|
||||||
|
* *//*
|
||||||
|
|
||||||
|
Stream<String> stream1 = one.stream().filter(s -> s.length() == 3).limit(3);
|
||||||
|
|
||||||
|
*//*
|
||||||
|
3. 第二个队伍只要姓张的成员姓名;
|
||||||
|
4. 第二个队伍筛选之后不要前2个人;
|
||||||
|
*//*
|
||||||
|
Stream<String> stream2 = two.stream().filter(s -> s.startsWith("张")).skip(2);
|
||||||
|
|
||||||
|
//5. 将两个队伍合并为一个队伍;
|
||||||
|
Stream<String> stream = Stream.concat(stream1, stream2);
|
||||||
|
//6.根据姓名创建Person对象(String流转为Person流)
|
||||||
|
Stream<Person> personStream = stream.map(s -> {
|
||||||
|
return new Person(s);
|
||||||
|
});
|
||||||
|
//7. 打印整个队伍的Person信息。
|
||||||
|
personStream.forEach(p-> System.out.println(p));*/
|
||||||
|
|
||||||
|
|
||||||
|
Stream.concat(
|
||||||
|
one.stream().filter(s -> s.length() == 3).limit(3),
|
||||||
|
two.stream().filter(s -> s.startsWith("张"))
|
||||||
|
.skip(2))
|
||||||
|
.map(s -> new Person(s)).forEach(p-> System.out.println(p));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
32
javaSE-day08/src/com/inmind/digui02/Demo08.java
Normal file
32
javaSE-day08/src/com/inmind/digui02/Demo08.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package com.inmind.digui02;
|
||||||
|
/*
|
||||||
|
递归的概念
|
||||||
|
|
||||||
|
递归:一个方法直接或者间接调用自己
|
||||||
|
直接调用:A->A->A
|
||||||
|
间接调用:A->B->A->B
|
||||||
|
|
||||||
|
递归代码必须要有结束条件,如果没有结束条件那么一定会出栈内存溢出错误.
|
||||||
|
|
||||||
|
扩展:
|
||||||
|
内存泄漏:一个对象已经再也不会使用,但是它还占用着内存
|
||||||
|
内存溢出:内存的空间总共有4G,但是我想开辟超过4G内存的空间,这就是内存溢出
|
||||||
|
*/
|
||||||
|
public class Demo08 {
|
||||||
|
private static int num = 1;
|
||||||
|
public static void main(String[] args) {
|
||||||
|
methodA();
|
||||||
|
}
|
||||||
|
public static void methodA(){
|
||||||
|
num++;
|
||||||
|
System.out.println(num);
|
||||||
|
if (num == 10000) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
methodB();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void methodB(){
|
||||||
|
methodA();
|
||||||
|
}
|
||||||
|
}
|
||||||
42
javaSE-day08/src/com/inmind/digui02/Demo09.java
Normal file
42
javaSE-day08/src/com/inmind/digui02/Demo09.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package com.inmind.digui02;
|
||||||
|
/*
|
||||||
|
递归求阶乘的代码
|
||||||
|
|
||||||
|
5! = 5*4*3*2*1
|
||||||
|
4! = 4*3*2*1
|
||||||
|
3! = 3*2*1
|
||||||
|
2! = 2*1
|
||||||
|
1! =1
|
||||||
|
|
||||||
|
阶乘另一种计算方式:
|
||||||
|
5! = 5*4!
|
||||||
|
4! = 4*3!
|
||||||
|
3! = 3*2!
|
||||||
|
2! = 2*1!
|
||||||
|
1! =1
|
||||||
|
|
||||||
|
求阶乘的数学规律是 n! = n*(n-1)!
|
||||||
|
|
||||||
|
核心思想:递归代码如何编写??
|
||||||
|
1.判断当前是否是结束条件,如果是就结束(return)
|
||||||
|
2.如果不是结束条件,那么按数学规律或者逻辑规律调用自己
|
||||||
|
|
||||||
|
n=1就是结束条件
|
||||||
|
求阶乘的数学规律是 n! = n*(n-1)!
|
||||||
|
*/
|
||||||
|
public class Demo09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//计算5的阶乘
|
||||||
|
System.out.println(getJieCheng(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
//定义求n的阶乘的功能方法
|
||||||
|
public static int getJieCheng(int n) {
|
||||||
|
//1.判断当前是否是结束条件,如果是就结束(return)
|
||||||
|
if (n == 1) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//2.如果不是结束条件,那么按数学规律或者逻辑规律调用自己
|
||||||
|
return n*getJieCheng(n-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
javaSE-day08/src/com/inmind/digui02/Demo10.java
Normal file
29
javaSE-day08/src/com/inmind/digui02/Demo10.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package com.inmind.digui02;
|
||||||
|
/*
|
||||||
|
递归求累和的练习
|
||||||
|
5的累和:5+4+3+2+1 = 15
|
||||||
|
4的累和:4+3+2+1 = 10
|
||||||
|
3的累和:3+2+1 = 6
|
||||||
|
2的累和:2+1 = 3
|
||||||
|
1的累和:1
|
||||||
|
|
||||||
|
规律:n的累和:n+(n-1)的累和
|
||||||
|
结束条件:1的累和就是1
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//100的累和是多少
|
||||||
|
System.out.println(getSum(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
//计算n的累和
|
||||||
|
public static int getSum(int n) {
|
||||||
|
//结束条件:1的累和就是1
|
||||||
|
if (n == 1) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//规律:n的累和:n+(n-1)的累和
|
||||||
|
return n + getSum(n - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
45
javaSE-day08/src/com/inmind/digui02/Demo11.java
Normal file
45
javaSE-day08/src/com/inmind/digui02/Demo11.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.digui02;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/*
|
||||||
|
13.递归遍历文件夹的练习(重点)
|
||||||
|
需求:将D:\io_test里面的所有的子内容的名称都打印出来.
|
||||||
|
|
||||||
|
在遍历文件夹操作中,文件中不断地创建文件夹所以普通的循环遍历得子内容的代码实现不了
|
||||||
|
所以只能使用递归代码
|
||||||
|
|
||||||
|
遍历文件夹的递归代码:
|
||||||
|
1.结束条件:如果是文件就打印结束
|
||||||
|
2.如果是文件夹就继续遍历,如果是文件就打印结束
|
||||||
|
*/
|
||||||
|
public class Demo11 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
File file = new File("D:\\io_test");
|
||||||
|
System.out.println(file.getName());
|
||||||
|
//定义出一个方法,将指定文件夹中的内容遍历出来
|
||||||
|
getFiles(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
//将指定文件夹中的内容遍历出来
|
||||||
|
private static void getFiles(File file) {
|
||||||
|
//安全性验证
|
||||||
|
if (file.exists() && file.isFile()) {
|
||||||
|
System.out.println(file.getName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//这里file一定是文件夹
|
||||||
|
File[] files = file.listFiles();
|
||||||
|
//遍历数组
|
||||||
|
for (File f : files) {
|
||||||
|
if (f.isDirectory()) {
|
||||||
|
System.out.println(f.getName());
|
||||||
|
getFiles(f);
|
||||||
|
} else {
|
||||||
|
//是文件直接打印名字
|
||||||
|
System.out.println(f.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
javaSE-day08/src/com/inmind/digui02/Demo12.java
Normal file
35
javaSE-day08/src/com/inmind/digui02/Demo12.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package com.inmind.digui02;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/*
|
||||||
|
14.练习_打印指定D:\io_test目录下所有的txt文件
|
||||||
|
|
||||||
|
分析:遍历获取指定目录中所有的File对象,是.txt后缀(名称)的文件
|
||||||
|
|
||||||
|
如果是.txt后缀(名称)的文件则打印,其他文件就不操作,如果是文件夹则继续递归调用自己,
|
||||||
|
*/
|
||||||
|
public class Demo12 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个File对象
|
||||||
|
File file = new File("D:\\io_test");
|
||||||
|
getFiles(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void getFiles(File file) {
|
||||||
|
if (file.exists() && file.isFile()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
File[] files = file.listFiles();
|
||||||
|
for (File f : files) {
|
||||||
|
//.txt后缀(名称)的文件则打印
|
||||||
|
if (f.isFile() && f.getName().endsWith(".txt")) {
|
||||||
|
System.out.println(f.getName());
|
||||||
|
} else {
|
||||||
|
//其他文件夹则递归调用自己
|
||||||
|
getFiles(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
45
javaSE-day08/src/com/inmind/file01/FileDemo01.java
Normal file
45
javaSE-day08/src/com/inmind/file01/FileDemo01.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.file01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/*
|
||||||
|
在java中定义了一个类File,用来表示计算机中的文件或文件夹.
|
||||||
|
在英文中:
|
||||||
|
File:文件
|
||||||
|
Directory:目录,文件夹
|
||||||
|
path:路径
|
||||||
|
|
||||||
|
File类表示文件或者文件夹,该类的使用场景:操作计算机中的文件或文件夹.在IO流中大量使用,文件下载文件上传等功能
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------------------
|
||||||
|
构造方法:
|
||||||
|
file的构造方法:
|
||||||
|
File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
|
||||||
|
File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例。
|
||||||
|
File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例。
|
||||||
|
*/
|
||||||
|
public class FileDemo01 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
|
||||||
|
//D:\io_test\a.txt
|
||||||
|
File file = new File("D:\\io_test\\a.txt");//注意:反斜杠在java代码中有转义的意思,要使用2个\\表示是一个普通反斜杠的作用
|
||||||
|
System.out.println(file);//file类重写了toString,展示该类对象的时候,直接展示它的路径
|
||||||
|
|
||||||
|
/*
|
||||||
|
File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例。
|
||||||
|
以D:\io_test\a.txt为例:
|
||||||
|
父路径:D:\io_test\
|
||||||
|
子路径:a.txt
|
||||||
|
*/
|
||||||
|
File file1 = new File("D:\\io_test\\", "a.txt");
|
||||||
|
System.out.println(file1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例。
|
||||||
|
*/
|
||||||
|
File parent = new File("D:\\io_test");
|
||||||
|
File file2 = new File(parent, "a.txt");
|
||||||
|
System.out.println(file2);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
29
javaSE-day08/src/com/inmind/file01/FileDemo02.java
Normal file
29
javaSE-day08/src/com/inmind/file01/FileDemo02.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package com.inmind.file01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/*
|
||||||
|
File类的获取方法
|
||||||
|
public String getAbsolutePath() :返回此File的绝对路径名字符串。(重点)
|
||||||
|
public String getPath() :将此File转换为路径名字符串。(了解)
|
||||||
|
public String getName() :返回由此File表示的文件或目录的名称。(重点)
|
||||||
|
public long length() :返回由此File表示的文件的长度。
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.对于length只能来获取文件的大小,而不能获取文件夹的大小.
|
||||||
|
2.在UTF-8编码方式中,一个中文占3个字节
|
||||||
|
在GBK编码方式中,一个中文占2个字节
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class FileDemo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建:D:\io_test\a.txt的文件对象
|
||||||
|
File file = new File("D:/io_test/a1.txt");
|
||||||
|
System.out.println(file.getAbsolutePath());//绝对路径
|
||||||
|
System.out.println(file.getPath());//传入的是什么路径就是什么路径
|
||||||
|
System.out.println(file.getName());//文件或目录的名称
|
||||||
|
System.out.println(file.length());//文件的长度(单位:字节)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
22
javaSE-day08/src/com/inmind/file01/FileDemo03.java
Normal file
22
javaSE-day08/src/com/inmind/file01/FileDemo03.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.file01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/*
|
||||||
|
学习内容:相对路径和绝对路径(重点)
|
||||||
|
绝对路径:带有盘符名的路径
|
||||||
|
相对路径:不带有盘符名的路径
|
||||||
|
比如:
|
||||||
|
d:/io_test/a1.txt
|
||||||
|
io_test/a1.txt
|
||||||
|
|
||||||
|
注意:在javaSE工程中,相对路径是将当前的工程路径作为父路径,拼接上相对路径的子路径生成一个新的路径
|
||||||
|
*/
|
||||||
|
public class FileDemo03 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
File file = new File("a.txt");
|
||||||
|
System.out.println(file);//File类重写toString的本质就是getPath
|
||||||
|
System.out.println(file.getPath());
|
||||||
|
System.out.println(file.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
36
javaSE-day08/src/com/inmind/file01/FileDemo04.java
Normal file
36
javaSE-day08/src/com/inmind/file01/FileDemo04.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package com.inmind.file01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/*
|
||||||
|
File中判断的方法
|
||||||
|
public boolean exists() :此File表示的文件或目录是否实际存在。
|
||||||
|
public boolean isDirectory() :此File表示的是否为目录。
|
||||||
|
public boolean isFile() :此File表示的是否为文件。
|
||||||
|
|
||||||
|
注意:不存在的文件,所有的判断方法都是false
|
||||||
|
*/
|
||||||
|
public class FileDemo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
File file = new File("D:/io_test/a1.txt");
|
||||||
|
System.out.println(file.getAbsolutePath());
|
||||||
|
|
||||||
|
System.out.println(file.exists());//true
|
||||||
|
System.out.println(file.isDirectory());//false
|
||||||
|
System.out.println(file.isFile());//true
|
||||||
|
|
||||||
|
File file1 = new File("D:/io_test");
|
||||||
|
System.out.println(file1.getAbsolutePath());
|
||||||
|
|
||||||
|
System.out.println(file1.exists());//true
|
||||||
|
System.out.println(file1.isDirectory());//true
|
||||||
|
System.out.println(file1.isFile());//false
|
||||||
|
|
||||||
|
File file2 = new File("D:/io_test/b1.txt");
|
||||||
|
System.out.println(file2.getAbsolutePath());
|
||||||
|
|
||||||
|
System.out.println(file2.exists());//true
|
||||||
|
System.out.println(file2.isDirectory());//true
|
||||||
|
System.out.println(file2.isFile());//false
|
||||||
|
}
|
||||||
|
}
|
||||||
30
javaSE-day08/src/com/inmind/file01/FileDemo05.java
Normal file
30
javaSE-day08/src/com/inmind/file01/FileDemo05.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.file01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
File中的创建功能
|
||||||
|
public boolean createNewFile() :当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。(重点)
|
||||||
|
public boolean mkdir() :创建由此File表示的目录。
|
||||||
|
public boolean mkdirs() :创建由此File表示的目录,包括任何必需但不存在的父目录。(重点)
|
||||||
|
*/
|
||||||
|
public class FileDemo05 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
File file = new File("a.txt");
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.createNewFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
File file1 = new File("b.txt");
|
||||||
|
if (!file1.exists()) {
|
||||||
|
file1.mkdir();
|
||||||
|
}
|
||||||
|
|
||||||
|
File file2 = new File("b/a");
|
||||||
|
if (!file2.exists()) {
|
||||||
|
//如果想要将对应不存在的父路径(b文件夹)也一次性地创建出来,那么必须使用mkdirs方法
|
||||||
|
file2.mkdirs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
38
javaSE-day08/src/com/inmind/file01/FileDemo06.java
Normal file
38
javaSE-day08/src/com/inmind/file01/FileDemo06.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package com.inmind.file01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/*
|
||||||
|
public boolean delete() :删除由此File表示的文件或目录。
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.如果一个文件夹中还有子内容,这个文件夹不允许直接删除,先删除所有子内容,才能删除父文件夹
|
||||||
|
2.删除方法是物理删除,不能恢复
|
||||||
|
*/
|
||||||
|
public class FileDemo06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//删除文件
|
||||||
|
File file = new File("a.txt");
|
||||||
|
if (file.exists()) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
//删除文件夹
|
||||||
|
File file1 = new File("b.txt");
|
||||||
|
if (file1.exists()) {
|
||||||
|
file1.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
//删除带有子内容的文件夹
|
||||||
|
File file2 = new File("b/a");
|
||||||
|
if (file2.exists()) {
|
||||||
|
file2.delete();
|
||||||
|
}
|
||||||
|
File file3 = new File("b");
|
||||||
|
if (file3.exists()) {
|
||||||
|
file3.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
27
javaSE-day08/src/com/inmind/file01/FileDemo07.java
Normal file
27
javaSE-day08/src/com/inmind/file01/FileDemo07.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.file01;
|
||||||
|
|
||||||
|
import com.sun.jdi.PathSearchingVirtualMachine;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/*
|
||||||
|
File类的遍历方法
|
||||||
|
public String[] list() :返回一个String数组,表示该File目录中的所有子文件或目录的名称。
|
||||||
|
public File[] listFiles() :返回一个File数组,表示该File目录中的所有的子文件或目录。
|
||||||
|
*/
|
||||||
|
public class FileDemo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
File file = new File("D:\\io_test\\a");
|
||||||
|
String[] names = file.list();
|
||||||
|
System.out.println(names.length);//4
|
||||||
|
System.out.println(Arrays.toString(names));
|
||||||
|
|
||||||
|
//获取D:\io_test\a文件夹中的所有子内容的绝对路径
|
||||||
|
File[] files = file.listFiles();
|
||||||
|
System.out.println(files.length);//4
|
||||||
|
for (File f : files) {
|
||||||
|
System.out.println(f.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
javaSE-day08/src/com/inmind/filefilter03/Demo14.java
Normal file
36
javaSE-day08/src/com/inmind/filefilter03/Demo14.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package com.inmind.filefilter03;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
|
||||||
|
/*
|
||||||
|
练习_打印指定D:\io_test目录下所有的txt文件(文件过滤器的方式)
|
||||||
|
*/
|
||||||
|
public class Demo14 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建父目录的File对象
|
||||||
|
File file = new File("D:\\io_test");
|
||||||
|
getFiles(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void getFiles(File file) {
|
||||||
|
if (file.exists() && file.isFile()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//只需要获取.txt文件或者文件夹
|
||||||
|
File[] files = file.listFiles(new FileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File f) {
|
||||||
|
return (f.isFile() && f.getName().endsWith(".txt"))||f.isDirectory();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (File f : files) {
|
||||||
|
if (f.isFile()) {
|
||||||
|
System.out.println(f.getName());
|
||||||
|
} else {
|
||||||
|
getFiles(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
42
javaSE-day08/src/com/inmind/filefilter03/Demo15.java
Normal file
42
javaSE-day08/src/com/inmind/filefilter03/Demo15.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package com.inmind.filefilter03;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
|
||||||
|
/*
|
||||||
|
使用lambda编写文件过滤器的实现
|
||||||
|
*/
|
||||||
|
public class Demo15 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建父目录的File对象
|
||||||
|
File file = new File("D:\\io_test");
|
||||||
|
getFiles(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void getFiles(File file) {
|
||||||
|
if (file.exists() && file.isFile()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//只需要获取.txt文件或者文件夹
|
||||||
|
/* File[] files = file.listFiles(new FileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File f) {
|
||||||
|
return (f.isFile() && f.getName().endsWith(".txt"))||f.isDirectory();
|
||||||
|
}
|
||||||
|
});*/
|
||||||
|
|
||||||
|
/*File[] files = file.listFiles((File f)->{
|
||||||
|
return (f.isFile() && f.getName().endsWith(".txt"))||f.isDirectory();
|
||||||
|
});*/
|
||||||
|
|
||||||
|
File[] files = file.listFiles(f-> (f.isFile() && f.getName().endsWith(".txt"))||f.isDirectory());
|
||||||
|
|
||||||
|
for (File f : files) {
|
||||||
|
if (f.isFile()) {
|
||||||
|
System.out.println(f.getName());
|
||||||
|
} else {
|
||||||
|
getFiles(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.inmind.filefilter03;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/*
|
||||||
|
文件过滤器的基本使用
|
||||||
|
1.文件过滤器的相关的api:
|
||||||
|
File[] listFiles(FileFilter filter) 返回一个抽象路径名数组,表示由此抽象路径名表示的满足指定过滤器的目录中的文件和目录。
|
||||||
|
|
||||||
|
2.@FunctionalInterface
|
||||||
|
public interface FileFilter
|
||||||
|
抽象方法的介绍:
|
||||||
|
boolean accept(File pathname)中的参数pathname是一个文件或者文件夹对象,它就是当前调用listFiles方法的文件夹中的每个子元素,
|
||||||
|
每个子元素会依次进入到accept方法中,执行得到结果,如果结果是true就保存在返回值(File[] )中,否则就直接过滤掉。
|
||||||
|
|
||||||
|
3.过滤器的基本使用步骤:
|
||||||
|
1.创建一个文件夹对象
|
||||||
|
2.先定义一个符合业务的文件过滤器实现类
|
||||||
|
3.调用listFiles(FileFilter filter)
|
||||||
|
|
||||||
|
|
||||||
|
需求:将指定的目录中的一级子内容中,使用文件过滤器,直接获取到一级目录的.txt后缀的文件
|
||||||
|
*/
|
||||||
|
public class FileFilterDemo13 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//1.创建一个文件夹对象
|
||||||
|
File file = new File("D:\\io_test");
|
||||||
|
//2.先定义一个符合业务的文件过滤器实现类
|
||||||
|
FileFilterImpl fileFilter = new FileFilterImpl();
|
||||||
|
//3.调用listFiles(FileFilter filter)
|
||||||
|
File[] files = file.listFiles(fileFilter);
|
||||||
|
|
||||||
|
for (File f : files) {
|
||||||
|
System.out.println(f.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
javaSE-day08/src/com/inmind/filefilter03/FileFilterImpl.java
Normal file
11
javaSE-day08/src/com/inmind/filefilter03/FileFilterImpl.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.filefilter03;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
//文件过滤器的目标:目录的.txt后缀的文件
|
||||||
|
public class FileFilterImpl implements FileFilter {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File f) {
|
||||||
|
return f.isFile() && f.getName().endsWith(".txt");
|
||||||
|
}
|
||||||
|
}
|
||||||
40
javaSE-day08/src/com/inmind/regex04/Demo16.java
Normal file
40
javaSE-day08/src/com/inmind/regex04/Demo16.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package com.inmind.regex04;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/*
|
||||||
|
1. Regex:正则表达式是用字符串写成的一种规则
|
||||||
|
|
||||||
|
2. String类的方法matches匹配规则
|
||||||
|
1. 案例:检查QQ号码是否合法,规则: 必须全数字,不能0开头,最低5位,最高12位
|
||||||
|
2. 案例:检查手机号码是否合法,规则:必须1开头,第二位: 34578,全数字,总共11位
|
||||||
|
3. String类的方法split
|
||||||
|
1. 案例:切割字符串,返回数组
|
||||||
|
2. 案例:切割网络ip
|
||||||
|
*/
|
||||||
|
public class Demo16 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//1. 案例:切割字符串,返回数组
|
||||||
|
String str = "刘备,,关羽,,,张飞";
|
||||||
|
String[] strings = str.split(",+");
|
||||||
|
System.out.println(strings.length);
|
||||||
|
System.out.println(Arrays.toString(strings));
|
||||||
|
|
||||||
|
//2. 案例:切割网络ip
|
||||||
|
String ip = "192.168.1.1";
|
||||||
|
String[] ips = ip.split("\\.");//表示普通的点号
|
||||||
|
System.out.println(ips.length);
|
||||||
|
System.out.println(Arrays.toString(ips));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void matchTest(String[] args) {
|
||||||
|
//1. 案例:检查QQ号码是否合法,规则: 必须全数字,不能0开头,最低5位,最高12位
|
||||||
|
String qq = "12345678";
|
||||||
|
boolean matches = qq.matches("[1-9]{1}[0-9]{4,11}");
|
||||||
|
System.out.println(matches);
|
||||||
|
//2. 案例:检查手机号码是否合法,规则:必须1开头,第二位: 34578,全数字,总共11位
|
||||||
|
String tel = "13770858888";
|
||||||
|
System.out.println(tel.matches("[1]{1}[34578]{1}[0-9]{9}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
68
javaSE-day09/src/com/inmind/inputstream02/Demo06.java
Normal file
68
javaSE-day09/src/com/inmind/inputstream02/Demo06.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package com.inmind.inputstream02;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
在java中使用抽象类InputStream表示字节输入流
|
||||||
|
常用的子类:FileInputStream
|
||||||
|
FileInputStream的作用;将硬盘中的文件的数据读取到内存
|
||||||
|
构造方法:
|
||||||
|
FileInputStream(File file) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
|
||||||
|
FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
|
||||||
|
常用方法:
|
||||||
|
void close() 关闭资源
|
||||||
|
int read() 从该输入流读取一个字节的数据。返回值就是字节数据
|
||||||
|
int read(byte[] b) 从字节输入流中将字节数据读取到参数b数组中,返回的值读取的字节个数
|
||||||
|
|
||||||
|
字节输入流的基本使用
|
||||||
|
步骤:
|
||||||
|
1.创建对象
|
||||||
|
2.调用读方法
|
||||||
|
3.释放资源
|
||||||
|
*/
|
||||||
|
public class Demo06 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
/*
|
||||||
|
1.创建对象
|
||||||
|
注意:
|
||||||
|
1.创建输入流对象时,文件不存在是报异常java.io.FileNotFoundException: b.txt
|
||||||
|
2.创建输出流对象是,文件不存在才会新建
|
||||||
|
*/
|
||||||
|
FileInputStream fis = new FileInputStream("a.txt");
|
||||||
|
|
||||||
|
//2.调用读方法
|
||||||
|
//int read() 从该输入流读取一个字节的数据。返回值就是字节数据
|
||||||
|
/*int c = fis.read();
|
||||||
|
System.out.println(c);//97
|
||||||
|
|
||||||
|
c = fis.read();
|
||||||
|
System.out.println(c);//98
|
||||||
|
|
||||||
|
c = fis.read();
|
||||||
|
System.out.println(c);//99
|
||||||
|
|
||||||
|
c = fis.read();
|
||||||
|
System.out.println(c);//-1*/
|
||||||
|
|
||||||
|
/*使用while循环来优化以上代码
|
||||||
|
返回值不是-1,则一直读取
|
||||||
|
*/
|
||||||
|
int c;//用来接收字节输入流读取的字节数据
|
||||||
|
/*
|
||||||
|
循环条件:(c = fis.read())!=-1
|
||||||
|
执行顺序:
|
||||||
|
1.fis.read():从字节输入流中读取一个字节数据
|
||||||
|
2.c = fis.read():将读取到的字节数据赋值给变量c
|
||||||
|
3.(c = fis.read())!=-1:判断是否读取到字节数据,如果读到那么继续操作
|
||||||
|
*/
|
||||||
|
while ((c = fis.read())!= -1) {
|
||||||
|
System.out.println((char) c);//按照ascii码表展示
|
||||||
|
}
|
||||||
|
|
||||||
|
//3.释放资源
|
||||||
|
fis.close();
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
53
javaSE-day09/src/com/inmind/inputstream02/Demo07.java
Normal file
53
javaSE-day09/src/com/inmind/inputstream02/Demo07.java
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package com.inmind.inputstream02;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
用字节输入流一次读取一个字节数组
|
||||||
|
FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
|
||||||
|
int read(byte[] b) 从字节输入流中将字节数据读取到参数b数组中,返回的值读取的字节个数
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
FileInputStream fis = new FileInputStream("a.txt");
|
||||||
|
|
||||||
|
|
||||||
|
/*byte[] bytes = new byte[2];//用来保存字节输入流读取的多个字节数据(暂且是2个)
|
||||||
|
int len ; //用来保存读取到字节数据的个数
|
||||||
|
len = fis.read(bytes);
|
||||||
|
System.out.println(len);
|
||||||
|
System.out.println(new String(bytes,0,len));
|
||||||
|
|
||||||
|
len = fis.read(bytes);
|
||||||
|
System.out.println(len);
|
||||||
|
System.out.println(new String(bytes,0,len));
|
||||||
|
|
||||||
|
len = fis.read(bytes);
|
||||||
|
System.out.println(len);
|
||||||
|
System.out.println(new String(bytes,0,len));
|
||||||
|
|
||||||
|
len = fis.read(bytes);
|
||||||
|
System.out.println(len);*/
|
||||||
|
|
||||||
|
//int read(byte[] b) 一次读一个字节数组
|
||||||
|
byte[] bytes = new byte[1024];//用来保存读取字节数据的数组
|
||||||
|
int len;//保存读取到字节的个数(判断读几个字节,是否是末尾)
|
||||||
|
/*
|
||||||
|
循环条件:
|
||||||
|
(len = fis.read(bytes))!=-1
|
||||||
|
1.fis.read(bytes):将字节输入流中的字节数据读取到bytes数组中
|
||||||
|
2.len = fis.read(bytes):将读取到的字节数据的长度赋值给len
|
||||||
|
3.(len = fis.read(bytes))!=-1:判断read的返回值是否是-1,就是在判断是否有更多的数据
|
||||||
|
|
||||||
|
*/
|
||||||
|
while ((len = fis.read(bytes)) != -1) {
|
||||||
|
System.out.println(len);
|
||||||
|
System.out.println(new String(bytes,0,len));//读取多少个字节,就获取多少个
|
||||||
|
}
|
||||||
|
|
||||||
|
fis.close();
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
31
javaSE-day09/src/com/inmind/inputstream02/Test08.java
Normal file
31
javaSE-day09/src/com/inmind/inputstream02/Test08.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package com.inmind.inputstream02;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
一次读取一个字节,实现文件的复制
|
||||||
|
FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
|
||||||
|
int read() 从该输入流读取一个字节的数据。返回值就是字节数据
|
||||||
|
FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
|
||||||
|
void write(int b) 将指定的字节写入此文件输出流。
|
||||||
|
*/
|
||||||
|
public class Test08 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//创建流对象(输入,输出)
|
||||||
|
FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");
|
||||||
|
|
||||||
|
//不停地读取一个字节,写出一个字节,直到读完
|
||||||
|
int c;//保存读取的字节
|
||||||
|
while ((c = fis.read()) != -1) {
|
||||||
|
//写出1个字节
|
||||||
|
fos.write(c);
|
||||||
|
}
|
||||||
|
//释放资源
|
||||||
|
fis.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
31
javaSE-day09/src/com/inmind/inputstream02/Test09.java
Normal file
31
javaSE-day09/src/com/inmind/inputstream02/Test09.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package com.inmind.inputstream02;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
一次读取一个字节数组,实现文件的复制
|
||||||
|
FileInputStream(String name) 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
|
||||||
|
int read(byte[] b) 从字节输入流中将字节数据读取到参数b数组中,返回的值读取的字节个数
|
||||||
|
FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
|
||||||
|
void write(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流。
|
||||||
|
*/
|
||||||
|
public class Test09 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//创建流对象(输入,输出)
|
||||||
|
FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\3.jpg");
|
||||||
|
|
||||||
|
//不停地读取一个字节数组,写出一个字节数组,直到读完
|
||||||
|
int len;//保存读取到的字节的个数
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
while ((len = fis.read(bytes)) != -1) {
|
||||||
|
fos.write(bytes,0,len);//读多少个字节,一次性写出多少个字节
|
||||||
|
}
|
||||||
|
//资源释放
|
||||||
|
fis.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
22
javaSE-day09/src/com/inmind/io01/Demo01.java
Normal file
22
javaSE-day09/src/com/inmind/io01/Demo01.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.io01;
|
||||||
|
/*
|
||||||
|
学习内容:IO流的分类以及所有的顶层父类
|
||||||
|
|
||||||
|
IO流通过2种方式进行分类
|
||||||
|
按流向分(基于内存):
|
||||||
|
输入流
|
||||||
|
输出流
|
||||||
|
按传输的数据类型分:
|
||||||
|
字节流
|
||||||
|
字符流
|
||||||
|
在java中,IO流的分类按传输数据类型分
|
||||||
|
字节输入流:InputStream
|
||||||
|
字节输出流:OutputStream
|
||||||
|
|
||||||
|
字符输入流:Reader
|
||||||
|
字符输出流:Writer
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
}
|
||||||
21
javaSE-day09/src/com/inmind/io01/Demo03.java
Normal file
21
javaSE-day09/src/com/inmind/io01/Demo03.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package com.inmind.io01;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Demo03 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//void write(int b) 将指定的字节写入此文件输出流。
|
||||||
|
/*FileOutputStream fos = new FileOutputStream("a.txt");
|
||||||
|
//输出int超出了256的值,将高位舍弃只留一个字节的内容,再按照ascii码表显示
|
||||||
|
//30000%256
|
||||||
|
fos.write('中');
|
||||||
|
fos.close();*/
|
||||||
|
|
||||||
|
System.out.println('中');
|
||||||
|
System.out.println('中'+0);
|
||||||
|
System.out.println(20000%256);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
40
javaSE-day09/src/com/inmind/io01/Demo04.java
Normal file
40
javaSE-day09/src/com/inmind/io01/Demo04.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package com.inmind.io01;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/*
|
||||||
|
字节数组和字符串的相互转换
|
||||||
|
|
||||||
|
字节数组--->字符串:构造方法
|
||||||
|
String(byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组来构造新的 String 。
|
||||||
|
String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String
|
||||||
|
字符串--->字节数组:成员方法
|
||||||
|
byte[] getBytes() 使用平台的默认字符集将该 String编码为一系列字节,将结果存储到新的字节数组中。
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.UTF-8:字母和数字都只占1个字节,而一个中文占3个字节
|
||||||
|
2.GBK:字母和数字都只占1个字节,而一个中文占2个字节
|
||||||
|
*/
|
||||||
|
public class Demo04 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str = "abc";
|
||||||
|
//字符串--->字节数组
|
||||||
|
byte[] bytes = str.getBytes();
|
||||||
|
System.out.println(bytes.length);
|
||||||
|
System.out.println(Arrays.toString(bytes));
|
||||||
|
|
||||||
|
String str1 = "中国";
|
||||||
|
byte[] bytes1 = str1.getBytes();
|
||||||
|
System.out.println(bytes1.length);
|
||||||
|
System.out.println(Arrays.toString(bytes1));
|
||||||
|
|
||||||
|
//字节数组--->字符串
|
||||||
|
String s1 = new String(bytes);
|
||||||
|
System.out.println(s1);
|
||||||
|
String s2 = new String(bytes1);
|
||||||
|
System.out.println(s2);
|
||||||
|
|
||||||
|
String s3 = new String(bytes1, 3, 3);
|
||||||
|
System.out.println(s3);
|
||||||
|
}
|
||||||
|
}
|
||||||
33
javaSE-day09/src/com/inmind/io01/Demo05.java
Normal file
33
javaSE-day09/src/com/inmind/io01/Demo05.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package com.inmind.io01;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
文件的续写并写出字节数组
|
||||||
|
续写:默认会将原本文件中的数据覆盖,但是我们想继续拼接输入
|
||||||
|
续写就是使用重载的构造方法即可,true:续写 false:覆盖
|
||||||
|
FileOutputStream(String name, boolean append) 创建文件输出流以指定的名称写入文件。
|
||||||
|
写出字节数组
|
||||||
|
void write(byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流。
|
||||||
|
void write(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流。
|
||||||
|
----------------------------------------------------------------------------------
|
||||||
|
文件内容的换行
|
||||||
|
windows:\r\n
|
||||||
|
linux: \n
|
||||||
|
mac: \r
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
FileOutputStream fos = new FileOutputStream("a.txt", true);
|
||||||
|
|
||||||
|
// fos.write("我是谁,我在哪,我在干嘛".getBytes());
|
||||||
|
// fos.write("我是谁我在哪我在干嘛".getBytes(),0,9);
|
||||||
|
// fos.write("我是谁\r\n我在哪\r\n我在干嘛".getBytes());
|
||||||
|
fos.write(("我是谁"+System.lineSeparator()+"我在哪我在干嘛").getBytes());
|
||||||
|
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
48
javaSE-day09/src/com/inmind/io01/OutputSteamDemo02.java
Normal file
48
javaSE-day09/src/com/inmind/io01/OutputSteamDemo02.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package com.inmind.io01;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
在java中有一个抽象类表示字节输出流OutputStream
|
||||||
|
常用的子类FileOutputStream
|
||||||
|
构造方法:
|
||||||
|
FileOutputStream(File file) 创建文件输出流以写入由指定的 File对象表示的文件。
|
||||||
|
FileOutputStream(String name) 创建文件输出流以指定的名称写入文件。
|
||||||
|
常用的方法:
|
||||||
|
void write(int b) 将指定的字节写入此文件输出流。
|
||||||
|
void write(byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流。
|
||||||
|
void write(byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流。
|
||||||
|
void close() 释放任何系统资源。
|
||||||
|
|
||||||
|
字节输出流的作用:将内存中的字节数据写出到硬盘的文件中
|
||||||
|
|
||||||
|
字节输出流的使用步骤:
|
||||||
|
1.创建io流对象
|
||||||
|
2.调用写方法
|
||||||
|
3.资源释放
|
||||||
|
*/
|
||||||
|
public class OutputSteamDemo02 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
/*
|
||||||
|
1.创建字节输出流对象
|
||||||
|
a.如果指定文件不存在,自动创建
|
||||||
|
b.如果指定文件存在,它会默认覆盖
|
||||||
|
c.将fos对象与指定的硬盘中的文件进行了绑定,操作fos就是在操作文件
|
||||||
|
*/
|
||||||
|
FileOutputStream fos = new FileOutputStream("a.txt");
|
||||||
|
//2.调用写方法
|
||||||
|
fos.write(97);//字节的取值范围是-128~127 ‘a’,但是对应应用程序使用ASCII码表进行展示
|
||||||
|
|
||||||
|
byte[] bytes = {48, 49, 50};
|
||||||
|
fos.write(bytes);
|
||||||
|
|
||||||
|
fos.write(bytes,1,2);
|
||||||
|
|
||||||
|
//3.资源释放
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
47
javaSE-day09/src/com/inmind/io_exception05/Demo14.java
Normal file
47
javaSE-day09/src/com/inmind/io_exception05/Demo14.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package com.inmind.io_exception05;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
//jdk7之前的标准代码
|
||||||
|
public class Demo14 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
FileInputStream fis = null;
|
||||||
|
FileOutputStream fos = null;
|
||||||
|
try {
|
||||||
|
//创建流对象(输入,输出)
|
||||||
|
fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||||
|
fos = new FileOutputStream("D:\\io_test\\upload\\3.jpg");
|
||||||
|
|
||||||
|
//不停地读取一个字节数组,写出一个字节数组,直到读完
|
||||||
|
int len;//保存读取到的字节的个数
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
while ((len = fis.read(bytes)) != -1) {
|
||||||
|
fos.write(bytes,0,len);//读多少个字节,一次性写出多少个字节
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}finally {
|
||||||
|
//资源释放
|
||||||
|
if (fis != null) {
|
||||||
|
try {
|
||||||
|
fis.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
fis = null;//当前流已经关闭,如果不设置为null,fis可能会引用这堆内存中的一个无用liu对象,造成内存泄漏
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fos != null) {
|
||||||
|
try {
|
||||||
|
fos.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
fos = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
33
javaSE-day09/src/com/inmind/io_exception05/Demo15.java
Normal file
33
javaSE-day09/src/com/inmind/io_exception05/Demo15.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package com.inmind.io_exception05;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
JDK7异常的处理方式(了解)
|
||||||
|
格式:
|
||||||
|
try(流的创建方式){
|
||||||
|
|
||||||
|
}catch{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
作用:当try-cath代码执行完毕,程序会自动去调用try(流的创建方式)的close方法
|
||||||
|
*/
|
||||||
|
public class Demo15 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建字节输入流
|
||||||
|
try (FileInputStream fis = new FileInputStream("a.txt")) {
|
||||||
|
//不停地读字节
|
||||||
|
int c ;//保存读取的字节数据
|
||||||
|
while ((c = fis.read()) != -1) {
|
||||||
|
System.out.print((char) c);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
18
javaSE-day09/src/com/inmind/io_exception05/Demo16.java
Normal file
18
javaSE-day09/src/com/inmind/io_exception05/Demo16.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package com.inmind.io_exception05;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Demo16 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建字节输入流
|
||||||
|
try (Student s = new Student()) {
|
||||||
|
System.out.println("try中的代码执行了");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
27
javaSE-day09/src/com/inmind/io_exception05/Demo17.java
Normal file
27
javaSE-day09/src/com/inmind/io_exception05/Demo17.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package com.inmind.io_exception05;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
JDK9的IO异常处理改进(了解)
|
||||||
|
|
||||||
|
作用:当try-cath代码执行完毕,程序会自动去调用try(流的创建方式)的close方法
|
||||||
|
*/
|
||||||
|
public class Demo17 {
|
||||||
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
|
FileInputStream fis = new FileInputStream("a.txt");
|
||||||
|
//创建字节输入流
|
||||||
|
try (fis) {
|
||||||
|
//不停地读字节
|
||||||
|
int c ;//保存读取的字节数据
|
||||||
|
while ((c = fis.read()) != -1) {
|
||||||
|
System.out.print((char) c);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
javaSE-day09/src/com/inmind/io_exception05/Student.java
Normal file
8
javaSE-day09/src/com/inmind/io_exception05/Student.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package com.inmind.io_exception05;
|
||||||
|
|
||||||
|
public class Student implements AutoCloseable{
|
||||||
|
@Override
|
||||||
|
public void close() throws Exception {
|
||||||
|
System.out.println("Student类的close方法执行了");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.inmind.properties06;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Properties 的特点与基本使用(重点)
|
||||||
|
|
||||||
|
构造方法
|
||||||
|
public Properties() :创建一个空的属性列表。
|
||||||
|
|
||||||
|
常用的存储方法
|
||||||
|
public Object setProperty(String key, String value) : 保存一对属性。
|
||||||
|
public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。
|
||||||
|
public Set<String> stringPropertyNames() :所有键的名称的集合。
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.Properties类似一个HashMap
|
||||||
|
2.它的真正作用是,能够将指定的配置文件的信息读取到properties对象中
|
||||||
|
*/
|
||||||
|
public class PropertiesDemo18 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建属性集对象
|
||||||
|
Properties properties = new Properties();
|
||||||
|
//保存属性键值对
|
||||||
|
/*properties.put("username", "admin");
|
||||||
|
properties.put("password", "123");*/
|
||||||
|
properties.setProperty("username", "admin");
|
||||||
|
properties.setProperty("password", "123");
|
||||||
|
|
||||||
|
Set<String> keys = properties.stringPropertyNames();
|
||||||
|
System.out.println(keys);
|
||||||
|
|
||||||
|
System.out.println(properties);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.inmind.properties06;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Properties与流相关的功能(从文件中读取键值对)(重点)
|
||||||
|
public void load(InputStream inStream): 从字节输入流中读取键值对。
|
||||||
|
|
||||||
|
操作步骤:
|
||||||
|
1.创建一个.properties的配置文件(它里面都是键值对)
|
||||||
|
2.创建Properties对象
|
||||||
|
3.调用Properties对象的load方法
|
||||||
|
*/
|
||||||
|
public class PropertiesDemo19 {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
|
||||||
|
FileInputStream fis = new FileInputStream("demo.properties");
|
||||||
|
properties.load(fis);
|
||||||
|
|
||||||
|
System.out.println(properties);
|
||||||
|
}
|
||||||
|
}
|
||||||
49
javaSE-day09/src/com/inmind/reader03/Demo10.java
Normal file
49
javaSE-day09/src/com/inmind/reader03/Demo10.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package com.inmind.reader03;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
字符流的读取一个字符
|
||||||
|
在java中使用抽象类Reader表示字符输入流
|
||||||
|
常用子类FileReader
|
||||||
|
构造方法:
|
||||||
|
FileReader(File file) 创建一个新的 FileReader ,给出 File读取。
|
||||||
|
FileReader(String fileName) 创建一个新的 FileReader ,给定要读取的文件的名称。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
abstract void close() 关闭资源
|
||||||
|
int read() 读一个字符,返回了读取到的字符数据
|
||||||
|
int read(char[] cbuf) 将字符读入数组。
|
||||||
|
|
||||||
|
使用步骤:
|
||||||
|
1.创建对象
|
||||||
|
2.调用读方法
|
||||||
|
3.释放资源
|
||||||
|
*/
|
||||||
|
public class Demo10 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
FileReader fr = new FileReader("a.txt");
|
||||||
|
|
||||||
|
//读一个字符
|
||||||
|
/*int ch = fr.read();
|
||||||
|
System.out.println((char) ch);
|
||||||
|
|
||||||
|
ch = fr.read();
|
||||||
|
System.out.println((char) ch);//中
|
||||||
|
|
||||||
|
ch = fr.read();
|
||||||
|
System.out.println((char) ch);//国
|
||||||
|
|
||||||
|
ch = fr.read();
|
||||||
|
System.out.println( ch);//-1*/
|
||||||
|
|
||||||
|
int ch;//用来接收读取到的字符
|
||||||
|
while ((ch = fr.read()) != -1) {
|
||||||
|
System.out.print((char) ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
fr.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
39
javaSE-day09/src/com/inmind/reader03/Demo11.java
Normal file
39
javaSE-day09/src/com/inmind/reader03/Demo11.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package com.inmind.reader03;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
字符流一次读取一个字符数组
|
||||||
|
|
||||||
|
字符流的读取一个字符
|
||||||
|
在java中使用抽象类Reader表示字符输入流
|
||||||
|
常用子类FileReader
|
||||||
|
构造方法:
|
||||||
|
FileReader(File file) 创建一个新的 FileReader ,给出 File读取。
|
||||||
|
FileReader(String fileName) 创建一个新的 FileReader ,给定要读取的文件的名称。
|
||||||
|
常用方法:
|
||||||
|
abstract void close() 关闭资源
|
||||||
|
int read(char[] cbuf) 将字符读入数组。
|
||||||
|
*/
|
||||||
|
public class Demo11 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//创建一个字符输入流
|
||||||
|
FileReader fr = new FileReader("a.txt");
|
||||||
|
|
||||||
|
//创建一个字符数组,用来保存读取到的多个字符
|
||||||
|
char[] chars = new char[1024];
|
||||||
|
//用来获取读取的字符的个数
|
||||||
|
int len;
|
||||||
|
/*len = fr.read(chars);
|
||||||
|
System.out.println(len);//6
|
||||||
|
System.out.println(new String(chars,0,len));//*/
|
||||||
|
|
||||||
|
while ((len = fr.read(chars)) != -1) {
|
||||||
|
System.out.println(new String(chars,0,len));//读多少个字符,就获取多少个
|
||||||
|
}
|
||||||
|
|
||||||
|
fr.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
45
javaSE-day09/src/com/inmind/writer04/Demo12.java
Normal file
45
javaSE-day09/src/com/inmind/writer04/Demo12.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.writer04;
|
||||||
|
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
字符输出流写数据
|
||||||
|
在java中使用抽象类Writer表示字符输出流
|
||||||
|
常用子类 FileWriter
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
FileWriter(File file) 给一个File对象构造一个FileWriter对象。
|
||||||
|
FileWriter(String fileName) 构造一个给定文件名的FileWriter对象。
|
||||||
|
|
||||||
|
FileWriter(File file, boolean append) 给一个File对象构造一个FileWriter对象。
|
||||||
|
FileWriter(String fileName, boolean append) 构造一个FileWriter对象,给出一个带有布尔值的文件名,表示是否附加写入的数据。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
abstract void close() 关闭资源
|
||||||
|
void write(char[] cbuf) 写入一个字符数组。
|
||||||
|
abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分。
|
||||||
|
void write(int c) 写一个字符
|
||||||
|
void write(String str) 写一个字符串
|
||||||
|
void write(String str, int off, int len) 写一个字符串的一部分。
|
||||||
|
|
||||||
|
使用步骤:
|
||||||
|
1.创建对象
|
||||||
|
2.调用写方法
|
||||||
|
3.刷新
|
||||||
|
4.释放资源
|
||||||
|
*/
|
||||||
|
public class Demo12 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//向a.txt,覆盖输出内容
|
||||||
|
FileWriter fw = new FileWriter("a.txt",true);
|
||||||
|
|
||||||
|
fw.write('我');
|
||||||
|
char[] chars = {'在','哪'};
|
||||||
|
fw.write(chars);
|
||||||
|
|
||||||
|
fw.write("我想吃饭");
|
||||||
|
|
||||||
|
fw.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
26
javaSE-day09/src/com/inmind/writer04/Demo13.java
Normal file
26
javaSE-day09/src/com/inmind/writer04/Demo13.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package com.inmind.writer04;
|
||||||
|
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
刷新和关闭方法的区别
|
||||||
|
|
||||||
|
当我们想将缓冲区的数据输出到文件中,但是该字符输出流还想继续使用时,那就要使用flush方法
|
||||||
|
总结:
|
||||||
|
1.flush方法的作用将缓冲区的数据刷新到硬盘上,但是io流的资源没有释放的
|
||||||
|
2.close方法,释放资源之前会默认调用一次flush,但是调用close方法,该流就不能再使用否则报异常IOException: Stream closed
|
||||||
|
*/
|
||||||
|
public class Demo13 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//创建字符输出流
|
||||||
|
FileWriter fw = new FileWriter("a.txt");
|
||||||
|
fw.write("数据在哪里");
|
||||||
|
//主要刷新到文件中
|
||||||
|
fw.flush();
|
||||||
|
fw.write("数据在这");
|
||||||
|
|
||||||
|
fw.close();
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
55
javaSE-day10/src/com/inmind/buffered_stream01/Demo01.java
Normal file
55
javaSE-day10/src/com/inmind/buffered_stream01/Demo01.java
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package com.inmind.buffered_stream01;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
/*
|
||||||
|
4个流本身是没有读写功能,都是基于昨天的字节流和字符流进行操作,所以,今天只要先区分到底是什么流,然后就调用对应流的api
|
||||||
|
|
||||||
|
缓冲流:基于字节字符流,它本身是没有读写功能,底层封装了一个8192长度的缓冲区(数组),提高读写效率,也叫高效流(空间换时间)
|
||||||
|
|
||||||
|
字节缓冲流:BufferedInputStream BufferedOutputStream
|
||||||
|
字符缓冲流:BufferedReader BufferedWriter
|
||||||
|
------------------------------------------------------------------------------------------------
|
||||||
|
字节缓冲流的基本使用以及使用缓冲流复制文件(一次读写一个字节)
|
||||||
|
字节缓冲输入流:BufferedInputStream
|
||||||
|
构造方法:
|
||||||
|
BufferedInputStream(InputStream in) 创建一个 BufferedInputStream并保存其参数,输入流 in供以后使用。
|
||||||
|
常用方法:
|
||||||
|
int read() :读取一个字节数据
|
||||||
|
int read(byte[] bytes) : 读取一个字节数组
|
||||||
|
|
||||||
|
|
||||||
|
字节缓冲输出流:BufferedOutputStream
|
||||||
|
构造方法:
|
||||||
|
BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
|
||||||
|
常用方法:
|
||||||
|
void write(int b) 将指定的字节写入缓冲的输出流。
|
||||||
|
public void write(byte[] b)
|
||||||
|
void write(byte[] b, int off, int len) 从偏移量 off开始的指定字节数组写入 len字节到缓冲输出流。
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
//创建流对象
|
||||||
|
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.txt"));
|
||||||
|
//调用写方法
|
||||||
|
bos.write(97);
|
||||||
|
bos.write("我是谁我在哪".getBytes());
|
||||||
|
bos.write("我在干嘛,哈哈".getBytes(),0,12);
|
||||||
|
|
||||||
|
//释放资源
|
||||||
|
bos.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void testBufferedInputStream(String[] args) throws Exception {
|
||||||
|
//IO流的操作
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
|
||||||
|
|
||||||
|
//读取a.txt中的字节数据
|
||||||
|
int b ;//接收读取到的字节数据
|
||||||
|
while ((b = bis.read()) != -1) {
|
||||||
|
System.out.println((char) b);
|
||||||
|
}
|
||||||
|
bis.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
45
javaSE-day10/src/com/inmind/buffered_stream01/Demo02.java
Normal file
45
javaSE-day10/src/com/inmind/buffered_stream01/Demo02.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.buffered_stream01;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
public class Demo02 {
|
||||||
|
//使用字节缓冲输入输出流复制一个文件,并计算出时间(一次读写一个字节)
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
/*FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");*/
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\io_test\\upload\\1.jpg"));
|
||||||
|
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\io_test\\upload\\3.jpg"));
|
||||||
|
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
int c;//用来保存读取的字节数据
|
||||||
|
while ((c = bis.read()) != -1) {
|
||||||
|
bos.write(c);
|
||||||
|
}
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
|
||||||
|
System.out.println("使用缓冲流复制文件花费:"+(end- start));//38
|
||||||
|
|
||||||
|
bis.close();
|
||||||
|
bos.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//使用原始的字节输入输出流复制一个文件,并计算出时间(一次读写一个字节)
|
||||||
|
public static void method1(String[] args) throws IOException {
|
||||||
|
FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
int c;//用来保存读取的字节数据
|
||||||
|
while ((c = fis.read()) != -1) {
|
||||||
|
fos.write(c);
|
||||||
|
}
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
|
||||||
|
System.out.println("使用字节流复制文件花费:"+(end- start));//5880
|
||||||
|
|
||||||
|
fis.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
52
javaSE-day10/src/com/inmind/buffered_stream01/Demo03.java
Normal file
52
javaSE-day10/src/com/inmind/buffered_stream01/Demo03.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package com.inmind.buffered_stream01;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class Demo03 {
|
||||||
|
|
||||||
|
//使用字节缓冲输入输出流复制一个文件,并计算出时间(一次读写一个字节数组)
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
/*FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");*/
|
||||||
|
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\io_test\\upload\\1.jpg"));
|
||||||
|
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\io_test\\upload\\3.jpg"));
|
||||||
|
|
||||||
|
byte[] bytes = new byte[1024];//读取到的字节数据保存到该数组中
|
||||||
|
int len ;//获取读取到的字节数据的长度
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
|
while ((len = bis.read(bytes)) != -1) {
|
||||||
|
bos.write(bytes,0,len);
|
||||||
|
}
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
|
||||||
|
System.out.println("使用字节流复制文件花费:"+(end- start));//3ms
|
||||||
|
|
||||||
|
bis.close();
|
||||||
|
bos.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//使用原始的字节输入输出流复制一个文件,并计算出时间(一次读写一个字节数组)
|
||||||
|
public static void method(String[] args) throws IOException {
|
||||||
|
FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");
|
||||||
|
|
||||||
|
byte[] bytes = new byte[1024];//读取到的字节数据保存到该数组中
|
||||||
|
int len ;//获取读取到的字节数据的长度
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
|
while ((len = fis.read(bytes)) != -1) {
|
||||||
|
fos.write(bytes,0,len);
|
||||||
|
}
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
|
||||||
|
System.out.println("使用字节流复制文件花费:"+(end- start));//8ms
|
||||||
|
|
||||||
|
fis.close();
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
65
javaSE-day10/src/com/inmind/buffered_stream01/Demo04.java
Normal file
65
javaSE-day10/src/com/inmind/buffered_stream01/Demo04.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package com.inmind.buffered_stream01;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
/*
|
||||||
|
字符缓冲流的基本使用
|
||||||
|
字符缓冲流: BufferedReader , BufferedWriter
|
||||||
|
字符缓冲输入流:BufferedReader
|
||||||
|
构造方法
|
||||||
|
BufferedReader(Reader in) 创建使用默认大小的输入缓冲区的缓冲字符输入流。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
int read() 读一个字符
|
||||||
|
int read(char[] cbuf) 将字符读入数组。
|
||||||
|
-------------------------------------------------------------------------------------
|
||||||
|
字符缓冲输出流:BufferedWriter
|
||||||
|
构造方法:
|
||||||
|
BufferedWriter(Writer out) 创建使用默认大小的输出缓冲区的缓冲字符输出流。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
void write(int c) 写一个字符
|
||||||
|
void write(char[] cbuf) 写入字符数组的一部分。
|
||||||
|
void write(char[] cbuf, int off, int len) 写入字符数组的一部分。
|
||||||
|
void write(String s) 写一个字符串的一部分。
|
||||||
|
void write(String s, int off, int len) 写一个字符串的一部分。
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.字符缓冲流的基本使用与昨天的字符流一模一样
|
||||||
|
2.字符缓冲流就是底层封装了一个8192的字符数组,提高字符流的读写效率
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo04 {
|
||||||
|
//字符缓冲输出流的基本使用
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
// BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt",true));
|
||||||
|
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
|
||||||
|
|
||||||
|
bw.write('我');
|
||||||
|
char[] chars = {'是','谁'};
|
||||||
|
bw.write(chars);
|
||||||
|
bw.write("我在哪我在干嘛");
|
||||||
|
|
||||||
|
bw.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//字符缓冲输入流的基本使用
|
||||||
|
public static void method(String[] args) throws IOException {
|
||||||
|
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
|
||||||
|
|
||||||
|
//一次读取一个字符
|
||||||
|
/*int ch;//用来接收读取到的字符
|
||||||
|
while ((ch = br.read()) != -1) {
|
||||||
|
System.out.print((char) ch);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
//一次读取一个字符数组
|
||||||
|
char[] chars = new char[1024];//用来保存字符流读取到的字符数据
|
||||||
|
int len;//用来保存读取到的字符个数
|
||||||
|
while ((len = br.read(chars)) != -1) {
|
||||||
|
System.out.print(new String(chars,0,len));
|
||||||
|
}
|
||||||
|
br.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
52
javaSE-day10/src/com/inmind/buffered_stream01/Demo05.java
Normal file
52
javaSE-day10/src/com/inmind/buffered_stream01/Demo05.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package com.inmind.buffered_stream01;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
/*
|
||||||
|
字符缓冲流中特有的功能
|
||||||
|
BufferedWriter: newLine 它能够进行多个系统之间的换行的兼容
|
||||||
|
|
||||||
|
BufferedReader: readLine 它能够将文件中的内容按一行文本来读取
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
//BufferedReader,一次读取一行内容
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
|
||||||
|
/*String line = br.readLine();
|
||||||
|
System.out.println(line);
|
||||||
|
|
||||||
|
line = br.readLine();
|
||||||
|
System.out.println(line);
|
||||||
|
|
||||||
|
line = br.readLine();
|
||||||
|
System.out.println(line);
|
||||||
|
|
||||||
|
line = br.readLine();
|
||||||
|
System.out.println(line);*/
|
||||||
|
String line = null;//接收字符缓冲输入流读取的一行内容
|
||||||
|
/*
|
||||||
|
循环条件:(line = br.readLine()) != null
|
||||||
|
1.br.readLine() 读取文件的一行内容
|
||||||
|
2.line = br.readLine() 将读取到的一行内容保存到line中
|
||||||
|
3.(line = br.readLine()) != null 判断是否读取到了文件的末尾
|
||||||
|
*/
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
br.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//BufferedWriter,换行输出内容
|
||||||
|
public static void method(String[] args) throws IOException {
|
||||||
|
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
|
||||||
|
bw.write("我是谁");
|
||||||
|
bw.newLine();
|
||||||
|
bw.write("我在哪");
|
||||||
|
bw.newLine();
|
||||||
|
bw.write("我在干嘛");
|
||||||
|
bw.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
52
javaSE-day10/src/com/inmind/buffered_stream01/Test06.java
Normal file
52
javaSE-day10/src/com/inmind/buffered_stream01/Test06.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package com.inmind.buffered_stream01;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/*
|
||||||
|
7.字符缓冲流的练习(文本排序)
|
||||||
|
|
||||||
|
|
||||||
|
3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必得裨补阙漏,有所广益。
|
||||||
|
8.愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。
|
||||||
|
4.将军向宠,性行淑均,晓畅军事,试用之于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。
|
||||||
|
2.宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。
|
||||||
|
1.先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。
|
||||||
|
9.今当远离,临表涕零,不知所言。
|
||||||
|
6.臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。
|
||||||
|
7.先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐付托不效,以伤先帝之明,故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。
|
||||||
|
5.亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也
|
||||||
|
|
||||||
|
实现分析:
|
||||||
|
1.创建一个Map对象,保存键值对,9---->今当远离,临表涕零,不知所言。
|
||||||
|
2.创建字符缓冲流,读取每行内容,保存到Map中
|
||||||
|
3.按照1~9序号,将map中的内容写出到新的文件中
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Test06 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建一个Map对象,保存键值对,9---->今当远离,临表涕零,不知所言。
|
||||||
|
HashMap<String, String> map = new HashMap<>();
|
||||||
|
//2.创建字符缓冲流,读取每行内容,保存到Map中
|
||||||
|
BufferedReader br = new BufferedReader(new FileReader("csb.txt"));
|
||||||
|
//3.不停地读取每行内容,保存到map中
|
||||||
|
String line = null;//用来保存每行内容
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
//数据处理
|
||||||
|
String[] strings = line.split("\\.");
|
||||||
|
map.put(strings[0], strings[1]);
|
||||||
|
}
|
||||||
|
System.out.println(map);
|
||||||
|
br.close();
|
||||||
|
//4.按照1~9序号,将map中的内容写出到新的文件中
|
||||||
|
BufferedWriter bw = new BufferedWriter(new FileWriter("csb_new.txt"));
|
||||||
|
for (int i = 1; i <= 9; i++) {
|
||||||
|
String s = map.get(i + "");
|
||||||
|
bw.write(s);
|
||||||
|
if (i != 9)
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
bw.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
29
javaSE-day10/src/com/inmind/object_stream03/Demo11.java
Normal file
29
javaSE-day10/src/com/inmind/object_stream03/Demo11.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package com.inmind.object_stream03;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
序列化流对象(ObjectOutputStream)
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
ObjectOutputStream(OutputStream out) 创建一个写入指定的OutputStream的ObjectOutputStream。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
void writeObject(Object obj) 将指定的对象写入ObjectOutputStream。
|
||||||
|
|
||||||
|
注意:要进行序列化,被序列化的对象类必须实现一个接口Serializable,否则会报NotSerializableException
|
||||||
|
*/
|
||||||
|
public class Demo11 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
Student s = new Student("张三", 20);
|
||||||
|
|
||||||
|
//序列化:将java对象保存到文件中
|
||||||
|
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student3.txt"));
|
||||||
|
oos.writeObject(s);
|
||||||
|
|
||||||
|
oos.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
30
javaSE-day10/src/com/inmind/object_stream03/Demo12.java
Normal file
30
javaSE-day10/src/com/inmind/object_stream03/Demo12.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.object_stream03;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/*
|
||||||
|
反序列化流读取对象(ObjectInputStream)
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
ObjectInputStream(InputStream in) 创建从指定的InputStream读取的ObjectInputStream。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
Object readObject() 从ObjectInputStream读取一个对象。
|
||||||
|
*/
|
||||||
|
public class Demo12 {
|
||||||
|
//将之前的student.txt读取到java进程中生成一个叫张三,18的Student对象
|
||||||
|
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student3.txt"));
|
||||||
|
Object o = ois.readObject();
|
||||||
|
if (o instanceof Student) {
|
||||||
|
Student s = ((Student) o);
|
||||||
|
System.out.println(s.name);
|
||||||
|
System.out.println(s.age);
|
||||||
|
}
|
||||||
|
ois.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
35
javaSE-day10/src/com/inmind/object_stream03/Student.java
Normal file
35
javaSE-day10/src/com/inmind/object_stream03/Student.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package com.inmind.object_stream03;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.io.Serializable;
|
||||||
|
/*
|
||||||
|
Serializable接口的作用:起到一个标识的作用,确认当前类的对象可以序列化
|
||||||
|
*/
|
||||||
|
public class Student implements Serializable {
|
||||||
|
|
||||||
|
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 7268053700279080345L;//字节码文件的版本号
|
||||||
|
String name;
|
||||||
|
//transient int age;//transient:瞬态,在序列化对象时,被它修饰的属性,不能被保存
|
||||||
|
int age;
|
||||||
|
|
||||||
|
static String classroom = "1903";//不会被序列化,静态内容跟着字节码文件,只跟类有关
|
||||||
|
|
||||||
|
public Student(String name, int age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void method(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Student{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", age=" + age +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
41
javaSE-day10/src/com/inmind/object_stream03/Test13.java
Normal file
41
javaSE-day10/src/com/inmind/object_stream03/Test13.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package com.inmind.object_stream03;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
1. 将存有多个自定义对象的集合序列化操作,保存到list.txt文件中。
|
||||||
|
2. 反序列化list.txt ,并遍历集合,打印对象信息。
|
||||||
|
*/
|
||||||
|
public class Test13 {
|
||||||
|
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||||
|
//创建集合保存学生对象
|
||||||
|
ArrayList<Student> students = new ArrayList<>();
|
||||||
|
students.add(new Student("张三", 18));
|
||||||
|
students.add(new Student("李四", 20));
|
||||||
|
students.add(new Student("王五", 19));
|
||||||
|
|
||||||
|
//序列化保存集合
|
||||||
|
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("list.txt"));
|
||||||
|
oos.writeObject(students);
|
||||||
|
oos.close();
|
||||||
|
|
||||||
|
//创建反序列化流对象
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("list.txt"));
|
||||||
|
Object o = ois.readObject();
|
||||||
|
ArrayList objs = null;
|
||||||
|
if (o instanceof ArrayList) {
|
||||||
|
objs = (ArrayList) o;
|
||||||
|
}
|
||||||
|
|
||||||
|
//遍历集合,打印对象信息。
|
||||||
|
for (Object obj : objs) {
|
||||||
|
if (obj instanceof Student) {
|
||||||
|
Student s = (Student) obj;
|
||||||
|
System.out.println(s.name);
|
||||||
|
System.out.println(s.age);
|
||||||
|
System.out.println(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
68
javaSE-day10/src/com/inmind/printstream_04/Demo14.java
Normal file
68
javaSE-day10/src/com/inmind/printstream_04/Demo14.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package com.inmind.printstream_04;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
/*
|
||||||
|
打印流的基本使用(PrintStream)(了解)
|
||||||
|
构造方法
|
||||||
|
PrintStream(String fileName) 使用指定的文件名创建新的打印流,无需自动换行
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
print():直接输出内容,但不换行
|
||||||
|
println():直接输出内容,但换行
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------
|
||||||
|
IO流总结:
|
||||||
|
IO流:字节流和字符流
|
||||||
|
字节输入输出流:
|
||||||
|
InputStream
|
||||||
|
OutputSteam
|
||||||
|
常用子类:
|
||||||
|
FileInputStream
|
||||||
|
FileOutputSteam
|
||||||
|
|
||||||
|
|
||||||
|
字符输入输出流:
|
||||||
|
Reader
|
||||||
|
Writer
|
||||||
|
常用子类:
|
||||||
|
FileReader
|
||||||
|
FileWriter
|
||||||
|
|
||||||
|
|
||||||
|
字节缓冲流:底层封装了个8192的数组提高读写效率
|
||||||
|
BufferedInputStream
|
||||||
|
BufferedOutputSteam
|
||||||
|
字符缓冲流:
|
||||||
|
BufferedReader
|
||||||
|
BufferedWriter
|
||||||
|
|
||||||
|
转换流:可以根据指定的编码方式进行读写数据(GBK UTF-8)
|
||||||
|
InputStreamReader
|
||||||
|
OutputSteamWriter
|
||||||
|
|
||||||
|
序列化流:
|
||||||
|
ObjectInputStream
|
||||||
|
ObjectOutputSteam
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo14 {
|
||||||
|
//修改掉sout的打印位置
|
||||||
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
|
PrintStream ps = new PrintStream("dest.txt");
|
||||||
|
System.setOut(ps);//修改了系统级别的打印位置
|
||||||
|
System.out.println("这是修改了系统级别的打印位置后的sout");
|
||||||
|
}
|
||||||
|
|
||||||
|
//打印流的基本使用
|
||||||
|
public static void method(String[] args) throws FileNotFoundException {
|
||||||
|
//往相对路径dest.txt中打印字符串
|
||||||
|
PrintStream ps = new PrintStream("dest.txt");
|
||||||
|
ps.println("你好java");
|
||||||
|
ps.print("helloworld");
|
||||||
|
System.out.println("你好java");
|
||||||
|
ps.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
29
javaSE-day10/src/com/inmind/transfer_stream02/Demo07.java
Normal file
29
javaSE-day10/src/com/inmind/transfer_stream02/Demo07.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package com.inmind.transfer_stream02;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/*
|
||||||
|
使用字符流,读取电脑中的文件数据到java内存,并打印内容
|
||||||
|
|
||||||
|
出现乱码的原因:中文数据在读写操作的前后的编码方式不一致(gbk utf-8).
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.UTF-8,字母和数字只占1个字节,而一个中文占3个字节,它的前128个字符完全兼容ASCII码表
|
||||||
|
2.GBK,字母和数字只占1个字节,而一个中文占2个字节,它的前128个字符完全兼容ASCII码表
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
// FileReader fr = new FileReader("D:\\io_test\\file_gbk.txt");
|
||||||
|
FileReader fr = new FileReader("D:\\io_test\\file.txt");
|
||||||
|
char[] chars = new char[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = fr.read(chars)) != -1) {
|
||||||
|
System.out.println(new String(chars,0,len));
|
||||||
|
}
|
||||||
|
|
||||||
|
fr.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
33
javaSE-day10/src/com/inmind/transfer_stream02/Demo08.java
Normal file
33
javaSE-day10/src/com/inmind/transfer_stream02/Demo08.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package com.inmind.transfer_stream02;
|
||||||
|
/*
|
||||||
|
编码表的介绍
|
||||||
|
编码:将字符转换成字节(类似加密)
|
||||||
|
解码:将字节转换成字符(类似解密)
|
||||||
|
|
||||||
|
编码表:将每个国家的文字和二进制对应起来
|
||||||
|
|
||||||
|
ASCII 使用1个字节表示字符(第一位 一定是0 正数) 0~127
|
||||||
|
ISO-8859-1 使用1个字节表示字符,包扩了ASCII的,表示拉丁,欧洲的语言,不包含中文
|
||||||
|
|
||||||
|
gb2312: 使用2个字节表示1个字符,7000个简体中文和符号
|
||||||
|
big5: 使用2个字节表示繁体字,片假名
|
||||||
|
|
||||||
|
GBK(国标码),使用2个字节表示1个字符,涵盖2万多个中文,繁体字完全兼容ASCII
|
||||||
|
|
||||||
|
unicode(万国码) u+0000到U+10FFFF的字符,包含110万字符,包含所有国家的文字
|
||||||
|
|
||||||
|
utf-8 :使用1,2,3,4个字节表示字符(3个字节表示1个中文)
|
||||||
|
|
||||||
|
utf-16 :使用2 ,4个字节表示字符
|
||||||
|
|
||||||
|
utf-32 :使用4个字节表示字符,比较占用内存
|
||||||
|
|
||||||
|
字符集:GBK(国标码) unicode(万国码)
|
||||||
|
编码方式:GBK编码 utf-8编码
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.GBK针对国内的字符进行编解码操作,它前128个字符完全兼容ascii,一个中文占2个字节
|
||||||
|
2.utf-8针对国际的字符进行编解码操作,它前128个字符完全兼容ascii,一个中文占3个字节
|
||||||
|
*/
|
||||||
|
public class Demo08 {
|
||||||
|
}
|
||||||
50
javaSE-day10/src/com/inmind/transfer_stream02/Demo09.java
Normal file
50
javaSE-day10/src/com/inmind/transfer_stream02/Demo09.java
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package com.inmind.transfer_stream02;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
/*
|
||||||
|
转换流_指定编码读取(InputStreamReader)
|
||||||
|
InputStreamReader是从字节流到字符流的桥梁:解码
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader。
|
||||||
|
InputStreamReader(InputStream in, String charsetName) 创建一个使用命名字符集的InputStreamReader。
|
||||||
|
常用方法:
|
||||||
|
close()
|
||||||
|
int read(); 读取一个字符
|
||||||
|
int read(char[] chars) 读取一个字符数组
|
||||||
|
|
||||||
|
注意:转换流的作用是指定编码方式去读写文件
|
||||||
|
*/
|
||||||
|
public class Demo09 {
|
||||||
|
//指定gbk编码方式去读取指定gbk文件
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//InputStreamReader(InputStream in, String charsetName) 创建一个使用命名字符集的InputStreamReader。
|
||||||
|
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\io_test\\file_gbk.txt"),"gbk");
|
||||||
|
char[] chars = new char[1024];
|
||||||
|
int len = -1;
|
||||||
|
while ((len = isr.read(chars)) != -1) {
|
||||||
|
System.out.println(new String(chars,0,len));
|
||||||
|
}
|
||||||
|
|
||||||
|
isr.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
//指定utf-8编码方式去读取指定文件
|
||||||
|
public static void readUtf8(String[] args) throws IOException {
|
||||||
|
//InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader。
|
||||||
|
// InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\io_test\\file_utf8.txt"));
|
||||||
|
//InputStreamReader(InputStream in, String charsetName) 创建一个使用命名字符集的InputStreamReader。
|
||||||
|
InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\io_test\\file_utf8.txt"),"utf-8");
|
||||||
|
char[] chars = new char[1024];
|
||||||
|
int len = -1;
|
||||||
|
while ((len = isr.read(chars)) != -1) {
|
||||||
|
System.out.println(new String(chars,0,len));
|
||||||
|
}
|
||||||
|
|
||||||
|
isr.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
40
javaSE-day10/src/com/inmind/transfer_stream02/Demo10.java
Normal file
40
javaSE-day10/src/com/inmind/transfer_stream02/Demo10.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package com.inmind.transfer_stream02;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
|
||||||
|
/*
|
||||||
|
转换流_按照指定编码写数据(OutputStreamWriter)
|
||||||
|
OutputStreamWriter是从字符流到字节流的桥梁: 编码
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
OutputStreamWriter(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
|
||||||
|
OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
close
|
||||||
|
flush
|
||||||
|
5个写方法
|
||||||
|
*/
|
||||||
|
public class Demo10 {
|
||||||
|
//使用转换输出流,指定编码方式GBK输出内容到文件中
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
|
||||||
|
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("file_gbk.txt"),"gbk");
|
||||||
|
osw.write("我是谁我在哪111");
|
||||||
|
osw.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//使用转换输出流,指定编码方式utf-8输出内容到文件中
|
||||||
|
public static void method(String[] args) throws IOException {
|
||||||
|
//OutputStreamWriter(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
|
||||||
|
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("file_utf8.txt"));
|
||||||
|
//OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
|
||||||
|
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("file_utf8.txt"),"utf-8");
|
||||||
|
osw.write("我是谁我在哪111");
|
||||||
|
osw.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.inmind.dead_circle_upload04;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/*
|
||||||
|
文件上传客户端的实现步骤:
|
||||||
|
1.创建字节输入流读取图片的字节数据
|
||||||
|
2.创建客户端对象指定服务器的IP和端口
|
||||||
|
3.获取客户端的字节输出流,将图片的字节数据边读边写,作为请求数据,发送给服务器
|
||||||
|
4.获取客户端的字节输入流,接收服务器的响应数据
|
||||||
|
5.资源释放
|
||||||
|
*/
|
||||||
|
public class TcpUploadClientDemo06 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建字节输入流读取图片的字节数据
|
||||||
|
FileInputStream fis = new FileInputStream("4.jpg");
|
||||||
|
//2.创建客户端对象指定服务器的IP和端口
|
||||||
|
Socket client = new Socket("192.168.22.51", 10002);
|
||||||
|
OutputStream os = client.getOutputStream();//发送请求数据
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
//3.获取客户端的字节输出流,将图片的字节数据边读边写,作为请求数据,发送给服务器
|
||||||
|
while ((len = fis.read(buf)) != -1) {
|
||||||
|
os.write(buf,0,len);
|
||||||
|
}
|
||||||
|
//通知服务端,请求数据发送结束了
|
||||||
|
fis.close();
|
||||||
|
System.out.println("客户端上传了图片");
|
||||||
|
client.shutdownOutput();
|
||||||
|
//4.获取客户端的字节输入流,接收服务器的响应数据
|
||||||
|
InputStream is = client.getInputStream();
|
||||||
|
while ((len = is.read(buf)) != -1) {
|
||||||
|
System.out.println(new String(buf, 0, len));
|
||||||
|
}
|
||||||
|
System.out.println("客户端接收到了响应的结果");
|
||||||
|
//5.资源释放
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.inmind.dead_circle_upload04;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/*
|
||||||
|
文件上传的服务端的实现步骤:
|
||||||
|
1.创建一个服务端对象,接收请求
|
||||||
|
2.获取客户端socket对象的字节输入流,接收请求数据(图片字节数据)
|
||||||
|
3.创建字节输出流,边读边写到硬盘中
|
||||||
|
4.获取客户端的socket对象字节输出流,发送响应数据(上传成功)
|
||||||
|
5.资源释放
|
||||||
|
*/
|
||||||
|
public class TcpUploadServerDemo06 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建一个服务端对象,接收请求
|
||||||
|
ServerSocket server = new ServerSocket(10002);
|
||||||
|
System.out.println("服务端启动了");
|
||||||
|
while (true) {
|
||||||
|
Socket clientSocket = server.accept();
|
||||||
|
System.out.println(clientSocket.getInetAddress().getHostAddress());
|
||||||
|
|
||||||
|
//2.获取客户端socket对象的字节输入流,接收请求数据(图片字节数据)
|
||||||
|
InputStream is = clientSocket.getInputStream();
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
//3.创建字节输出流,边读边写到硬盘中
|
||||||
|
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\"+System.currentTimeMillis()+".jpg");
|
||||||
|
while ((len = is.read(buf)) != -1) {
|
||||||
|
fos.write(buf,0,len);
|
||||||
|
}
|
||||||
|
fos.close();
|
||||||
|
System.out.println("服务器保存了上传的图片");
|
||||||
|
//4.获取客户端的socket对象字节输出流,发送响应数据(上传成功)
|
||||||
|
OutputStream os = clientSocket.getOutputStream();
|
||||||
|
os.write("上传成功".getBytes());
|
||||||
|
System.out.println("服务器响应了结果给客户端");
|
||||||
|
//5.资源释放
|
||||||
|
clientSocket.close();
|
||||||
|
// server.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.inmind.lambda_upload06;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/*
|
||||||
|
文件上传客户端的实现步骤:
|
||||||
|
1.创建字节输入流读取图片的字节数据
|
||||||
|
2.创建客户端对象指定服务器的IP和端口
|
||||||
|
3.获取客户端的字节输出流,将图片的字节数据边读边写,作为请求数据,发送给服务器
|
||||||
|
4.获取客户端的字节输入流,接收服务器的响应数据
|
||||||
|
5.资源释放
|
||||||
|
*/
|
||||||
|
public class TcpUploadClientDemo08 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建字节输入流读取图片的字节数据
|
||||||
|
FileInputStream fis = new FileInputStream("1.jpg");
|
||||||
|
//2.创建客户端对象指定服务器的IP和端口
|
||||||
|
Socket client = new Socket("192.168.22.51", 10002);
|
||||||
|
OutputStream os = client.getOutputStream();//发送请求数据
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
//3.获取客户端的字节输出流,将图片的字节数据边读边写,作为请求数据,发送给服务器
|
||||||
|
while ((len = fis.read(buf)) != -1) {
|
||||||
|
os.write(buf,0,len);
|
||||||
|
}
|
||||||
|
//通知服务端,请求数据发送结束了
|
||||||
|
fis.close();
|
||||||
|
System.out.println("客户端上传了图片");
|
||||||
|
client.shutdownOutput();
|
||||||
|
//4.获取客户端的字节输入流,接收服务器的响应数据
|
||||||
|
InputStream is = client.getInputStream();
|
||||||
|
while ((len = is.read(buf)) != -1) {
|
||||||
|
System.out.println(new String(buf, 0, len));
|
||||||
|
}
|
||||||
|
System.out.println("客户端接收到了响应的结果");
|
||||||
|
//5.资源释放
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package com.inmind.lambda_upload06;
|
||||||
|
|
||||||
|
import com.inmind.threads_upload05.UploadTask;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
/*
|
||||||
|
多线程上传服务器
|
||||||
|
多线程启动方式:
|
||||||
|
1.继承Thread
|
||||||
|
2.实现Runnable接口
|
||||||
|
3.线程池
|
||||||
|
*/
|
||||||
|
public class TcpUploadServerDemo08 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建一个服务端对象,接收请求
|
||||||
|
ServerSocket server = new ServerSocket(10002);
|
||||||
|
|
||||||
|
//创建一个线程池对象
|
||||||
|
ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||||
|
System.out.println("服务端启动了");
|
||||||
|
while (true) {
|
||||||
|
final Socket clientSocket = server.accept();
|
||||||
|
//创建线程,启动执行文件上传的任务
|
||||||
|
//new Thread(new UploadTask(clientSocket)).start();
|
||||||
|
//使用线程池,执行文件上传的任务
|
||||||
|
executorService.execute(()->{
|
||||||
|
try {
|
||||||
|
System.out.println(clientSocket.getInetAddress().getHostAddress());
|
||||||
|
System.out.println(Thread.currentThread().getName()+"在执行文件上传任务");
|
||||||
|
//2.获取客户端socket对象的字节输入流,接收请求数据(图片字节数据)
|
||||||
|
InputStream is = clientSocket.getInputStream();
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
//3.创建字节输出流,边读边写到硬盘中
|
||||||
|
//注意:在多线程中可能获取相同的毫秒值,导致不同文件的覆盖,这里推荐使用uuid
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\"+ UUID.randomUUID() +".jpg");
|
||||||
|
while ((len = is.read(buf)) != -1) {
|
||||||
|
fos.write(buf,0,len);
|
||||||
|
}
|
||||||
|
fos.close();
|
||||||
|
System.out.println("服务器保存了上传的图片");
|
||||||
|
//4.获取客户端的socket对象字节输出流,发送响应数据(上传成功)
|
||||||
|
OutputStream os = clientSocket.getOutputStream();
|
||||||
|
os.write("上传成功".getBytes());
|
||||||
|
System.out.println("服务器响应了结果给客户端");
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
if (clientSocket != null) {//JDK7之前的IO异常处理方案
|
||||||
|
//5.资源释放
|
||||||
|
try {
|
||||||
|
clientSocket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
61
javaSE-day11/src/com/inmind/network01/Demo01.java
Normal file
61
javaSE-day11/src/com/inmind/network01/Demo01.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package com.inmind.network01;
|
||||||
|
/*
|
||||||
|
软件结构:
|
||||||
|
C(client)/S(server):需要安装一个客户端,才能获取一些服务,比如桌面版腾讯会议,QQ,微信,爱奇艺,LOL(这不是我们今后的开发方向)
|
||||||
|
B(browser)/S(server):只需要一个浏览器,就可以获取一些服务,比如百度,淘宝,京东,爱奇艺网站(这是javaWEB方向)
|
||||||
|
----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
网络协议:通信协议是对计算机必须遵守的规则,只有遵守这些规则,计算机之间才能进行通信
|
||||||
|
|
||||||
|
网络协议有传输层协议:TCP UDP
|
||||||
|
TCP:
|
||||||
|
1.必须建立连接才能发送数据(三次握手)
|
||||||
|
2.传输安全
|
||||||
|
3.传输数据没有长度限制
|
||||||
|
|
||||||
|
应用场景:文件下载,文件上传
|
||||||
|
|
||||||
|
UDP:
|
||||||
|
1.不用建立连接就可以发送数据
|
||||||
|
2.传输不安全,效率高
|
||||||
|
3.传输数据有限制,分为多个数据包,每包限制64KB
|
||||||
|
|
||||||
|
应用场景:视频通话,语音通话,实况直播
|
||||||
|
|
||||||
|
--------------------------------------------------------------------
|
||||||
|
ip地址:在同一个网络中找到唯一的一台计算机
|
||||||
|
IP地址相关的命令:
|
||||||
|
ipconfig :查看本机ip
|
||||||
|
ping 192.168.1.1 :判断是否能与指定计算机通讯
|
||||||
|
-------------------------------------------------------------------
|
||||||
|
端口号:使用2个字节,0~65535的整数,用来标识计算机中的唯一的进程(正在运行的应用程序)
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.端口号0~1023,不能使用,已经系统进程占用
|
||||||
|
2.常见的端口号:mysql(3306) tomcat(8080) http(80)
|
||||||
|
|
||||||
|
端口号的作用:根据指定的端口,直接找到计算机中唯一的一个进程,端口是不能重复的
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
网络通信分2端:客户端和服务端
|
||||||
|
|
||||||
|
服务端:
|
||||||
|
a.从硬件上讲,它就是一台配置较高的计算机,提供一些服务
|
||||||
|
b.从软件上讲,它就是一台计算机安装了一个软件,就会在该计算机中生成一个server,该服务器给客户端提供功能
|
||||||
|
|
||||||
|
客户端:一台配置较低的计算机(手机,平板),它向服务器发送请求,获取服务
|
||||||
|
|
||||||
|
注意:一定是先启动服务端,客户端才能发送请求
|
||||||
|
|
||||||
|
在java中使用类表示客户端和服务端
|
||||||
|
UDP:DataGramSocket
|
||||||
|
|
||||||
|
TCP:
|
||||||
|
Socket 客户端
|
||||||
|
ServerSocket 服务端
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo01 {
|
||||||
|
}
|
||||||
50
javaSE-day11/src/com/inmind/tcp03/TcpClientDemo04.java
Normal file
50
javaSE-day11/src/com/inmind/tcp03/TcpClientDemo04.java
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package com.inmind.tcp03;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/*
|
||||||
|
TCP的客户端功能实现
|
||||||
|
在java中使用一个类Socket表示客户端
|
||||||
|
构造方法:
|
||||||
|
Socket(String host, int port) 创建流套接字并将其连接到指定主机上的指定端口号。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
void close() 关闭此套接字。
|
||||||
|
InetAddress getInetAddress() 返回套接字所连接的地址。
|
||||||
|
InputStream getInputStream() 返回此套接字的输入流.(接收响应数据的)
|
||||||
|
OutputStream getOutputStream() 返回此套接字的输出流。 (发送请求)
|
||||||
|
void shutdownOutput() 禁用此套接字的输出流 (设置了一个发送请求数据的结束标记)
|
||||||
|
|
||||||
|
客户端的实现步骤:
|
||||||
|
1.创建客户端对象
|
||||||
|
2.获取字节输出流,发送请求数据
|
||||||
|
3.获取字节输入流,接收响应数据
|
||||||
|
4.释放资源
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class TcpClientDemo04 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建客户端对象
|
||||||
|
//Socket(String host, int port) 创建流套接字并将其连接到指定主机上的指定端口号。
|
||||||
|
Socket client = new Socket("192.168.22.51", 10001);
|
||||||
|
//2.获取字节输出流,发送请求数据
|
||||||
|
OutputStream os = client.getOutputStream();
|
||||||
|
os.write("淘宝的官网".getBytes());
|
||||||
|
//注意:当发送完毕请求数据后,要设置结束的标记,否则服务器会长时间等待客户端的请求数据
|
||||||
|
client.shutdownOutput();
|
||||||
|
//3.获取字节输入流,接收响应数据
|
||||||
|
InputStream is = client.getInputStream();
|
||||||
|
//一次读字节数组的方式来读取响应数据
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = is.read(bytes)) != -1) {
|
||||||
|
//输出响应结果
|
||||||
|
System.out.println(new String(bytes,0,len));
|
||||||
|
}
|
||||||
|
//4.释放资源
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
57
javaSE-day11/src/com/inmind/tcp03/TcpServerDemo04.java
Normal file
57
javaSE-day11/src/com/inmind/tcp03/TcpServerDemo04.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package com.inmind.tcp03;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/*
|
||||||
|
在java中使用一个类表示服务端ServerSocket
|
||||||
|
构造方法:
|
||||||
|
ServerSocket(int port) 创建绑定到指定端口的服务器套接字。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
Socket accept() 侦听要连接到客户端请求并接受它。
|
||||||
|
void close() 关闭此套接字。
|
||||||
|
InetAddress getInetAddress() 返回套接字所连接的地址。
|
||||||
|
InputStream getInputStream() 返回此套接字的输入流.(接收请求数据的)
|
||||||
|
OutputStream getOutputStream() 返回此套接字的输出流。 (发送响应数据)
|
||||||
|
void shutdownOutput() 禁用此套接字的输出流 (设置了一个发送请求数据的结束标记)
|
||||||
|
void close() 关闭此套接字。
|
||||||
|
|
||||||
|
服务器实现步骤:
|
||||||
|
1.创建服务器对象
|
||||||
|
2.接收客户端请求
|
||||||
|
3.获取请求数据
|
||||||
|
4.业务逻辑代码
|
||||||
|
5.发送响应数据
|
||||||
|
6.释放资源
|
||||||
|
*/
|
||||||
|
public class TcpServerDemo04 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建服务器对象
|
||||||
|
//ServerSocket(int port) 创建绑定到指定端口的服务器套接字。
|
||||||
|
ServerSocket server = new ServerSocket(10001);
|
||||||
|
System.out.println("服务启动了");
|
||||||
|
//2.接收客户端请求
|
||||||
|
//Socket accept() 侦听要连接到客户端请求并接受它。
|
||||||
|
Socket clientSocket = server.accept();//阻塞方法
|
||||||
|
//3.获取请求数据
|
||||||
|
InputStream is = clientSocket.getInputStream();
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = is.read(buf)) != -1) {
|
||||||
|
//打印获取的请求数据
|
||||||
|
System.out.println(new String(buf,0,len));
|
||||||
|
}
|
||||||
|
//4.业务逻辑代码
|
||||||
|
String result = "www.taobao.com";
|
||||||
|
//5.发送响应数据
|
||||||
|
OutputStream os = clientSocket.getOutputStream();
|
||||||
|
os.write(result.getBytes());
|
||||||
|
//6.释放资源
|
||||||
|
clientSocket.close();
|
||||||
|
server.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
44
javaSE-day11/src/com/inmind/tcp03/TcpUploadClientDemo05.java
Normal file
44
javaSE-day11/src/com/inmind/tcp03/TcpUploadClientDemo05.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package com.inmind.tcp03;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/*
|
||||||
|
文件上传客户端的实现步骤:
|
||||||
|
1.创建字节输入流读取图片的字节数据
|
||||||
|
2.创建客户端对象指定服务器的IP和端口
|
||||||
|
3.获取客户端的字节输出流,将图片的字节数据边读边写,作为请求数据,发送给服务器
|
||||||
|
4.获取客户端的字节输入流,接收服务器的响应数据
|
||||||
|
5.资源释放
|
||||||
|
*/
|
||||||
|
public class TcpUploadClientDemo05 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建字节输入流读取图片的字节数据
|
||||||
|
FileInputStream fis = new FileInputStream("1.jpg");
|
||||||
|
//2.创建客户端对象指定服务器的IP和端口
|
||||||
|
Socket client = new Socket("192.168.22.51", 10002);
|
||||||
|
OutputStream os = client.getOutputStream();//发送请求数据
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
//3.获取客户端的字节输出流,将图片的字节数据边读边写,作为请求数据,发送给服务器
|
||||||
|
while ((len = fis.read(buf)) != -1) {
|
||||||
|
os.write(buf,0,len);
|
||||||
|
}
|
||||||
|
//通知服务端,请求数据发送结束了
|
||||||
|
fis.close();
|
||||||
|
System.out.println("客户端上传了图片");
|
||||||
|
client.shutdownOutput();
|
||||||
|
//4.获取客户端的字节输入流,接收服务器的响应数据
|
||||||
|
InputStream is = client.getInputStream();
|
||||||
|
while ((len = is.read(buf)) != -1) {
|
||||||
|
System.out.println(new String(buf, 0, len));
|
||||||
|
}
|
||||||
|
System.out.println("客户端接收到了响应的结果");
|
||||||
|
//5.资源释放
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
45
javaSE-day11/src/com/inmind/tcp03/TcpUploadServerDemo05.java
Normal file
45
javaSE-day11/src/com/inmind/tcp03/TcpUploadServerDemo05.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.tcp03;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/*
|
||||||
|
文件上传的服务端的实现步骤:
|
||||||
|
1.创建一个服务端对象,接收请求
|
||||||
|
2.获取客户端socket对象的字节输入流,接收请求数据(图片字节数据)
|
||||||
|
3.创建字节输出流,边读边写到硬盘中
|
||||||
|
4.获取客户端的socket对象字节输出流,发送响应数据(上传成功)
|
||||||
|
5.资源释放
|
||||||
|
*/
|
||||||
|
public class TcpUploadServerDemo05 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
//1.创建一个服务端对象,接收请求
|
||||||
|
ServerSocket server = new ServerSocket(10002);
|
||||||
|
System.out.println("服务端启动了");
|
||||||
|
Socket clientSocket = server.accept();
|
||||||
|
System.out.println(clientSocket.getInetAddress().getHostAddress());
|
||||||
|
|
||||||
|
//2.获取客户端socket对象的字节输入流,接收请求数据(图片字节数据)
|
||||||
|
InputStream is = clientSocket.getInputStream();
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
//3.创建字节输出流,边读边写到硬盘中
|
||||||
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\a.jpg");
|
||||||
|
while ((len = is.read(buf)) != -1) {
|
||||||
|
fos.write(buf,0,len);
|
||||||
|
}
|
||||||
|
fos.close();
|
||||||
|
System.out.println("服务器保存了上传的图片");
|
||||||
|
//4.获取客户端的socket对象字节输出流,发送响应数据(上传成功)
|
||||||
|
OutputStream os = clientSocket.getOutputStream();
|
||||||
|
os.write("上传成功".getBytes());
|
||||||
|
System.out.println("服务器响应了结果给客户端");
|
||||||
|
//5.资源释放
|
||||||
|
clientSocket.close();
|
||||||
|
server.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user