• 抽象基类 节点流(或文件流) 缓冲流(处理流的一种)
      InputStream FileInputStream BufferedFileInputStream
      OutputStream FileOutputStream BufferedFileOutputStream

    /*
    测试FileInputStream和FileOutputStream的使用

    结论:
    1,对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
    2,对于非文本文件(.jpj,.mp3,.mp4,.doc,.ppt…..),使用字节流处理
    *

    1. package com.atguigu.java1;
    2. import org.junit.Test;
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.io.IOException;
    7. /**
    8. * 测试FileInputStream和FileOutputStream的使用
    9. *
    10. * 结论:
    11. * 1,对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
    12. * 2,对于非文本文件(.jpj,.mp3,.mp4,.doc,.ppt.....),使用字节流处理
    13. *
    14. *
    15. * @author Dxkstart
    16. * @create 2021-05-30 12:41
    17. */
    18. public class File_Input_OutputStream_Test {
    19. //使用字节流FileInputStream处理文本文件,可能出现乱码。
    20. @Test
    21. public void test_FileInputStream(){
    22. FileInputStream fis = null;
    23. try {
    24. //1.造文件
    25. File file = new File("Hello1.txt");
    26. //2.造流
    27. fis = new FileInputStream(file);
    28. //3.读数据
    29. byte[] buffer = new byte[5];
    30. int len;//记录每次读取的字节的个数
    31. while((len = fis.read(buffer)) != -1) {
    32. String str = new String(buffer, 0, len);
    33. System.out.print(str);
    34. }
    35. } catch (IOException e) {
    36. e.printStackTrace();
    37. } finally {
    38. try {
    39. //4.关闭流资源
    40. if(fis != null) {
    41. fis.close();
    42. }
    43. } catch (IOException e) {
    44. e.printStackTrace();
    45. }
    46. }
    47. }
    48. /*
    49. 实现对图片的复制操作
    50. */
    51. @Test
    52. public void test_FileInput_OutputStream(){
    53. FileInputStream fis = null;
    54. FileOutputStream fos = null;
    55. try {
    56. //
    57. File file1 = new File("C:\\Users\\Administrator\\Desktop\\IO\\图片\\爱丽丝.jpg");
    58. File file2 = new File("爱丽丝1.jpg");
    59. //
    60. fis = new FileInputStream(file1);
    61. fos = new FileOutputStream(file2);
    62. //复制的过程
    63. byte[] buffer = new byte[5];
    64. int len;
    65. while((len = fis.read(buffer)) != -1){
    66. fos.write(buffer,0,len);
    67. }
    68. } catch (IOException e) {
    69. e.printStackTrace();
    70. } finally {
    71. //
    72. try {
    73. if(fis != null) {
    74. fis.close();
    75. }
    76. } catch (IOException e) {
    77. e.printStackTrace();
    78. }
    79. try {
    80. if(fos != null) {
    81. fos.close();
    82. }
    83. } catch (IOException e) {
    84. e.printStackTrace();
    85. }
    86. }
    87. }
    88. //指定路径下文件的复制
    89. @Test
    90. public void copyFile(String srcPath,String destPath){
    91. FileInputStream fis = null;
    92. FileOutputStream fos = null;
    93. try {
    94. //
    95. File file1 = new File(srcPath);
    96. File file2 = new File(destPath);
    97. //
    98. fis = new FileInputStream(file1);
    99. fos = new FileOutputStream(file2);
    100. //复制的过程
    101. byte[] buffer = new byte[1024];
    102. int len;
    103. while((len = fis.read(buffer)) != -1){
    104. fos.write(buffer,0,len);
    105. }
    106. } catch (IOException e) {
    107. e.printStackTrace();
    108. } finally {
    109. //
    110. try {
    111. if(fis != null) {
    112. fis.close();
    113. }
    114. } catch (IOException e) {
    115. e.printStackTrace();
    116. }
    117. try {
    118. if(fos != null) {
    119. fos.close();
    120. }
    121. } catch (IOException e) {
    122. e.printStackTrace();
    123. }
    124. }
    125. }
    126. @Test
    127. public void testCopyFile(){
    128. long start = System.currentTimeMillis();
    129. String srcPath = "C:\\Users\\Administrator\\Desktop\\IO\\图片\\螺旋丸.jpg";
    130. String destPath = "C:\\Users\\Administrator\\Desktop\\IO\\图片\\螺旋丸2.jpg";
    131. copyFile(srcPath,destPath);
    132. long end = System.currentTimeMillis();
    133. System.out.println("复制操作花费的时间为:" + (end - start));
    134. }
    135. }
    1. package com.atguigu.java1;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. /**
    7. * @author Dxkstart
    8. * @create 2021-05-30 14:13
    9. */
    10. public class testCopy {
    11. public static void main(String[] args) {
    12. long start = System.currentTimeMillis();
    13. String srcPath = "C:\\Users\\Administrator\\Desktop\\IO\\视频\\01.mp4";
    14. String destPath = "C:\\Users\\Administrator\\Desktop\\IO\\视频\\02.mp4";
    15. new copy().copyFile(srcPath,destPath);
    16. long end = System.currentTimeMillis();
    17. System.out.println("复制操作花费的时间为:" + (end - start)); //380ms
    18. }
    19. }
    20. class copy{
    21. public void copyFile(String srcPath,String destPath){
    22. FileInputStream fis = null;
    23. FileOutputStream fos = null;
    24. try {
    25. //
    26. File file1 = new File(srcPath);
    27. File file2 = new File(destPath);
    28. //
    29. fis = new FileInputStream(file1);
    30. fos = new FileOutputStream(file2);
    31. //复制的过程
    32. byte[] buffer = new byte[102400];
    33. int len;
    34. while((len = fis.read(buffer)) != -1){
    35. fos.write(buffer,0,len);
    36. }
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. } finally {
    40. //
    41. try {
    42. if(fis != null) {
    43. fis.close();
    44. }
    45. } catch (IOException e) {
    46. e.printStackTrace();
    47. }
    48. try {
    49. if(fos != null) {
    50. fos.close();
    51. }
    52. } catch (IOException e) {
    53. e.printStackTrace();
    54. }
    55. }
    56. }
    57. }