目录
一、io流
1.什么是io流
io流:存储和读取数据的解决方案
2.流的方向
i.输入流
ii.输出流
字节输出流基本方法
FileOutputStream fos=new FileOutputStream("a.txt");
fos.write(97);
fos.close();
3.操作文件的类型
i.字节流
1.拷贝
ii.字符流
空参read
带参read
3.字符流输出流出数据
![](https://i-blog.csdnimg.cn/direct/f1485e45968541e080f2b09a892af2b1.png)
![](https://i-blog.csdnimg.cn/direct/4bec1da3edd748369204520da4d4f783.png)
![](https://i-blog.csdnimg.cn/direct/9d1fc1b43f2148318a84e8ca781229a1.png)
4.字节流和字符流的使用场景
5.练习
一:
public class Main {
public static void main(String[] args) throws IOException {
//拷贝一个文件夹,考虑子文件夹
//1.创建对象表示数据源
File src=new File("D:\\aaa\\src");
//2.创建对象表示目的地
File dest=new File("D:\\aaa\\dest");
//3.调用方法开始拷贝
copydir(src,dest);
}
/*
作用:拷贝文件
参数一:数据源
参数二:目的地
*/
private static void copydir(File src, File dest) throws IOException {
dest.mkdirs();//如果不存在则创建,如果存在则创建失败
//递归
//1.进入数据源
File[] files = src.listFiles();//会把所有的文件还
// 有文件夹的路径放到数组当中给你返回好
//2.遍历数组
for (File file : files) {
if (file.isFile()){
//3.判断文件,拷贝
FileInputStream fis=new FileInputStream(file);
FileOutputStream fos=new FileOutputStream(new File(dest,file.getName()));
byte[] bytes=new byte[1024];
int len;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}else{
//判断文件夹,递归
copydir(file,new File(dest,file.getName()));
}
}
}
}
二:
^异或:两边相同true两边不同false,两次同一个是原来的数据
加密:如以下代码
解密:再创一个c,将a改成b,b改成c就行了
FileInputStream fis=new FileInputStream("D:\\java\\io流\\myio\\a.txt");
FileOutputStream fos=new FileOutputStream("D:\\java\\io流\\myio\\b.txt");
//加密处理
int b;
while((b=fis.read())!=-1){
fos.write(b^2);
}
fos.close();
fis.close();
三:
注意:初始文件里面不要换行否则会隐含\r\n
代码:
//读取数据
FileReader fr=new FileReader("D:\\java\\io流\\myio\\a.txt");
StringBuilder sb=new StringBuilder();
int ch;
while((ch= fr.read())!=-1){
sb.append((char)ch);
}
fr.close();
System.out.println(sb);
//排序
Integer[] arr = Arrays.stream(sb.toString()
.split("-"))
.map(Integer::parseInt)//方法引用
.sorted()
.toArray(Integer[]::new);//再收集到Integer的数组里面
//写出数据
FileWriter fw=new FileWriter("D:\\java\\io流\\myio\\a.txt",true);
String s = Arrays.toString(arr)
.replace(", ","-") ;
String result= s.substring(1,s.length()-1);
fw.write("\r\n");
fw.write(result);
fw.close();
6.缓冲流
1.字节缓冲流拷贝文件
更加高级的知识点
![](https://i-blog.csdnimg.cn/direct/e24f602882524f978dd988727839e1d0.png)
![](https://i-blog.csdnimg.cn/direct/fa8c3cbb464843099667de817ed225d5.png)
2.字符缓冲流特有的方法
1.方法
![](https://i-blog.csdnimg.cn/direct/4b98468e30a747b6be3bf692da544140.png)
![](https://i-blog.csdnimg.cn/direct/db81f45e139f4d3d859706be356747a3.png)
2.总结
7.转换流基本用法
8.小练习;
二、多线程
1.实现方式
i.继承Thread类的方法进行实现
打印出来的数据是一会1一会2的
ii.实现Runnable接口的方式进行实现
iii.利用Callable接口和Future接口方式实现
vi.三种方式对比
v.常见的成员方法
2.守护线程等
i.守护线程
ii.礼让线程
让其尽可能均匀
iii.插入线程
vi.线程的生命周期![](https://i-blog.csdnimg.cn/direct/712ea1c072b14724974adb6564ce0308.png)
v.线程的安全
eg:买票:如下代码,结果就是很多票数重复了
小细节:
也可以这样写
注意:不同的线程互斥地访问临界资源,这是互斥。所有的线程都必须等待其他线程卖了票之后才能卖票,这是同步
//重写方法快捷键:Alt+回车
iv.同步方法
ctrl+alt+m快捷创建函数键
3、线程的锁
i.lock锁
ii.死锁
是一种故障:避免:别把两个锁嵌套起来
iii.多线程唤醒机制思路
![](https://i-blog.csdnimg.cn/direct/6d04ccd37275474a9310551e6381e35e.png)
4、多线程练习
i.抢红包
public class ThreadDemo {
public static void main(String[] args){
//创建线程的对象
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
MyThread t4=new MyThread();
MyThread t5=new MyThread();
//。给线程设置名字
t1.setName("小一");
t2.setName("小二");
t3.setName("小三");
t4.setName("小四");
t5.setName("小五");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
public class MyThread extends Thread{
//共享数据
static double money=100;
static int count=3;
//最小的中奖金额,final修饰变常量
static final double MIN=0.1;
@Override
public void run() {
//循环(由于抢红包一个人只能抢一次,就没必要循环了)
//同步代码块
//判断,共享数据是否到了末尾(已经到和没有到)
synchronized (MyThread.class){
if(count==0){
System.out.println(getName()+"没有抢到红包!");
}else {
double prize=0;
if(count==1){
prize=money;
}else{
Random r=new Random();
double bounds =money-(count-1)*MIN;
prize= r.nextDouble(bounds);
if(prize<MIN){
prize=MIN;
}
}
money=money-prize;
count--;
System.out.println((getName()+"抢到了"+prize+"元红包!"));
}
}
}
}
ii.抽奖
public class ThreadDemo {
public static void main(String[] args){
ArrayList<Integer> list=new ArrayList<>();
Collections.addAll(list,10,5,20,50,100,200,500,800,2,80,300,700);
MyThread t1=new MyThread(list);
MyThread t2=new MyThread(list);
t1.setName("抽奖箱1");
t2.setName("抽奖箱2");
t1.start();
t2.start();
}
}
public class MyThread extends Thread{
ArrayList<Integer> list;
public MyThread(ArrayList<Integer> list) {
this.list=list;
}
@Override
public void run() {
while(true){
synchronized (MyThread.class){
if(list.size()==0){
break;
}else {
Collections.shuffle(list);
int prize=list.remove(0);
System.out.println(getName()+"又产生了一个"+prize+"元大奖");
}
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
iii.统计并求最大值
public class ThreadDemo {
public static void main(String[] args){
ArrayList<Integer> list=new ArrayList<>();
Collections.addAll(list,10,5,20,50,100,200,500,800,2,80,300,700);
MyThread t1=new MyThread(list);
MyThread t2=new MyThread(list);
t1.setName("抽奖箱1");
t2.setName("抽奖箱2");
t1.start();
t2.start();
}
}
public class MyThread extends Thread {
ArrayList<Integer> list;
public MyThread(ArrayList<Integer> list) {
this.list = list;
}
@Override
public void run() {
ArrayList<Integer> boxlist = new ArrayList<>();
while (true) {
synchronized (MyThread.class) {
if (list.size() == 0) {
System.out.println(getName()+boxlist);
break;
} else {
Collections.shuffle(list);
int prize = list.remove(0);
boxlist.add(prize);
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}