6.19实训笔记

6.19

一、座右铭

我的故事你说,我的文字我落,我值几两你定,我去何方我挑。

二、知识回顾

2.1 Java集合体系

  1. 单列集合Collection–Iterable迭代器、增强的for循环

    1. List集合:可以重复、而且加入有序,提供几个可以使用素引进行取值默值的操作

      1. Vector:数组
      2. ArrayList:数组
      3. LinkedList:双向链表–内部类Node
    2. Set集合:不可重复

      1. HashSet:无序

      2. LinkedHashSet:加入有序

      3. TreeSet:大小有序、需要借助比较器实现

        不可重复
        1、hashCode
        2、Equals方法

    3. Queue集合–接触不多

    4. Collection—Java给我们提供的封装了单列集合常用的工具方法的工具类–提供的方法针对List集合体系

      代码示例

      List<Integer> list = Arrays.asList(3,2,1,5,4,6);
      Collections.sort(list,new Comparator<Integer>() {
              
              
      	@Override
      	public int compare(Integer o1, Integer o2) {
              
              
      			return o1>o2?-1:(o1==o2?0:1);			}
      		});
      		System.out.println(list);//[6, 5, 4, 3, 2, 1]
      		
      	    int i = Collections.binarySearch(list, 7);
      	    System.out.println(i);//-7
      		
      		List<Object> list2 = Collections.emptyList();
      		
      		Collections.reverse(list);
      		System.out.println(list);//[6, 4, 5, 1, 2, 3]
      		
      		Collections.shuffle(list);
      		System.out.println(list);//[3, 1, 4, 6, 5, 2]
      
    5. 接口JDK1.8版本以后增加了两个新的内容:静态方法、默认方法

      		  List<Integer> list = List.of(1,2,3,4,5);
      		  Map<String,Integer> of = Map.of("zs", 1, "ls", 2);
      		  System.out.println(list);
      		  System.out.println(of);
      
  2. 双列集合Map

    1. 特点

      1. 每一行数据都是有两列组成的,其中第一列称为key,第二列称为value
      2. 其中在Map集合当中,key值不允许重复,value允许重复的。Map集合底层所有的key值通过Set集合来进行存储的,Value值通过Collection集合进行存储的
      3. 如果Map集合增加了重复性的key值,会把原有的key值对应的value数据替换掉
    2. Map接口的常用方法

在这里插入图片描述

      >   代码示例

      ```java
      		Map<String,Double> map = new HashMap<>();
      		System.out.println(map.size());//查看集合的元素个数   //0
      		System.out.println(map.isEmpty());//判断map集合是否为空  //true
      		//map集合添加元素--添加重复性元素 会覆盖原有的旧的value值
      		map.put("香酥鸡", 69.0);
      		map.put("糖醋丸子", 21.5);
      		map.put("北京烤鸭", 21.5);
      		map.put("鸡公煲", 60.0);
      		map.put("香酥鸡", 99.0);
      		map.put("红烧鱼", 128.0);
      		System.out.println(map);  //{北京烤鸭=21.5, 糖醋丸子=21.5, 香酥鸡=99.0, 鸡公煲=60.0, 红烧鱼=128.0}
      		
      		//判断map集合是否包含某个key值
      		System.out.println(map.containsKey("清蒸鲈鱼"));//false
      		System.out.println(map.containsKey("香酥鸡"));//true
      		//根据key值获取map集合对应的value值 如果key值不存在,那么会得到一个null值
      		double price = map.get("香酥鸡");
      		System.out.println(price);//99.0
      		Double price1 = map.get("香酥鸭");
      		System.out.println(price1);//null
      		//map集合为了预防获取不存在的key值导致的空指针问题,提供了一个获取数据的方法并且防止null值
      		Double default1 = map.getOrDefault("红烧鱼", 0.0);
      		System.out.println(default1);//128.0
      		//清空集合
      //		map.clear();
      //		System.out.println(map);
      		
      		//map集合元素替换--key对应的value
      		map.replace("红烧鱼", 158.0);
      		System.out.println(map);//{北京烤鸭=21.5, 糖醋丸子=21.5, 香酥鸡=99.0, 鸡公煲=60.0, 红烧鱼=158.0}
      		//根据key移除map集合中某个元素
      		Double remove = map.remove("红烧鱼");
      		System.out.println(map);//{北京烤鸭=21.5, 糖醋丸子=21.5, 香酥鸡=99.0, 鸡公煲=60.0}
      		
      		/**
      		 * Map集合的遍历:
      		 *     1、keySet():Set<K>  获取map集合中的所有key值,返回的是set集合
      		 *     2、entrySet():Set<Entry<K,V>>: 获取map集合中所有元素,变成一个set集合,只不过map集合每一条的key和value使用内部类Entry封装起来
      		 */
      //		Set<String> set = map.keySet();
      //		for (String key : set) {
      //			Double double1 = map.get(key);
      //			System.out.println(key+"="+double1);
      //		}
      		
      		Set<Entry<String,Double>> entrySet = map.entrySet();
      		for(Entry<String,Double> entry : entrySet) {
      			System.out.println(entry.getKey()+"="+entry.getValue());
      			//北京烤鸭=21.5
      			//糖醋丸子=21.5
      			//香酥鸡=99.0
      			//鸡公煲=60.0
      		}
      ```

      

 3.   Map接口的常用实现类

      1.   Map接口的常用实现类

      2.   LinkedHashMap:有序,加入有序

      3.   Hashtable:无序

           >   Properties:一般是用来读取一些配置文件数据的。

      4.   TreeMap:key的大小有序,key值必须都有比较器

           >   代码示例

           ```java
           Map<String,Double> map = new TreeMap<>();
           map.put("xiangsuji", 69.0);
           map.put("tangcuwanzi", 21.5);
           map.put("beijingkaoya", 21.5);
           map.put("jigongbao", 60.0);
           map.put("xiangsuji", 99.0);
           map.put("hongshaoyu", 128.0);
           System.out.println(map);//{beijingkaoya=21.5, hongshaoyu=128.0, jigongbao=60.0, tangcuwanzi=21.5, xiangsuji=99.0}
           ```

