1、使用IO字节流读取文件数据

  1. /**
  2. * 使用IO字节流读取文件数据
  3. *
  4. * @param path 文件的路径,可以是相对路径或者绝对路径
  5. */
  6. public static void readFileByFileInputStream(String path) {
  7. System.out.println("=== 读取文件开始 Start ===");
  8. // 根据路径拿到文件对象,文件对象可以获取更多的文件信息
  9. File file = new File(path);
  10. // 初始化输入流
  11. InputStream inputStream = null;
  12. try {
  13. // 创建字节输入流
  14. inputStream = new FileInputStream(file);
  15. // 创建1kb字节数组用来
  16. byte[] buffer = new byte[2048];
  17. // 每次读取的字节数
  18. int len;
  19. // 读取数据并放到buffer数组中
  20. while ((len = inputStream.read(buffer)) != -1) {
  21. // UTF-8为变长编码,一个汉字占3个字节
  22. System.out.println("本次读取" + len + "个字节数据内容为:" + new String(buffer, 0, len));
  23. }
  24. } catch (FileNotFoundException e) {
  25. // 文件未找到时异常处理
  26. e.printStackTrace();
  27. } catch (IOException e) {
  28. // 读取过程中,删除文件会出此异常
  29. e.printStackTrace();
  30. } finally { // 关闭流过程,也有可能出现异常
  31. try {
  32. if (inputStream != null) inputStream.close();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. System.out.println("=== 读取文件 End ===");
  38. }

2、写入文件通过IO字节流方法

  1. /**
  2. * 写入文件通过IO字节流方法
  3. *
  4. * @param path 输出文件的路径
  5. */
  6. public static void writeFileByOS(String path) {
  7. System.out.println("=== 写入文件 Start ===");
  8. // 1、创建文件对象
  9. File file = new File(path);
  10. // 2、初始化字节输出流
  11. OutputStream outputStream = null;
  12. // 3、写出内容
  13. String outInfo = "唐建-写出测试outputstream";
  14. // 4、内容转成字节数组
  15. byte[] byteArray = outInfo.getBytes();
  16. try {
  17. // 输出字节流(file,true)接通管道,这里有true的话,代表可以在文件后面追加内容
  18. outputStream = new FileOutputStream(file);
  19. // 输出流写入字节数据
  20. outputStream.write(byteArray);
  21. System.out.println("按照字节流成功写出内容:" + outInfo);
  22. } catch (FileNotFoundException e) {
  23. e.printStackTrace();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. } finally { // 关闭写出流时,注意抓异常
  27. try {
  28. if (outputStream != null) outputStream.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. System.out.println("=== 写入文件 End ===");
  34. }

3、文件复制 try catch finally

  1. /**
  2. * 通过字节流的方式将文件内容拷贝到另一个文件中
  3. * <p>
  4. * Step 1.根据文件路径,构建源文件对象
  5. * Step 2.根据文件路径,构造目的文件对象
  6. * Step 3.创建字节输入流从源文件中读取信息
  7. * Step 4.将读入到内存的信息再写出到目的文件中
  8. * Step 5.拷贝完成后关闭输入输出流
  9. */
  10. public static void copyFile(String from, String to) {
  11. System.out.println("=== 复制文件 Start ===");
  12. // 输入文件对象
  13. File inFile = new File(from);
  14. // 输出文件对象
  15. File outFile = new File(to);
  16. // 初始化输入流
  17. InputStream inputStream = null;
  18. // 初始化输出流
  19. OutputStream outputStream = null;
  20. try {
  21. // 将输入流和输入文件对象接通
  22. inputStream = new FileInputStream(inFile);
  23. // 将输出流和输出文件对象接通
  24. outputStream = new FileOutputStream(outFile);
  25. // 定义一个字节数组转移数据
  26. byte[] buffer = new byte[1024];
  27. int len;//记录每次读取的字节数
  28. while ((len=inputStream.read(buffer))!=-1){
  29. outputStream.write(buffer,0,len); //读多少,输出多少
  30. }
  31. System.out.println("复制文件成功完成");
  32. } catch (FileNotFoundException e) {
  33. e.printStackTrace();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. } finally { // 关闭输入流异常后,也要保证输出流关闭
  37. if (inputStream != null) {
  38. try {
  39. inputStream.close();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. } finally {
  43. if (outputStream != null) {
  44. try {
  45. outputStream.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51. }
  52. }
  53. System.out.println("=== 复制文件 End ===");
  54. }

4、文件复制try catch resouce写法

  1. public static void copyFile(String from, String to) {
  2. System.out.println("=== 复制文件 Start ===");
  3. // 输入文件对象
  4. File inFile = new File(from);
  5. // 输出文件对象
  6. File outFile = new File(to);
  7. try (// 将输入流和输入文件对象接通
  8. InputStream inputStream = new FileInputStream(inFile);
  9. // 将输出流和输出文件对象接通
  10. OutputStream outputStream = new FileOutputStream(outFile);
  11. ) { // 定义一个字节数组转移数据
  12. byte[] buffer = new byte[1024];
  13. int len;//记录每次读取的字节数
  14. while ((len = inputStream.read(buffer)) != -1) {
  15. outputStream.write(buffer, 0, len); //读多少,输出多少
  16. }
  17. System.out.println("复制文件成功完成");
  18. } catch (FileNotFoundException e) {
  19. e.printStackTrace();
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. System.out.println("=== 复制文件 End ===");
  24. }