1、昨日复习

1.如何遍历Map的key集,value集,key-value集,使用上泛型

Map map = new HashMap();
map.put();….
//遍历key
Set keySet = map.keySet();
for(String key : keySet){
System.out.println(key);
}

//遍历value
Collection values = map.values();
Iterator iterator = values.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}

//遍历key-value
Set> entrySet = map.entrySet();
Iterator> iterator = entrySet.iterator();
while(iterator.hasNext()){
Map.Entry entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + “—->” + value);
}

  1. 写出使用Iterator 和 增强for 循环遍历List的代码,使用上泛型
  1. 提供一个方法,用于遍历获取HashMap中的所有value,并存放在List中返回。考虑上集合中泛型的使用。

public List getValueList(HashMap map){
ArrayList valueList = new ArrayList<>():
Collection values = map.values();
for(String value : values){
valueList.add(value);
}
return valueList;
}

  1. 创建一个与a.txt文件同目录下的另外一个文件b.txt

File file1 = new File(“d:\a\a.txt”);
File file2 = new File(file1.getParent(),”b.txt”);

  1. Map接口中的常用方法有哪些
    增:put(K k,V v)
    删: V remove(K k)
    改: put(K k,V v)
    查: V get(K k)
    长度:int size()
    遍历

2、File类的使用

  1. package com.atguigu.java1;
  2. import org.junit.Test;
  3. import java.io.File;
  4. import java.io.IOException;
  5. /**
  6. * File类的使用
  7. *
  8. * 1、File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)
  9. * 2、File类声明在java.io包下
  10. * 3、File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法,
  11. * 并未涉及到写入或读取文件内容的操作。如果需要读取或写入问文件内容,必须使用IO流来完成。
  12. * 4、后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的“终点”。
  13. *
  14. */
  15. public class FileTest {
  16. /*
  17. 1、如何创建File类的实例
  18. public File(String pathname)
  19. public File(String parent,String child)
  20. public File(File parent,String child)
  21. 2、
  22. 相对路径:相较于某个路径下,指明的路径
  23. 绝对路径:包含盘符在内的文件或文件目录的路径
  24. 3、路径分隔符:
  25. Windows:\\
  26. Unix:/
  27. */
  28. @Test
  29. public void test1(){
  30. //构造器1
  31. File file1 = new File("hello.text");//相对于当前的module
  32. File file2 = new File("D:\\IdeaProjects\\StudyJava\\Day25\\he.txt");
  33. System.out.println(file1);
  34. System.out.println(file2);
  35. //构造器2
  36. File file3 = new File("D:\\IdeaProjects\\StudyJava","JavaSenior");
  37. System.out.println(file3);
  38. //构造器3
  39. File file4 = new File(file3,"hi.txt");
  40. System.out.println(file4);
  41. }
  42. /*
  43.  public String getAbsolutePath():获取绝对路径
  44.  public String getPath() :获取路径
  45.  public String getName() :获取名称
  46.  public String getParent():获取上层文件目录路径。若无,返回null
  47.  public long length() :获取文件长度(即:字节数)。不能获取目录的长度。
  48.  public long lastModified() :获取最后一次的修改时间,毫秒值
  49. 如下的两个方法适用于文件目录
  50.  public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
  51.  public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File数组
  52. */
  53. @Test
  54. public void test2(){
  55. File file1 = new File("hello.txt");
  56. File file2 = new File("D:\\io\\hi.txt");
  57. System.out.println(file1.getAbsolutePath());
  58. System.out.println(file1.getPath());
  59. System.out.println(file1.getName());
  60. System.out.println(file1.getParent());
  61. System.out.println(file1.length());
  62. System.out.println(file1.lastModified());
  63. System.out.println("***********************");
  64. System.out.println(file2.getAbsolutePath());
  65. System.out.println(file2.getPath());
  66. System.out.println(file2.getName());
  67. System.out.println(file2.getParent());
  68. System.out.println(file2.length());
  69. System.out.println(file2.lastModified());
  70. }
  71. @Test
  72. public void test3(){
  73. File file = new File("D:\\IdeaProjects\\StudyJava");
  74. String[] list = file.list();
  75. for (String s : list){
  76. System.out.println(s);
  77. }
  78. File[] files = file.listFiles();
  79. for (File f:files){
  80. System.out.println(f);
  81. }
  82. }
  83. /*
  84. public boolean renameTo(File dest):把文件重命名为指定的文件路径
  85. 比如file1.renameTo(file2)为例:
  86. 要想保证返回true,需要file1在硬盘中是存在的,且file2不能再硬盘中存在
  87. */
  88. @Test
  89. public void test4(){
  90. File file1 = new File("hello.txt");
  91. File file2 = new File("D:\\io\\hi.txt");
  92. boolean renameTo = file2.renameTo(file1);
  93. System.out.println(renameTo);
  94. }
  95. /*
  96.  public boolean isDirectory():判断是否是文件目录
  97.  public boolean isFile() :判断是否是文件
  98.  public boolean exists() :判断是否存在
  99.  public boolean canRead() :判断是否可读
  100.  public boolean canWrite() :判断是否可写
  101.  public boolean isHidden() :判断是否隐藏
  102. */
  103. @Test
  104. public void test5(){
  105. File file1 = new File("hello.txt");
  106. System.out.println(file1.isDirectory());
  107. System.out.println(file1.isFile());
  108. System.out.println(file1.exists());
  109. System.out.println(file1.canRead());
  110. System.out.println(file1.canWrite());
  111. System.out.println(file1.isHidden());
  112. }
  113. /*
  114. 创建硬盘中对应的文件或文件目录
  115.  public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
  116.  public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。
  117.  public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建
  118. public boolean delete():删除文件或者文件夹
  119. Java中的删除不走回收站。 要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录
  120. */
  121. @Test
  122. public void test6() throws IOException {
  123. //文件的创建
  124. File file1 = new File("hi.txt");
  125. if (!file1.exists()){
  126. file1.createNewFile();
  127. System.out.println("创建成功");
  128. }else {
  129. file1.delete();
  130. System.out.println("删除成功");
  131. }
  132. }
  133. @Test
  134. public void test7(){
  135. //文件目录的创建
  136. File file1 = new File("D:\\io\\io1");
  137. boolean mkdir = file1.mkdir();
  138. if (mkdir){
  139. System.out.println("创建成功");
  140. }
  141. File file2 = new File("D:\\io\\io2");
  142. boolean mkdir1 = file2.mkdirs();
  143. if (mkdir1){
  144. System.out.println("创建成功2");
  145. }
  146. }
  147. }

