Java高级部分总结

墨蓝 2023-07-20 10:49 126阅读 0赞

IO流

File类

  1. File(File parent, String child)
  2. 从父抽象路径名和子路径名字符串创建新的 File实例。
  3. File(String pathname)
  4. 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
  5. File(String parent, String child)
  6. 从父路径名字符串和子路径名字符串创建新的 File实例。
  7. public static final String separator
  8. 与系统相关的默认名称 - 分隔符字符,以方便的方式表示为字符串。 该字符串包含一个字符,即separatorChar
  9. File常用方法(File类中的方法只是对文件/文件夹进行操作,并没对文件内容进行操作!!!)
  10. Files. exists():检测文件路径是否存在。
  11. Files. createFile():创建文件。
  12. Files. createDirectory():创建文件夹。
  13. Files. delete():删除一个文件或目录。
  14. Files. copy():复制文件。
  15. Files. move():移动文件。
  16. Files. size():查看文件个数。
  17. Files. read():读取文件。
  18. Files. write():写入文件。
  19. BIO,NIO,AIO 有什么区别?
  20. BIOBlock IO 同步阻塞式 IO,就是我们平常使用的传统 IO,它的特点是模式简单使用方便,并发处理能力低。
  21. NIONon IO 同步非阻塞 IO,是传统 IO 的升级,客户端和服务器端通过 Channel(通道)通讯,实现了多路复用。
  22. AIOAsynchronous IO NIO 的升级,也叫 NIO2,实现了异步非堵塞 IO ,异步 IO 的操作基于事件和回调机制

IO流分类

1.按流向

输入流:InputStream
输出流:OutputStream

2.数据单位

字节流(byte 8bit)
字符流(char 16bit)

3.按流的角色

