FileInputStream

  1. FileInputStream从文件系统中的某个文件中获得输入字节。
  2. 构造方法:
  3. 1.FileInputStream(File file)
  4. 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的File对象file指定。
  5. 2.FileInputStream(String name)
  6. 通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的路径名name指定。
  7. 成员方法:
  8. 1.read() 从此输入流中读取一个数据字节,如果没有输入该方法将会阻塞。
  9. 2.read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
  10. 3.read(byte[] b, int off, int len) 从此输入流中将最多len个字节的数据读入一个byte数组中。
  11. 4.close() 关闭此文件输入流并释放与此流有关的所有系统资源。
  1. //二进制文件读取案例
  2. public static void main(String[] args) {
  3. String filePath = "C:\\Users\\ms674\\Desktop\\File\\img1.png";
  4. //FileInputStream从文件系统中的某个文件中获得输入字节。
  5. FileInputStream fis = null;
  6. try {
  7. //创建FileInputStream对象,用于读取本地文件
  8. fis = new FileInputStream(new File(filePath));
  9. int len;
  10. byte[] bs = new byte[1024];
  11. //如果返回-1表示读取完毕,如果读取正常返回实际读取的字节数
  12. while ((len = fis.read(bs)) != -1) {
  13. System.out.println(new String(bs, 0, len));
  14. }
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. } finally {
  18. if (fis != null) {
  19. try {
  20. fis.close();//关闭本地输入字节流
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. }

FileOutputStream

  1. 文件输出流是用于将数据写入FileFileDescriptor 的输出流。
  2. 构造方法:
  3. 1.FileOutputStream(File file) 创建一个向指定 File对象表示的文件中写入数据的文件输出流。
  4. 2.FileOutputStream(File file, boolean append) 创建一个向指定File对象表示的文件中写入数据的文件输出流。
  5. 成员方法:
  6. 1.close() 关闭此文件输出流并释放与此流有关的所有系统资源。
  7. 2.write(byte[] b) b.length个字节从指定 byte 数组写入此文件输出流中。
  8. 3.write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量off开始的len个字节写入此文件输出流。
  9. 4.write(int b) 将指定字节写入此文件输出流。
  1. //二进制流写文件案例
  2. public static void main(String[] args) {
  3. //创建FilePutStream对象
  4. String filePath = "C:\\Users\\ms674\\Desktop\\File\\file2.txt";
  5. FileOutputStream fos = null;
  6. try {
  7. //得到FileOutputStream对象,如果文件不存在就创建文件
  8. //默认覆盖形式写入
  9. fos = new FileOutputStream(new File(filePath),true);
  10. //写入数据
  11. fos.write("FileOutPutStream...".getBytes());
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. } finally {
  15. if (fos != null) {
  16. try {
  17. fos.close();//关闭FileOutPutStream
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }
  23. }

二进制文件拷贝案例

  1. public static void main(String[] args) {
  2. String sourcePath = "C:\\Users\\ms674\\Desktop\\File\\img1.png";
  3. String copyPath = "C:\\Users\\ms674\\Desktop\\File\\DDD\\copy-img1.png";
  4. FileInputStream fis = null;
  5. FileOutputStream fos = null;
  6. try {
  7. fis = new FileInputStream(sourcePath);
  8. fos = new FileOutputStream(copyPath);
  9. int len;
  10. byte[] bs = new byte[1024];
  11. while ((len = fis.read(bs)) != -1) {
  12. fos.write(bs,0,len);
  13. }
  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. if (fis != null) {
  25. try {
  26. fis.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. }