2.2 工具类Utils

工具类就是把项目或者是某些技术的共有的方法抽取到同一个类中,实现代码的解耗合和可重复利用性,工具类为了便于开发者使用,一般工具类中提供的方法都是纯静态方法、因此工具类一般不需要构建对象、工具类的构造器都是私有化的。

三、JavaIO流

Java Input/Output,一般情况指的是Java操作一些外部数据时,使用IO流的形式进行操作,外部数据主要包括文件、网络等等。

3.1 File类

JavaIO既可以操作文件外部数据,还可以操作网络端口这种外部数据,如果Java要操作文件数据,必须要借助一个类File文件对象类

File类是Java中对文件/文件夹的抽象表示,通过这个File类,我们可以将操作系统本地的一个文件/文件夹加载到Java程序当中,随后通过File对象可以对文件进行增删改查等操作

File类只能操作文件的外部内容、而文件当中有哪些数据,这个操作File类做不到

代码示例

		/**
		 * key-value均是Object类型
		 */
		Properties prop = new Properties();
		prop.put("zs", 1);
		System.out.println(prop);//{zs=1}
		/**
		 * prop除了map集合有的方法以外,还多了三个方法:
		 *   load()
		 *   setProperty(String key,String value)===put
		 *   getProperty(String key)===get
		 */
		prop.load(new FileInputStream("F:\\eclipse-workspace\\java-study-619\\project.properties"));
		System.out.println(prop);//{password=123456, zs=1, username=zs}
		String value = prop.getProperty("username");
		System.out.println(value);//zs
		prop.setProperty("username", "ls");
		System.out.println(prop);//{password=123456, zs=1, username=ls}

3.2 File类的使用

3.2.1 File文件/文件夹类的创建

  1. 根据全路径创建
  2. 根据父子路径创建
  3. 根据父子路径创建,只不过父路径也是File对象

3.2.2 File类的获取操作

方法名
getName
getParent
getPath
getAbsolutePath

3.2.3 File类判断操作–boolean

方法名
exists()
isFile()
isDirectory()
isHidden()
canRead()
canWrite()
canExecute()

3.2.4 File类对文件/文件夹的增删改

  1. 创建

    1. 创建文件createNewFile():要求父目录必须存在
    2. 创建文件夹mkdir()/mkdirs()
  2. 删除

    delete()–如果是目录,目录必须空目录

  3. 修改

    renameTo(File):boolean