节点流
处理流

  1. 抽象基类 字节流 字符流
  2. 输入流 InputStream Reader
  3. 输出流 OutputStream Writer
  4. /**
  5. * 从文件中读入数据到内存操作时:
  6. * 1.read()方法读入到末尾返回-1
  7. * 2.为了保证流一定能正常关闭,采取try-catch-finally进行处理
  8. * 3.读入文件一定要存在,否则会报FileNorFoundException
  9. */
  10. @Test
  11. public void test1() {
  12. File file1 = null;
  13. FileReader fileReader = null;
  14. try {
  15. file1 = new File("Hello.txt");
  16. fileReader = new FileReader(file1);
  17. int i = fileReader.read();
  18. while (i != -1) {
  19. System.out.print((char) i);
  20. i = fileReader.read();
  21. }
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. } finally {
  25. try {
  26. if (fileReader != null) {
  27. fileReader.close();
  28. }
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. @Test
  35. public void test2() {
  36. File file = null;
  37. FileReader fileReader = null;
  38. try {
  39. file = new File("Hello.txt");
  40. fileReader = new FileReader(file);
  41. char[] chars = new char[5];
  42. int len;
  43. while ((len = fileReader.read(chars)) != -1) {
  44. //方式1:
  45. //错误的
  46. // for (int i = 0; i < chars.length; i++) {
  47. // System.out.print(chars[i]);
  48. // }
  49. //正确的
  50. // for (int i = 0; i < len; i++) {
  51. // System.out.print(chars[i]);
  52. // }
  53. //方式2:
  54. //错误的
  55. // String string = new String(chars);
  56. // System.out.println(string);
  57. //正确的
  58. String string = new String(chars,0,len);
  59. System.out.println(string);
  60. }
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. } finally {
  64. if (fileReader != null) {
  65. try {
  66. fileReader.close();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * 从内存中写出数据到硬盘文件
  75. * 1.写出过程中,File("xxx")指定文件可以不存在,会自动创建
  76. * 2.File("xxx")存在:
  77. * 2.1:如果使用File(file)/File(file,false),会进行原有文件的覆盖
  78. * 2.2:如果使用File(file,true),则会在原有文件的基础上进行追加内容
  79. */
  80. @Test
  81. public void test1() {
  82. File file = null;
  83. FileWriter fileWriter = null;
  84. try {
  85. file = new File("Hello1.txt");
  86. fileWriter = new FileWriter(file);
  87. fileWriter.write("我是好人!");
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. } finally {
  91. if (fileWriter != null) {
  92. try {
  93. fileWriter.close();
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * 将文件中的数据读入内存,然后写出到硬盘中的文件
  102. */
  103. @Test
  104. public void test1() {
  105. FileReader fileReader = null;
  106. FileWriter fileWriter = null;
  107. try {
  108. File rfile = new File("Hello.txt");
  109. File wfile = new File("Hello2.txt");
  110. fileReader = new FileReader(rfile);
  111. fileWriter = new FileWriter(wfile);
  112. char[] chars = new char[5];
  113. int len;
  114. while ((len = fileReader.read(chars)) != -1) {
  115. fileWriter.write(chars, 0, len);
  116. }
  117. } catch (IOException e) {
  118. e.printStackTrace();
  119. } finally {
  120. try {
  121. if(fileWriter!=null){
  122. fileWriter.close();
  123. }
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }
  127. try {
  128. if(fileReader!=null){
  129. fileReader.close();
  130. }
  131. } catch (IOException e) {
  132. e.printStackTrace();
  133. }
  134. }
  135. }
  136. @Test
  137. public void test1(){
  138. FileInputStream fileInputStream = null;
  139. FileOutputStream fileOutputStream = null;
  140. try {
  141. File rfile = new File("C:\\Users\\xxx\\Desktop\\Screenshot.png");
  142. File wfile = new File("C:\\Users\\xxx\\Desktop\\shot.png");
  143. fileInputStream = new FileInputStream(rfile);
  144. fileOutputStream = new FileOutputStream(wfile);
  145. byte[] bytes = new byte[1024];
  146. int len;
  147. while ((len = fileInputStream.read(bytes)) != -1){
  148. fileOutputStream.write(bytes,0,len);
  149. }
  150. } catch (IOException e) {
  151. e.printStackTrace();
  152. } finally {
  153. if (fileOutputStream != null){
  154. try {
  155. fileInputStream.close();
  156. } catch (IOException e) {
  157. e.printStackTrace();
  158. }
  159. }
  160. if (fileInputStream != null){
  161. try {
  162. fileInputStream.close();
  163. } catch (IOException e) {
  164. e.printStackTrace();
  165. }
  166. }
  167. }
  168. }

缓冲流作用在已有流的基础上的(不仅节点流),加快读写速度,代码和节点流的一样。
转换流是讲字节流转成字符流,带内存,再将字符流转成字节流到硬盘文件(FileInputStream——->FileReader——->内存——->FileWirter——->FileOutputStream)

对象流

  1. @Test
  2. //序列化
  3. //将内存中的对象保存到磁盘中
  4. public void test1(){
  5. ObjectOutputStream objectOutputStream = null;
  6. try {
  7. objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("hi.txt")));
  8. objectOutputStream.writeObject(new String("我爱中国"));
  9. objectOutputStream.flush();
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. } finally {
  13. if(objectOutputStream != null){
  14. try {
  15. objectOutputStream.close();
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21. }
  22. @Test
  23. //反序列化
  24. //从磁盘中将对象还原到内存
  25. public void test2(){
  26. ObjectInputStream objectInputStream = null;
  27. try {
  28. objectInputStream = new ObjectInputStream(new FileInputStream(new File("hi.txt")));
  29. Object readObject = objectInputStream.readObject();
  30. System.out.println(readObject);
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. } catch (ClassNotFoundException e) {
  34. e.printStackTrace();
  35. } finally {
  36. if(objectInputStream != null){
  37. try {
  38. objectInputStream.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. }

序列化机制:自定义类需要实现Serializable接口,并显示定义全局常量serialVersionUID,属性要保证是可序列化的,被static/transient修饰的属性是不能被序列化

发表评论

表情:
评论列表 (有 0 条评论,126人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java高级部分总结

    反射(框架的灵魂) > JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取

    相关 Java高级部分总结

    TCP&&UDP&&URL(传输层主要使用的协议) > 1.UDP不提供复杂的控制机制,发送数据之前不需要建立连接。并且它是将应用程序发来的数据在收到的那一刻,立刻按照原

    相关 Java高级部分总结

    枚举类 类的对象有限个,确定,叫做枚举类 当需要定义一组常量,强烈建议使用枚举类 当枚举类的对象只有一个时,则可以作为单例模式的方式处理 1.使用enum关键字

    相关 Java高级部分总结

    集合框架和数组 数组: 一旦初始化,数组长度就确立下来,不能修改 数组一旦定义好,类型也就确立了 对数组的添加,删除,插入等操作不方便,效率不高