4f734c8a95528ae50ad91201478684bcd92c8ff2.svn-base 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package cn.com.goldenwater.dcproj.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. public class IoUtil {
  7. /**
  8. * 写出
  9. *
  10. * @param file
  11. * @param out
  12. */
  13. public static void writeFile(File file, OutputStream out) {
  14. ;
  15. try (FileInputStream fs = new FileInputStream(file)) {
  16. //循环写入输出流
  17. byte[] b = new byte[1024 * 8];
  18. int len;
  19. while ((len = fs.read(b)) > 0) {
  20. out.write(b, 0, len);
  21. }
  22. } catch (Exception e) {
  23. throw new RuntimeException("流写出错误");
  24. } finally {
  25. close(out);
  26. }
  27. }
  28. /**
  29. * 关流
  30. */
  31. public static void close(InputStream is) {
  32. if (is != null) {
  33. try {
  34. is.close();
  35. is = null;
  36. } catch (Exception e2) {
  37. throw new RuntimeException("关闭流失败");
  38. }
  39. }
  40. }
  41. /**
  42. * 关流
  43. */
  44. public static void close(OutputStream os) {
  45. if (os != null) {
  46. try {
  47. os.close();
  48. os = null;
  49. } catch (Exception e2) {
  50. throw new RuntimeException("关闭流失败");
  51. }
  52. }
  53. }
  54. /**
  55. * 关流
  56. */
  57. public static void close(InputStream is, OutputStream os) {
  58. if (is != null) {
  59. try {
  60. is.close();
  61. is = null;
  62. } catch (Exception e2) {
  63. throw new RuntimeException("关闭流失败");
  64. }
  65. }
  66. if (os != null) {
  67. try {
  68. os.close();
  69. os = null;
  70. } catch (Exception e2) {
  71. throw new RuntimeException("关闭流失败");
  72. }
  73. }
  74. }
  75. }