代码示例

		/**
 		* File类的使用
 		*    静态属性
 		* @author 11018
 		*
 		* windows:  \
 		* mac/linux:  /
 		*/	
		//在eclipse中,因为创建的是Java项目,Java项目中所有的相对路径,指的都是项目名下的某个路径 而非Java源文件的同级路径
		File file = new File("lzc/a.txt");
		//获取文件名  路径的最后一个文件/文件夹的名字
		String fileName = file.getName();
		System.out.println(fileName);//a.txt
		//获取文件的父路径  取决于你再构建File对象时有没有传入父路径
		String parent = file.getParent();
		System.out.println(parent);//lzc
		//获取文件的路径 ---传入的路径
		String path = file.getPath();
		System.out.println(path);//lzc\a.txt
		//获取文件的绝对路径--传入路径没有关系的
		String absolutePath = file.getAbsolutePath();
	    System.out.println(absolutePath);//F:\eclipse-workspace\java-study-619\lzc\a.txt
		
		System.out.println(file.exists());//false
		System.out.println(file.isFile());//false
     	System.out.println(file.isDirectory());//false
		System.out.println(file.isHidden());//false
		System.out.println(file.canRead());//false
		System.out.println(file.canWrite());//false
		System.out.println(file.canExecute());//false
		
		
		boolean mkdir = file.mkdirs();
		System.out.println(mkdir);//true
		boolean createNewFile = file.createNewFile();
		System.out.println(createNewFile);//false
		
//		boolean delete = file.delete();
//		System.out.println(delete);
		//重命名要求两个路径必须在同一个路径下
		boolean result = file.renameTo(new File("c:\\dnn"));
		System.out.println(result);//false
		
		System.out.println(file.lastModified());//1687161837429
		//只能获取文件的大小,无法获取文件夹的大小
		System.out.println(file.length());//0

3.2.5 、File类的获取子文件夹以及子文件的方法

listFiles()

list():返回指定目录下的下一级的文件或者文件夹

3.3 Java中IO流多种纬度的纬度

3.3.1 按照流向–Java程序

  1. 输入流 Input/Reader

  2. 输出流 Output/Writer

3.3.2 按照流的大小分类

  1. 字节流:Stream:什么样的数据都可以处理

  2. 字符流:Reader/Writer:只能处理纯文本类型的数据

3.3.3 按照流的功能分类

  1. 节点流:直接对接到数据源上的流

    文件流

    数组流

    网络流

  2. 处理流:无法直接对接到数据源上,而是包装了节点流,在节点流基础之上提供了更加强大的功能

3.4 JavaIO流的四大基类

  1. Javaio流中所有的流都有四个顶尖父类,四个顶尖父类是四个抽象类,四个抽象类当中封装了和流有关的很多的公用方法。

  2. InputStream:java中所有字节输入流的顶尖父类

    方法名
    read:int
    available:int
    close()
  3. OutputStream:Java中所有字节输出流的顶尖父类

    方法名
    write:写出的字节
    close()
  4. Reader:Java中所有字符输入流的顶尖父类

    方法名
    read:int
    close()
  5. Writer:Java中所有字符输出流的顶尖父类

    方法名
    write
    close–底层调用了flush方法
    flush

Reader、Writer:

因为要根据编码集进行数据的读取,一次要读取一个字符,而一个字符对应了多个字节。
编码集只有纯文本才有

