一、Java常用包

1、java.lang. Java编程的基础包 (String,StringBuffer,StringBuild,Math、Object、线程,异常等类)
2、java.util. Java工具包(日期,数组,集合,枚举,各种工具类)
3、java.io. 该包通过文件系统、数据流和序列化提供系统的输入与输出
4、java.net. 跟网络编程有关的包(socket网络编程包)
5、java.sql. 放置的是跟数据库进行交互的类
6、java.awt. java.swing. 这是Java提供的图形界面包
7、java.text. 放置的是自然语言无关的方式来处理文本、日期、数字和消息的类和接口
8、javax. 其他商业公司的扩展包

二、字节流

这种流都是以字节为单位进行数据处理
所有的字节流都继承至:InputStream、OutputStream
图片1.png
图片2.png
字节流,绝大多数情况都是用来处理“二进制文件”,当然也可以用来处理“文本文件”
计算机中,只认识“二进制”,不管是“二进制文件”,还是“文本文件”。它们的区别只在于表现形式不一样而已

2.1 低级字节流

1、专门用来读取文件的FileInputStream,FileOutputStream
2、专门用来读取内存空间的字节数组的 ByteArrayInputStream, ByteArrayOuntputStream
3、专门用来读取其他线程传输过来的字节流 PipedInputStream 和PipedOutputStream

2.1.1 文件输入流 FileInputStream (*)

FileInputStream 是文件输入流,该流属于 低级字节流 因为它直接面向文件

FileInputStream的使用

使用场景:文件上传

  1. package com.woniuxy.java18.study.low;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.Arrays;
  7. /**
  8. * 文件输入流(低级流) 直接操作文件
  9. *
  10. * @author Administrator
  11. *
  12. */
  13. public class FileInputStreamStudy {
  14. public static void main(String[] args) {
  15. // TODO Auto-generated method stub
  16. // firstInputMethod();
  17. secondInputMethod();
  18. }
  19. /**
  20. * 一批一批的读
  21. * 相比于第一种读取 ,有一定的性能提升
  22. */
  23. private static void secondInputMethod() {
  24. // TODO Auto-generated method stub
  25. // 定义文件的路径,并交给文件对象
  26. String path = "D:\\movie\\japan\\db\\jz.txt";
  27. File file = new File(path);
  28. // 定义一个文件输入流
  29. FileInputStream fis = null;
  30. try {
  31. fis = new FileInputStream(file);
  32. //读取文件的内容
  33. byte[] data = new byte[1024];
  34. while(fis.read(data) != -1) {
  35. //输出读取回来的内容
  36. System.out.println(Arrays.toString(data));
  37. }
  38. } catch (Exception e) {
  39. // TODO: handle exception
  40. e.printStackTrace();
  41. }finally {
  42. try {
  43. fis.close();//关闭流
  44. } catch (IOException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. /**
  51. * 一个字节一个字节的读
  52. */
  53. private static void firstInputMethod() {
  54. // TODO Auto-generated method stub
  55. // 定义文件的路径,并交给文件对象
  56. String path = "D:\\movie\\japan\\db\\jz.txt";
  57. File file = new File(path);
  58. // 定义一个文件输入流
  59. FileInputStream fis = null;
  60. try {
  61. // 给流定义 实例
  62. fis = new FileInputStream(file);
  63. // 通过流,读取文件的内容到内存中来
  64. // 代表读取的内容
  65. int content = 0;
  66. while ((content = fis.read()) != -1) {
  67. // 输出(0-255)的值
  68. System.out.println(content);
  69. }
  70. } catch (FileNotFoundException e) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace();
  73. } catch (IOException e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. } finally {
  77. // 关闭流
  78. try {
  79. fis.close();
  80. } catch (IOException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86. }

2.1.2 文件输出流FileOutputStream (*)

使用场景:文件下载
当然它也是低级字节流,它的作用是:将程序内部的资源 写入到外部文件中去

FileOutputStream的用法

  1. package com.woniuxy.java18.study.low;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class FileOutPutStreamStudy {
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8. // firstWriteMethod();
  9. secondWriteMethod();
  10. }
  11. /**
  12. * 一批一批的写
  13. */
  14. private static void secondWriteMethod() {
  15. // TODO Auto-generated method stub
  16. String path = "D:\\movie\\japan\\dj\\xzm.txt";
  17. File file = new File(path);
  18. //定义输出流
  19. FileOutputStream fos = null;
  20. try {
  21. //存不存在
  22. if(!file.exists()) {
  23. file.createNewFile();
  24. }
  25. //实例化输出流
  26. fos = new FileOutputStream(file);
  27. String str = "你老了,可以退休,因为没人看了!!!!!";
  28. byte[] data = str.getBytes("UTF-8");
  29. //向文件中一次型写入内容
  30. fos.write(data);
  31. } catch (Exception e) {
  32. // TODO: handle exception
  33. e.printStackTrace();
  34. }finally {
  35. try {
  36. fos.close();//关闭流
  37. } catch (IOException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. /**
  44. * 一个字节一个字节的写
  45. */
  46. private static void firstWriteMethod() {
  47. // TODO Auto-generated method stub
  48. String path = "D:\\movie\\japan\\dj\\bdy.txt";
  49. File file = new File(path);
  50. //定义输出流
  51. FileOutputStream fos = null;
  52. try {
  53. //存不存在
  54. if(!file.exists()) {
  55. file.createNewFile();
  56. }
  57. //实例化输出流
  58. fos = new FileOutputStream(file);
  59. String str = "这是今年最好的一部电影!!!";
  60. byte[] data = str.getBytes("UTF-8");
  61. for (byte c : data) {
  62. //操作流
  63. fos.write(c);
  64. }
  65. } catch (Exception e) {
  66. // TODO: handle exception
  67. e.printStackTrace();
  68. }finally {
  69. try {
  70. fos.close();//关闭流
  71. } catch (IOException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77. }