一、使用I/O操作文件

1、java的流
流:是指一连串流动的字符,是以先进先出的方式发送和接收数据的通道。
2、流分为输入流和输出流;这些输入/输出流类的对象称为对象流;
输入流:数据输入到内存——>关联的是源数据——>读取文件
输出流:内存中输出———>关联目标数据——->写入文件
image.png

3、创建目录与文件删除等方法

  1. public static void main(String[] args) {
  2. Lianxi lx=new Lianxi();
  3. lx.creatFile( "1.txt","D:/newFile");
  4. lx.write("1.txt", "D:/newFile", "abcdefg");
  5. lx.getSize("1.txt", "D:/newFile");
  6. lx.read("1.txt", "D:/newFile");
  7. lx.delete("3.txt", "D:/file2");
  8. lx.deletePath("D:/file3");
  9. }
  10. //删除目录
  11. private void deletePath(String path){
  12. File file=new File(path);
  13. if(file.exists()){
  14. file.delete();
  15. }
  16. }
  17. //删除文件
  18. private void delete(String fileName,String path){
  19. File file=new File(path+"/"+fileName);
  20. if(file.exists()){
  21. file.delete();
  22. }
  23. }
  24. //读取字节
  25. private void read(String fileName,String path){
  26. FileInputStream fos=null;
  27. try {
  28. fos=new FileInputStream(path+"/"+fileName);
  29. System.out.println("可读取的字节数是:"+fos.available()+"个");
  30. int a;
  31. while((a=fos.read())!=-1){
  32. System.out.println((char)a);
  33. }
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }finally{
  39. if(fos!=null){
  40. try {
  41. fos.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. }
  48. //获取修改时间
  49. private void getDate(String fileName,String path){
  50. File file=new File(path+"/"+fileName);
  51. long l=file.lastModified();
  52. Date date=new Date();
  53. date.setTime(l);
  54. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  55. System.out.println(sdf.format(date));
  56. }
  57. //获取文件大小
  58. private void getSize(String fileName,String path){
  59. File file=new File(path+"/"+fileName);
  60. System.out.println("文件大小:"+file.length());
  61. }
  62. //写入文件
  63. private void write(String fileName,String path,String word){
  64. FileOutputStream fos=null;
  65. try {
  66. fos=new FileOutputStream(path+"/"+fileName);
  67. fos.write(word.getBytes(), 0, word.length());
  68. } catch (FileNotFoundException e) {
  69. e.printStackTrace();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }finally{
  73. if(fos!=null){
  74. try {
  75. fos.close();
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. }
  81. }
  82. //获得路径
  83. private void getPath(String fileName,String path){
  84. File filePath=new File(path+"/"+fileName);
  85. System.out.println("相对"+filePath.getPath());
  86. System.out.println("绝对"+filePath.getAbsolutePath());
  87. }
  88. //创建文件夹和文件
  89. private void creatFile(String fileName,String path){
  90. File filePath=new File(path);
  91. if(!filePath.isDirectory()){
  92. if(!filePath.exists()){
  93. //创建一个
  94. filePath.mkdirs();
  95. }
  96. }
  97. File file=new File(path+"/"+fileName);
  98. if(!file.exists()){
  99. try {
  100. file.createNewFile();
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. }

二、字节流主方法读写文件

  1. /*
  2. * 读取文件
  3. */
  4. public static void main(String[] args) {
  5. FileInputStream fis=null;
  6. //输入流读取文件
  7. try {
  8. fis=new FileInputStream("D:/file2/1.txt");
  9. System.out.println("可读取的字节数:"+fis.available());
  10. //循环读取文件内容
  11. int data;//data=-1,读取完成
  12. while((data=fis.read())!=-1){
  13. System.out.println((char)data);
  14. }
  15. } catch (FileNotFoundException e) {
  16. e.printStackTrace();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }finally{
  20. //关闭流
  21. if(fis!=null){
  22. try {
  23. fis.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. }

5、主方法写入文件

  1. /*
  2. * 写入文件
  3. */
  4. public static void main(String[] args) {
  5. FileOutputStream fos=null;
  6. try {
  7. //true 表示追加内容,不写true就是直接覆盖
  8. fos=new FileOutputStream("D:/file2/1.txt",true);
  9. String s="def";
  10. fos.write(s.getBytes(),0,s.length());
  11. } catch (FileNotFoundException e) {
  12. e.printStackTrace();
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }finally{
  16. if(fos!=null){
  17. try {
  18. fos.close();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }
  24. }

5、file2/1.txt复制到file2/3.txt

将某文件的内容复制到新开辟的文件夹的文件内

  1. public static void main(String[] args) {
  2. FileInputStream fis=null;
  3. FileOutputStream fos=null;
  4. try {
  5. fis=new FileInputStream("D:/file2/1.txt");
  6. fos=new FileOutputStream("D:/file2/3.txt");
  7. //循环读取文件内容
  8. int data;//data=-1,读取完成
  9. while((data=fis.read())!=-1){
  10. fos.write(data);
  11. }
  12. } catch (FileNotFoundException e) {
  13. e.printStackTrace();
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }finally{
  17. if(fos!=null){
  18. try {
  19. fos.close();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. //关闭流
  25. if(fis!=null){
  26. try {
  27. fis.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33. }

三、字符流读写文件

QQ图片20210317150558.png
1、读文件

  1. public static void main(String[] args) {
  2. FileInputStream fis=null;
  3. InputStreamReader isr=null;
  4. BufferedReader br=null;//自带缓冲区去读文件
  5. try {
  6. fis=new FileInputStream("D:/file/a.txt");
  7. isr=new InputStreamReader(fis);//如果出乱码,在fis后面,“utf-8”或者“GBK”
  8. br=new BufferedReader(isr);
  9. //读取文件中一行数据
  10. String s=br.readLine();
  11. while(s!=null){
  12. System.out.println(s);
  13. s=br.readLine();
  14. }
  15. System.out.println("读取完毕");
  16. } catch (FileNotFoundException e) {
  17. e.printStackTrace();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }finally{
  21. try{
  22. if(br!=null){
  23. br.close();
  24. }
  25. if(isr!=null){
  26. isr.close();
  27. }
  28. if(fis!=null){
  29. fis.close();
  30. }
  31. }catch(IOException e){
  32. e.printStackTrace();
  33. }
  34. }
  35. }

2、写文件

  1. public static void main(String[] args) {
  2. FileOutputStream fos=null;
  3. OutputStreamWriter osw=null;
  4. BufferedWriter bw=null;
  5. try {
  6. fos=new FileOutputStream("D:/file/b.txt");
  7. osw=new OutputStreamWriter(fos);
  8. bw=new BufferedWriter(osw);
  9. bw.write("3.15");
  10. bw.newLine();//另起一行换行在写
  11. bw.write("名表维修-消磁天价收费");
  12. bw.newLine();
  13. bw.write("智联招聘企业账号可以随意下载");
  14. //刷新缓冲区
  15. bw.flush();
  16. System.out.println("写入完成");
  17. } catch (FileNotFoundException e) {
  18. e.printStackTrace();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }finally{
  22. try{
  23. if(bw!=null){
  24. bw.close();
  25. }
  26. if(osw!=null){
  27. osw.close();
  28. }
  29. if(fos!=null){
  30. fos.close();
  31. }
  32. }catch(IOException e){
  33. e.printStackTrace();
  34. }
  35. }
  36. }

1、复制文件的几种方法

  1. public static void main(String[] args) {
  2. CopyFile cf=new CopyFile();
  3. // String sourceFile="D:/file/b.txt";
  4. // String targetFile="D:/file/6.txt";
  5. String sourceFile="D:/myDoc/1.png";
  6. String targetFile="D:/file/1-copy.png";
  7. cf.copy6(sourceFile, targetFile);
  8. }
  9. //复制图片--->二进制流
  10. private void copy6(String sourceFile,String targetFile){
  11. FileInputStream fis=null;
  12. DataInputStream dis=null;//二进制流
  13. FileOutputStream fos=null;
  14. DataOutputStream dos=null;
  15. try {
  16. //输入流读文件
  17. fis=new FileInputStream(sourceFile);
  18. dis=new DataInputStream(fis);
  19. fos=new FileOutputStream(targetFile);
  20. dos=new DataOutputStream(fos);
  21. int data;
  22. //读
  23. while((data=dis.read())!=-1){
  24. //写
  25. dos.write(data);
  26. }
  27. System.out.println("图片复制成功");
  28. } catch (FileNotFoundException e) {
  29. e.printStackTrace();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }finally{
  33. try{
  34. if(dos!=null){
  35. dos.close();
  36. }
  37. if(fos!=null){
  38. fos.close();
  39. }
  40. if(dis!=null){
  41. dis.close();
  42. }
  43. if(fis!=null){
  44. fis.close();
  45. }
  46. }catch(IOException e){
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51. //含有中文的复制
  52. private void copy5(String sourceFile,String targetFile){
  53. FileInputStream fis=null;
  54. InputStreamReader isr=null;
  55. BufferedReader br=null;//自带缓冲区去读文件
  56. //
  57. FileOutputStream fos=null;
  58. OutputStreamWriter osw=null;
  59. BufferedWriter bw=null;
  60. try {
  61. fis=new FileInputStream(sourceFile);
  62. isr=new InputStreamReader(fis);//如果出乱码,在fis后面,“utf-8”或者“GBK”
  63. br=new BufferedReader(isr);
  64. //
  65. fos=new FileOutputStream(targetFile);
  66. osw=new OutputStreamWriter(fos);
  67. bw=new BufferedWriter(osw);
  68. //读取文件中一行数据
  69. String s=br.readLine();
  70. while(s!=null){
  71. //写入
  72. bw.write(s);
  73. bw.newLine();
  74. s=br.readLine();
  75. }
  76. bw.flush();
  77. System.out.println("复制成功");
  78. } catch (FileNotFoundException e) {
  79. e.printStackTrace();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }finally{
  83. try{
  84. if(bw!=null){
  85. bw.close();
  86. }
  87. if(osw!=null){
  88. osw.close();
  89. }
  90. if(fos!=null){
  91. fos.close();
  92. }
  93. if(br!=null){
  94. br.close();
  95. }
  96. if(isr!=null){
  97. isr.close();
  98. }
  99. if(fis!=null){
  100. fis.close();
  101. }
  102. }catch(IOException e){
  103. e.printStackTrace();
  104. }
  105. }
  106. }
  107. //文件大适合
  108. private void copy4(String sourceFile,String targetFile){
  109. FileInputStream fis=null;
  110. FileOutputStream fos=null;
  111. try {
  112. fis=new FileInputStream(sourceFile);
  113. fos=new FileOutputStream(targetFile);
  114. byte[] b=new byte[1024];
  115. int n=0;//去除占用空间
  116. while((n=fis.read(b,0,b.length))!=-1){
  117. fos.write(b,0,n);
  118. }
  119. System.out.println("操作成功");
  120. } catch (FileNotFoundException e) {
  121. e.printStackTrace();
  122. } catch (IOException e) {
  123. e.printStackTrace();
  124. }finally{
  125. try{
  126. if(fos!=null){
  127. fos.close();
  128. }
  129. if(fis!=null){
  130. fis.close();
  131. }
  132. }catch(IOException e){
  133. e.printStackTrace();
  134. }
  135. }
  136. }
  137. //文件居中适合
  138. private void copy3(String sourceFile,String targetFile){
  139. FileInputStream fis=null;
  140. FileOutputStream fos=null;
  141. StringBuffer s=new StringBuffer();
  142. //读文件,读取到String
  143. try {
  144. fis=new FileInputStream(sourceFile);
  145. int data;
  146. //fis.read()读文件
  147. while((data=fis.read())!=-1){
  148. s.append((char)data);
  149. }
  150. } catch (FileNotFoundException e) {
  151. e.printStackTrace();
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }finally{
  155. if(fis!=null){
  156. try {
  157. fis.close();
  158. } catch (IOException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. }
  163. //写文件
  164. try {
  165. fos=new FileOutputStream(targetFile);
  166. fos.write(s.toString().getBytes());
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. }finally{
  170. if(fos!=null){
  171. try {
  172. fos.close();
  173. } catch (IOException e) {
  174. e.printStackTrace();
  175. }
  176. }
  177. }
  178. }
  179. private void copy2(String sourceFile,String targetFile){
  180. FileInputStream fis=null;
  181. FileOutputStream fos=null;
  182. String s="";
  183. //读文件,读取到String
  184. try {
  185. fis=new FileInputStream(sourceFile);
  186. int data;
  187. //fis.read()读文件
  188. while((data=fis.read())!=-1){
  189. s+=(char)data;
  190. }
  191. } catch (FileNotFoundException e) {
  192. e.printStackTrace();
  193. } catch (IOException e) {
  194. e.printStackTrace();
  195. }finally{
  196. if(fis!=null){
  197. try {
  198. fis.close();
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. }
  202. }
  203. }
  204. //写文件
  205. try {
  206. fos=new FileOutputStream(targetFile);
  207. fos.write(s.getBytes());
  208. } catch (Exception e) {
  209. e.printStackTrace();
  210. }finally{
  211. if(fos!=null){
  212. try {
  213. fos.close();
  214. } catch (IOException e) {
  215. e.printStackTrace();
  216. }
  217. }
  218. }
  219. }
  220. //文件小适用
  221. private void copy1(String sourceFile,String targetFile){
  222. //读取文件
  223. FileInputStream fis=null;
  224. FileOutputStream fos=null;
  225. try {
  226. fis=new FileInputStream(sourceFile);
  227. fos=new FileOutputStream(targetFile);
  228. int data;
  229. //fis.read()读文件
  230. while((data=fis.read())!=-1){
  231. //fos.write(data)写入文件
  232. fos.write(data);
  233. }
  234. System.out.println("文件复制成功");
  235. } catch (FileNotFoundException e) {
  236. e.printStackTrace();
  237. } catch (IOException e) {
  238. e.printStackTrace();
  239. }finally{
  240. if(fos!=null){
  241. try {
  242. fos.close();
  243. } catch (IOException e) {
  244. e.printStackTrace();
  245. }
  246. }
  247. if(fis!=null){
  248. try {
  249. fis.close();
  250. } catch (IOException e) {
  251. e.printStackTrace();
  252. }
  253. }
  254. }
  255. }

四、利用二进制字符流复制图片

  1. //复制图片--->二进制流
  2. private void copy6(String sourceFile,String targetFile){
  3. FileInputStream fis=null;
  4. DataInputStream dis=null;//二进制流
  5. FileOutputStream fos=null;
  6. DataOutputStream dos=null;
  7. try {
  8. //输入流读文件
  9. fis=new FileInputStream(sourceFile);
  10. dis=new DataInputStream(fis);
  11. fos=new FileOutputStream(targetFile);
  12. dos=new DataOutputStream(fos);
  13. int data;
  14. //读
  15. while((data=dis.read())!=-1){
  16. //写
  17. dos.write(data);
  18. }
  19. System.out.println("图片复制成功");
  20. } catch (FileNotFoundException e) {
  21. e.printStackTrace();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }finally{
  25. try{
  26. if(dos!=null){
  27. dos.close();
  28. }
  29. if(fos!=null){
  30. fos.close();
  31. }
  32. if(dis!=null){
  33. dis.close();
  34. }
  35. if(fis!=null){
  36. fis.close();
  37. }
  38. }catch(IOException e){
  39. e.printStackTrace();
  40. }
  41. }
  42. }

五、序列化和反序列化

先写入再输出
FileOutputStream fos=null;
ObjectOutputStream oos=null;
FileInputStream fis=null;
ObjectInputStream ois=null;
1、首先类被实例化之前,要实现接口Serializable

  1. import java.io.Serializable;
  2. //
  3. /*要想Student被序列化,必须实现Serializable接口
  4. *
  5. */
  6. public class Student implements Serializable {
  7. /**
  8. * -8173462577973577860L---->ID号
  9. */
  10. private static final long serialVersionUID = -8173462577973577860L;
  11. private String name;
  12. private Integer age;
  13. //由transient 关键字修饰的属性不被序列化
  14. private transient String pwd;
  15. public Student(){
  16. }
  17. public Student(String name, Integer age, String pwd) {
  18. super();
  19. this.name = name;
  20. this.age = age;
  21. this.pwd = pwd;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public Integer getAge() {
  30. return age;
  31. }
  32. public void setAge(Integer age) {
  33. this.age = age;
  34. }
  35. public String getPwd() {
  36. return pwd;
  37. }
  38. public void setPwd(String pwd) {
  39. this.pwd = pwd;
  40. }
  41. }

2、序列化
程序(内存)———>(存储)文件
关键代码:实例化对象
//※序列化操作(自动加密)
oos.writeObject(list);
如果想要属性限制被序列化,用transient来修饰就可以

  1. public static void main(String[] args) {
  2. FileOutputStream fos=null;
  3. ObjectOutputStream oos=null;
  4. try {
  5. fos=new FileOutputStream("D:/file/stu.txt");
  6. oos=new ObjectOutputStream(fos);
  7. //实例化对象
  8. Student stu1=new Student("莉莉",18,"123123");
  9. Student stu2=new Student("明明",19,"123123");
  10. Student stu3=new Student("壮壮",20,"123123");
  11. List<Student> list=new ArrayList<Student>();
  12. list.add(stu1);
  13. list.add(stu2);
  14. list.add(stu3);
  15. //※序列化操作(自动加密)
  16. oos.writeObject(list);
  17. System.out.println("序列化完成");
  18. } catch (FileNotFoundException e) {
  19. e.printStackTrace();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }finally{
  23. try{
  24. if(oos!=null){
  25. oos.close();
  26. }
  27. if(fos!=null){
  28. fos.close();
  29. }
  30. }catch(IOException e){
  31. e.printStackTrace();
  32. }
  33. }
  34. }

3、反序列化
文件中的对象——>程序(内存)
//※反序列化操作
List list=(ArrayList)ois.readObject();

  1. public static void main(String[] args) {
  2. FileInputStream fis=null;
  3. ObjectInputStream ois=null;
  4. try {
  5. fis=new FileInputStream("D:/file/stu.txt");
  6. ois=new ObjectInputStream(fis);
  7. //※反序列化操作
  8. List<Student> list=(ArrayList<Student>)ois.readObject();
  9. // Student stu=(Student)ois.readObject();
  10. for (Student stu1 : list) {
  11. System.out.println("学生姓名:"+stu1.getName());
  12. System.out.println("年龄:"+stu1.getAge());
  13. System.out.println("密码:"+stu1.getPwd());
  14. }
  15. } catch (FileNotFoundException e) {
  16. e.printStackTrace();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. } catch (ClassNotFoundException e) {
  20. e.printStackTrace();
  21. }finally{
  22. try{
  23. if(ois!=null){
  24. ois.close();
  25. }
  26. if(fis!=null){
  27. fis.close();
  28. }
  29. }catch(IOException e){
  30. e.printStackTrace();
  31. }
  32. }
  33. }

六、java反射机制

1、/
(记住这句话即可)java的反射 可以动态的创建类,并且动态的获得类的属性和方法
还可以动态的调用和执行相应的方法
/
2、stu. eclipse 动态获取该类的属性和方法
3、//获取反射类的方法
(1)、getClass()—->创建了对象的情况(了解即可)
(2)、类名.class——>不创建对象的情况
(3)、(用的更多).Class.forName(“全类名”);
4、//获取clazz对象的属性和方法(私有属性无法获取)
Field[] fields=clazz.getFields();//返回属性数组,私有属性反射获取不到
5、clazz=Class.forName(“cn.bdqn.reflect.Student”);括号里面是类所在的全部地址
6、

  1. /*
  2. * (记住这句话即可)java的反射 可以动态的创建类,并且动态的获得类的属性和方法
  3. * 还可以动态的调用和执行相应的方法
  4. */
  5. public static void main(String[] args) {
  6. //
  7. //Object o=new Object();
  8. //stu. eclipse 动态获取该类的属性和方法
  9. //获取反射类的方法
  10. //(了解即可)1.getClass()--->创建了对象的情况
  11. // Student stu=new Student();
  12. // Class clazz=stu.getClass();
  13. //2.类名.class---->不创建对象的情况
  14. // Class clazz=Student.class;
  15. //(用的更多)3.Class.forName("全类名");
  16. Class clazz=null;
  17. try {
  18. clazz=Class.forName("cn.bdqn.reflect.Student");
  19. } catch (ClassNotFoundException e) {
  20. e.printStackTrace();
  21. }
  22. System.out.println("---------获取属性---------");
  23. //获取clazz对象的属性和方法(私有属性无法获取)
  24. Field[] fields=clazz.getFields();//返回属性数组,私有属性反射获取不到
  25. for (Field field : fields) {
  26. System.out.println(field.getName());
  27. }
  28. System.out.println("----------获取方法--------------");
  29. //获取方法
  30. Method[] methods=clazz.getMethods();
  31. for (Method method : methods) {
  32. System.out.println(method.getName());
  33. }
  34. }

7、反射还可以动态的调用和执行相应的方法
例如:setName再getName
image.png

  1. /*
  2. * 反射还可以动态的调用和执行相应的方法
  3. */
  4. public static void main(String[] args) {
  5. // 1Student反射类创建好
  6. Class clazz=null;
  7. clazz=Student.class;
  8. //getMethod("方法名", 参数)
  9. try {
  10. //2newInstance 获取clazz的实例对象
  11. Student stu=(Student)clazz.newInstance();
  12. //3
  13. Method m=clazz.getMethod("setName", String.class);
  14. //4调用方法
  15. m.invoke(stu, "李雷");
  16. //setName需要传参,getName不需要传参,所以是null
  17. Method m1=clazz.getMethod("getName", null);
  18. //调用方法
  19. Object o=m1.invoke(stu, null);
  20. System.out.println(o);
  21. } catch (NoSuchMethodException e) {
  22. e.printStackTrace();
  23. } catch (SecurityException e) {
  24. e.printStackTrace();
  25. } catch (IllegalAccessException e) {
  26. e.printStackTrace();
  27. } catch (IllegalArgumentException e) {
  28. e.printStackTrace();
  29. } catch (InvocationTargetException e) {
  30. e.printStackTrace();
  31. } catch (InstantiationException e) {
  32. e.printStackTrace();
  33. }
  34. }

8、拓展
main方法内{
T2.a(“a”,”b”);
}
private static void a(String…s){
//拓展,…表示可以传入1个或者多个参数
System.out.println(s[0]);
System.out.println(s[1]);
}
9、线程安全性

  1. public static void main(String[] args) {
  2. //HashMap线程不安全
  3. Map map1=new HashMap();
  4. //Hashtable线程安全(全锁)
  5. Map map2=new Hashtable();
  6. //ConcurrentHashMap线程安全 (1.7分段锁,1.8自旋锁CAS+synchronized)
  7. Map map3=new ConcurrentHashMap();
  8. }