代码示例

		InputStream is = null;
		try {
    
    
			is = new FileInputStream("lzc/b.txt");
			//读取数据源的一个字节  read方法每一次读取完成,下一次再进行读取,基于上一次读取的结果向后读
			/*
			 * int read = is.read();
			 * System.out.println(read);
			 * //返回值 如果是read()方法,代表的是每一次读取完成的字节的值,read(byte[])的话返回值不做研究
			 * //不管什么情况下 read的返回值一旦为-1 那么代表数据源没数据了
			 * int read2 = is.read();
			 * System.out.println(read2);
			 */
			//字节流中可以利用的字节数有多少
			int available = is . available ( ) ;
			System.out.println(available);//12
			byte[] array = new byte[3];
			int read = is.read(array);
			String string = new String(array,"UTF-8");
			System.out.println(string);//中
			int read2 = is.read();
			System.out.println(read2);//229
			
		} catch (FileNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			if (is != null) {
    
    
				try {
    
    
					is.close();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
		/**
 		* 方法递归:就是类似于循环的概念
 		* 当我们在执行一个逻辑的时候,需要重复性的执行某段代码,但是我们不清楚代码需要调用多少次
 		* 我们清楚代码在什么时候调用结束,这种情况下我们就可以方法递归来完成。
 		* 递归返回中必须有两个核心要素点:----if else分支
 		* 1、递归的出口
 		* 2、递归的入口----自己调用自己的逻辑
 		* @author lenovo
 		*
 		*/	
		File file = new File ( "c:/users/lenovo/desktop/demo" ) ;
		String[] files = file.list () ;
		System . out . println (Arrays.toString(files)) ; //[a.txt]
		
		
		File[] files2=file.listFiles();
		for ( File f : files2 ) {
    
    
	
        System.out.println(f.getAbsolutePath());
        //c:\\users\\lenovo\\desktop\\demo\\a.txt
		/**
		*	InputStream:字节输入流
		*	
		*/
		InputStream is = null;
		try {
    
    
			is = new FileInputStream("lzc/b.txt");
			//读取数据源的一个字节  read方法每一次读取完成,下一次再进行读取,基于上一次读取的结果向后读
			/*
			 * int read = is.read();
			 * System.out.println(read);
			 * //返回值 如果是read()方法,代表的是每一次读取完成的字节的值,read(byte[])的话返回值不做研究
			 * //不管什么情况下 read的返回值一旦为-1 那么代表数据源没数据了
			 * int read2 = is.read();
			 * System.out.println(read2);
			 */
			//字节流中可以利用的字节数有多少
			int available = is . available ( ) ;
			System.out.println(available);//12
			byte[] array = new byte[3];
			int read = is.read(array);
			String string = new String(array,"UTF-8");
			System.out.println(string);//中
			int read2 = is.read();
			System.out.println(read2);//229
			
		} catch (FileNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			if (is != null) {
    
    
				try {
    
    
					is.close();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		/**
 		* OutputStream基类提供的常用方法:
 		* write(int  字节)
 		* @author lenovo
 		*
 		*/
		OutputStream os = null;
		try {
    
    
			os = new FileOutputStream(new File("lzc/b.txt"));
			os.write(97);//a
			os.write("中国加油".getBytes("UTF-8"));//中国加油
			os.write("中国加油".getBytes("UTF-8"),0,6);//中国
		} catch (FileNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
			if (os !=null) {
    
    
				try {
    
    
					os.close();
				} catch (IOException e) {
    
    
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
				}
		}
		/**
 		* Reader的常用方法
 		* close()
 		* @author lenovo
 		*
 		*/
try {
    
    
			Reader r = new FileReader(new File("lzc/b.txt"));
			int read = r.read();
			System.out.println(read);//97
			int read2 = r.read();
			System.out.println(read2);//20013
			char c = (char)read2;
			System.out.println(c);//中
			char[] buf = new char[5];
			r.read(buf);
			System.out.println(Arrays.toString(buf));//[国, 加, 油, 中, 国]
			r.close();
		} catch (FileNotFoundException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Writer w = new FileWriter(new File("lzc/b.txt"));
		//w.append('a');
		w.write("zs123");
		w.write("1s123");
		w.write("1s123");
		
		w.flush();
		w.close();
	}
		//文件复制
		InputStream is = null;
		OutputStream os = null;
		try {
    
    
		is = new FileInputStream(new File ("c:/users/lenovo/desktop/A.jpg")) ;
		os = new FileOutputStream("c:/users/lenovo/desktop/B.jpg");
		int read;
		byte[] buf = new byte[1024*1024];
		while((read =is.read())!=-1){
    
    
		//while((read =is.read(buf))!=-1){
    
    
			os.write(read);
			//os.write(read^5);
			//os.write(buf);
			}
		} catch(FileNotFoundException e){
    
    
		//TODO Auto-generated catch block
			e.printStackTrace();
	} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
		if (os != null) {
    
    
			try {
    
    
				os.close();
			} catch (IOException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
			}
		if (is != null) {
    
    
			try {
    
    
				is.close();
			} catch (IOException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
		//加密照片		
		FileReader is = null;
		OutputStream os = null;
		try {
    
    
		is = new FileReader(new File ("c:/users/lenovo/desktop/A.png")) ;
		os = new FileOutputStream("c:/users/lenovo/desktop/A.png");
		int read;
		byte[] buf = new byte[1024*1024];
		while((read =is.read())!=-1){
    
    
		//while((read =is.read(buf))!=-1){
    
    
			//os.write(read);
			os.write(read^5);
			//os.write(buf);
			}
		} catch(FileNotFoundException e){
    
    
		//TODO Auto-generated catch block
			e.printStackTrace();
	} catch (IOException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
    
    
		if (os != null) {
    
    
			try {
    
    
				os.close();
			} catch (IOException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
			}
		if (is != null) {
    
    
			try {
    
    
				is.close();
			} catch (IOException e) {
    
    
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

猜你喜欢

转载自blog.csdn.net/cai_4/article/details/131294749