文件复制Util写法,可以适用于多种条件

代码写法:

  1 import java.io.BufferedInputStream;
  2 import java.io.BufferedOutputStream;
  3 import java.io.ByteArrayInputStream;
  4 import java.io.ByteArrayOutputStream;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileOutputStream;
  8 import java.io.IOException;
  9 import java.io.InputStream;
 10 import java.io.OutputStream;
 11 import java.io.Reader;
 12 import java.io.StringWriter;
 13 import java.io.Writer;
 14 
 15 /**
 16  * 用于文件和流复制的简单实用方法。所有复制方法使用4096字节的块大小,并在完成后关闭所有受影响的流。
 17  */
 18 public abstract class FileCopyUtils {
 19 
 20     public static final int BUFFER_SIZE = 4096;
 21     /**
 22      * 将给定输入文件的内容复制到给定的输出文件。
 23      * @param    要复制的文件
 24      * @param     将文件复制到
 25      * @return     复制的字节数
 26      * @throws     I / O错误时发生IOException
 27      */
 28     public static int copy(File in, File out) throws IOException {
 29         return copy(new BufferedInputStream(new FileInputStream(in)),
 30             new BufferedOutputStream(new FileOutputStream(out)));
 31     }
 32 
 33     /**
 34      * 将给定字节数组的内容复制到给定的输出文件。
 35      * @param     要从中复制的字节数组
 36      * @param    将文件复制到
 37      * @throws     I / O错误时发生IOException
 38      */
 39     public static void copy(byte[] in, File out) throws IOException {
 40         ByteArrayInputStream inStream = new ByteArrayInputStream(in);
 41         OutputStream outStream = new BufferedOutputStream(new FileOutputStream(out));
 42         copy(inStream, outStream);
 43     }
 44 
 45     /**
 46      * 将给定输入文件的内容复制到新的字节数组中。
 47      * @param     要复制的文件
 48      * @return     已复制到的新字节数组
 49      * @throws     I / O错误时发生IOException
 50      */
 51     public static byte[] copyToByteArray(File in) throws IOException {
 52         return copyToByteArray(new BufferedInputStream(new FileInputStream(in)));
 53     }
 54     /**
 55      * 将给定InputStream的内容复制到给定的OutputStream。完成后关闭两个流。
 56      * @param     复制到
 57      * @return     复制的字节数
 58      * @throws     I / O错误时发生IOException
 59      */
 60     public static int copy(InputStream in, OutputStream out) throws IOException {
 61         try {
 62             int byteCount = 0;
 63             byte[] buffer = new byte[BUFFER_SIZE];
 64             int bytesRead = -1;
 65             while ((bytesRead = in.read(buffer)) != -1) {
 66                 out.write(buffer, 0, bytesRead);
 67                 byteCount += bytesRead;
 68             }
 69             out.flush();
 70             return byteCount;
 71         }
 72         finally {
 73             try {
 74                 in.close();
 75             }
 76             catch (IOException ex) {
 77             }
 78             try {
 79                 out.close();
 80             }
 81             catch (IOException ex) {
 82             }
 83         }
 84     }
 85 
 86     /**
 87      * 将给定字节数组的内容复制到给定的OutputStream。完成后关闭流。
 88      * @param     要复制的字节数组
 89      * @param     输出要复制到的输出流
 90      * @throws     I / O错误时发生IOException
 91      */
 92     public static void copy(byte[] in, OutputStream out) throws IOException {
 93         try {
 94             out.write(in);
 95         }
 96         finally {
 97             try {
 98                 out.close();
 99             }
100             catch (IOException ex) {
101             }
102         }
103     }
104 
105     /**
106      * 将给定输入流的内容复制到新的字节数组中,完成后关闭流。
107      * @return     已复制到的新字节数组
108      * @throws     I / O错误时发生IOException
109      */
110     public static byte[] copyToByteArray(InputStream in) throws IOException {
111         ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
112         copy(in, out);
113         return out.toByteArray();
114     }
115     
116     /*java.io.reader/java.io.writer的复制方法*/
117 
118     /**
119      * 将给定读者的内容复制到给定的作者,完成后都关闭。
120      */
121     public static int copy(Reader in, Writer out) throws IOException {
122         try {
123             int byteCount = 0;
124             char[] buffer = new char[BUFFER_SIZE];
125             int bytesRead = -1;
126             while ((bytesRead = in.read(buffer)) != -1) {
127                 out.write(buffer, 0, bytesRead);
128                 byteCount += bytesRead;
129             }
130             out.flush();
131             return byteCount;
132         }
133         finally {
134             try {
135                 in.close();
136             }
137             catch (IOException ex) {
138             }
139             try {
140                 out.close();
141             }
142             catch (IOException ex) {
143             }
144         }
145     }
146 
147     /**
148      * 将给定String的内容复制到给定的输出Writer。完成后关闭写入。
149      */
150     public static void copy(String in, Writer out) throws IOException {
151         try {
152             out.write(in);
153         }
154         finally {
155             try {
156                 out.close();
157             }
158             catch (IOException ex) {
159             }
160         }
161     }
162 
163     /**
164      * 将给定Reader的内容复制到String中。完成后关闭读取。
165      */
166     public static String copyToString(Reader in) throws IOException {
167         StringWriter out = new StringWriter();
168         copy(in, out);
169         return out.toString();
170     }
171 
172 }
原文地址:https://www.cnblogs.com/wangquanyi/p/11348054.html