目录
一、io流
1.什么是io流
io流:存储和读取数据的解决方案
2.流的方向
i.输入流



ii.输出流
字节输出流基本方法
java
FileOutputStream fos=new FileOutputStream("a.txt");
fos.write(97);
fos.close();
3.操作文件的类型
i.字节流
1.拷贝



ii.字符流



空参read

带参read
3.字符流输出流出数据



4.字节流和字符流的使用场景

5.练习
一:

java
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就行了
java
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
代码:
java
//读取数据
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.字节缓冲流拷贝文件
更加高级的知识点


2.字符缓冲流特有的方法
1.方法


2.总结
7.转换流基本用法
8.小练习;
二、多线程
1.实现方式

i.继承Thread类的方法进行实现

打印出来的数据是一会1一会2的
ii.实现Runnable接口的方式进行实现
iii.利用Callable接口和Future接口方式实现

vi.三种方式对比
v.常见的成员方法
2.守护线程等
i.守护线程
ii.礼让线程
让其尽可能均匀
iii.插入线程

vi.线程的生命周期
v.线程的安全
eg:买票:如下代码,结果就是很多票数重复了


小细节:
也可以这样写
注意:不同的线程互斥地访问临界资源,这是互斥。所有的线程都必须等待其他线程卖了票之后才能卖票,这是同步
//重写方法快捷键:Alt+回车
iv.同步方法
ctrl+alt+m快捷创建函数键