3、IO流原理及流的分类

I/O是Input/Output的缩写, I/O技术是非常实用的技术,用于处理设备之间的数据传输
输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中。


QQ截图20220124205601.png
文本数据:字符流;非文本数据:字节流
QQ截图20220124210220.png
QQ截图20220124210249.png
QQ截图20220125093122.png

  1. package com.atguigu.java1;
  2. import org.junit.Test;
  3. import java.io.*;
  4. /*
  5. 一、流的分类:
  6. 1、操作数据单位:字节流、字符流
  7. 2、数据的流向:输入流、输出流
  8. 3、流的角色:节点流、处理流
  9. 二、流的体系结构
  10. 抽象基类 节点流 缓冲流
  11. InputStream FileInputStream BufferedInputStream
  12. OutputStream FileOutputStream BufferedOutputStream
  13. Reader FileReader BufferedReader
  14. Writer FileWriter BufferedWriter
  15. */
  16. public class FileReadWriterTest {
  17. /*
  18. 说明点:
  19. 1、read()的理解:返回读入的一个字符,如果达到文件末尾,返回-1
  20. 2、异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
  21. 3、读入的文件一定要存在,否则就会报异常
  22. */
  23. @Test
  24. public void test1() {
  25. FileReader fr = null;
  26. try {
  27. //1、实例化File类的对象,指明要操作的对象
  28. File file = new File("hello.txt");
  29. //2、提供具体的流
  30. fr = new FileReader(file);
  31. //3、数据的读入
  32. //read():返回读入的一个字符。如果达到文件末尾,返回-1
  33. //方式一
  34. /*int data = fr.read();
  35. while (data!=-1){
  36. System.out.print((char) data);
  37. data = fr.read();
  38. }*/
  39. //方式二
  40. int data;
  41. while ((data = fr.read()) != -1) {
  42. System.out.print((char) data);
  43. }
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. } finally {
  47. //4、流的关闭操作
  48. try {
  49. fr.close();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. //对read()操作升级,使用read重载方法
  56. @Test
  57. public void test2() {
  58. FileReader fr = null;
  59. try {
  60. //1、File类的实例化
  61. File file = new File("hello.txt");
  62. //2、FileReader流的实例化
  63. fr = new FileReader(file);
  64. //3、读入的操作
  65. //read(char[] cbuf):返回每次读入cbuf数组中的字符的个数,如果达到文件末尾,返回-1
  66. char[] ch = new char[5];
  67. int len;
  68. while ((len = fr.read(ch)) != -1) {
  69. for (int i = 0; i < len; i++) {
  70. System.out.print(ch[i]);
  71. }
  72. }
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. } finally {
  76. //4、资源的关闭
  77. try {
  78. fr.close();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }
  84. /*
  85. 从内存中写出数据到硬盘的文件里。
  86. 说明:
  87. 1、输出操作,对应的File可以不存在的。并不会报异常
  88. 2、
  89. File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件
  90. File对应的硬盘中的文件如果存在:
  91. 如果流使用的构造器是FileWriter(file,false)/FileWriter(file):对原有的文件的覆盖
  92. 如果流使用的构造器是FileWriter(file,true):不会对原有的文件的覆盖,而是在原有文件基础上追加的内容
  93. */
  94. @Test
  95. public void test3() throws IOException {
  96. //1、提供File类的对象,指明写出到的文件
  97. File file = new File("hello1.txt");
  98. //2、提供FileWriter的对象,用于数据的写出
  99. FileWriter fw = new FileWriter(file, false);
  100. //3、写出的操作
  101. fw.write("I have a dream!\n");
  102. fw.write("you need to have a dream!");
  103. //4、流的关闭
  104. fw.close();
  105. }
  106. @Test
  107. public void test4() {
  108. FileReader fr = null;
  109. FileWriter fw = null;
  110. try {
  111. // 1、创建File类的对象,指明读入和写出的文件
  112. File srcFile = new File("hello.txt");
  113. File destFile = new File("hello2.txt");
  114. // 2、创建输入流和输出流的对象
  115. fr = new FileReader(srcFile);
  116. fw = new FileWriter(destFile);
  117. // 3、数据的读入和写出操作
  118. char[] cbuf = new char[5];
  119. int len;
  120. while ((len = fr.read(cbuf)) != -1) {
  121. fw.write(cbuf, 0, len);
  122. }
  123. } catch (IOException e) {
  124. e.printStackTrace();
  125. }finally {
  126. // 4、关闭流资源
  127. try {
  128. fw.close();
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. }
  132. try {
  133. fr.close();
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
  139. }

结论:
1、对于文本文件(.txt,.java,.c,.cpp),使用字符流处理,也可以用字节流来处理(但最好不要在控制输出台看)
2、对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,…),使用字节流处理


处理流之一:缓冲流的使用
1、缓冲流:
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
2、作用:提供流的读取、写入的速度
提高读写速度的原因:内部提供了一个缓冲区
3、处理流,就是“套接”在已有的流的基础上。

 为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类
时,会创建一个内部缓冲区数组,缺省使用8192个字节(8Kb)的缓冲区。
 缓冲流要“套接”在相应的节点流之上,根据数据操作单位可以把缓冲流分为:
BufferedInputStream 和 BufferedOutputStream
BufferedReader 和 BufferedWriter

 当读取数据时,数据按块读入缓冲区,其后的读操作则直接访问缓冲区
 当使用BufferedInputStream读取字节文件时,BufferedInputStream会一次性从文件中读取8192个(8Kb),存在缓冲区中,直到缓冲区装满了,才重新从文件中读取下一个8192个字节数组。
 向流中写入字节时,不会直接写到文件,先写到缓冲区中直到缓冲区写满,BufferedOutputStream才会把缓冲区中的数据一次性写到文件里。使用方法flush()可以强制将缓冲区的内容全部写入输出流
 关闭流的顺序和打开流的顺序相反。只要关闭最外层流即可,关闭最外层流也会相应关闭内层节点流
 flush()方法的使用:手动将buffer中内容写入文件
 如果是带缓冲区的流对象的close()方法,不但会关闭流,还会在关闭流之前刷新缓冲区,关闭后不能再写出

  1. BufferedReader br = null;
  2. BufferedWriter bw = null;
  3. try
  4. {
  5. // 创建缓冲流对象:它是处理流,是对节点流的包装
  6. br = new BufferedReader(new FileReader("d:\\IOTest\\source.txt"));
  7. bw = new BufferedWriter(new FileWriter("d:\\IOTest\\dest.txt"));
  8. String str;
  9. while ((str = br.readLine()) != null) { // 一次读取字符文本文件的一行字符
  10. bw.write(str); // 一次写入一行字符串
  11. bw.newLine(); // 写入行分隔符
  12. }
  13. bw.flush(); // 刷新缓冲区
  14. } catch(
  15. IOException e)
  16. {
  17. e.printStackTrace();
  18. } finally
  19. {
  20. // 关闭IO流对象
  21. try {
  22. if (bw != null) {
  23. bw.close(); // 关闭过滤流时,会自动关闭它所包装的底层节点流
  24. }
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. try {
  29. if (br != null) {
  30. br.close();
  31. }
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }

QQ截图20220125162802.png

  1. package com.atguigu.java2;
  2. /*
  3. 处理流之二:转换流的使用
  4. 1、转换流:
  5. InputStreamReader:将一个字节的输入流转换为字符的输入流
  6. OutputStreamWriter:将一个字符的输出流转换为字节的输出流
  7. 2、作用:提供字节流与字符流之间的转换
  8. 3、解码:字节、字节数组---->字符数组、字符串
  9. 编码:字符数组、字符串---->字节、字节数组
  10. 4、字符集
  11.  ASCII:美国标准信息交换码。
  12.  用一个字节的7位可以表示。
  13.  ISO8859-1:拉丁码表。欧洲码表
  14.  用一个字节的8位表示。
  15.  GB2312:中国的中文编码表。最多两个字节编码所有字符
  16.  GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
  17.  Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
  18.  UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。
  19. */
  20. import org.junit.Test;
  21. import java.io.*;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. public class InputStreamReaderTest {
  25. @Test
  26. public void test1() throws IOException {
  27. FileInputStream fis = new FileInputStream("dbcp.txt");
  28. //参数2指明字符集,具体使用哪个字符集取决于文件保存时使用的字符集
  29. InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
  30. char[] cbuf = new char[20];
  31. int len;
  32. while ((len = isr.read(cbuf)) !=-1){
  33. String str = new String(cbuf,0,len);
  34. System.out.print(str);
  35. }
  36. isr.close();
  37. fis.close();
  38. }
  39. @Test
  40. public void test2(){
  41. File file1 = new File("dbcp.txt");
  42. File file2 = new File("dbcp_gbk.txt");
  43. InputStreamReader isr = null;
  44. OutputStreamWriter osw = null;
  45. try {
  46. FileInputStream fis = new FileInputStream(file1);
  47. FileOutputStream fos = new FileOutputStream(file2);
  48. isr = new InputStreamReader(fis,"UTF-8");
  49. osw = new OutputStreamWriter(fos,"gbk");
  50. char[] ch = new char[20];
  51. int len;
  52. while ((len = isr.read(ch))!=-1){
  53. osw.write(ch,0,len);
  54. }
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }finally {
  58. try {
  59. isr.close();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. try {
  64. osw.close();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. }
  70. }
  1. package com.atguigu.java2;
  2. import org.junit.Test;
  3. import java.io.*;
  4. import java.util.Locale;
  5. import java.util.Scanner;
  6. /*
  7. 其他流的使用:
  8. 1、标准的输入、输出流
  9. 2、打印流
  10. 3、数据流
  11. */
  12. public class OtherStreamTest {
  13. /*
  14. 1、标准的输入、输出流
  15. 1.1
  16. System.in:标准的输入流,默认从键盘输入
  17. System.out:标准的输出流,默认从控制台输出
  18. 1.2
  19. System类的setIn(InputStream is)/setOut(PrintStream out) 方式重新指定输入和输出的流。
  20. 1.3练习
  21. 从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,
  22. 直至当输入“e”或者“exit”时,退出程序。
  23. 方法一:使用Scanner实现,调用next()返回一个字符串
  24. 方法二:使用System.in实现。System.in--->转换流--->BufferedReader的readLine()方法
  25. */
  26. public static void main(String[] args) {
  27. BufferedReader br = null;
  28. try {
  29. InputStreamReader isr = new InputStreamReader(System.in);
  30. br = new BufferedReader(isr);
  31. while (true){
  32. System.out.println("请输入字符串:");
  33. String data = br.readLine();
  34. if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
  35. System.out.println("程序结束");
  36. break;
  37. }
  38. String upperCase = data.toUpperCase();
  39. System.out.println(upperCase);
  40. }
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }finally {
  44. try {
  45. br.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51. /*
  52. 2、打印流:PrintStream和PrintWriter实现将基本数据类型的数据格式转化为字符串输出
  53. 2.1、提供了一系列重载的print()和println()
  54. 2.2、练习:
  55. */
  56. @Test
  57. public void test2(){
  58. PrintStream ps = null;
  59. try {
  60. FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
  61. // 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
  62. ps = new PrintStream(fos, true);
  63. if (ps != null) {// 把标准输出流(控制台输出)改成文件
  64. System.setOut(ps);
  65. }
  66. for (int i = 0; i <= 255; i++) { // 输出ASCII字符
  67. System.out.print((char) i);
  68. if (i % 50 == 0) { // 每50个数据一行
  69. System.out.println(); // 换行
  70. } }
  71. } catch (FileNotFoundException e) {
  72. e.printStackTrace();
  73. } finally {
  74. if (ps != null) {
  75. ps.close();
  76. } }
  77. }
  78. /*
  79. 3、数据流
  80. 3.1 DataInputStream 和 DataOutputStream
  81. 3.2 用于读取和写出基本数据类型、String类的数据
  82. */
  83. }

尚硅谷宋红康第13章_IO流.pdf
尚硅谷宋红康计算机字符编码.pdf
拓展:装饰设计模式.pdf