Java IO 文件拷贝功能的实现(2)

/**
    * 定义一个方法来实现拷贝文件的功能
    *
    * @param src 源文件路径
    * @param target 目标文件路径
    * @throws IOException
    */

public static void CopyFile(File src, File target) throws IOException{
        /**
        * 验证源文件是否存在
        */
        if(!src.exists()){
            System.err.println("There is no such a file !!!");
            System.exit(1);
        }
        /**
        * 验证目标路径是否存在,不存在的话创建父路径
        */
        if(!target.getParentFile().exists()){
            target.getParentFile().mkdirs();
        }

int temp = 0; 

byte data[] = new byte[1024];  //每次读取1024字节

InputStream input = new FileInputStream(src);
        OutputStream output = new FileOutputStream(target);

while((temp = input.read(data)) != -1){
            output.write(data,0,temp);
        }

input.close();
        output.close();
    }

public static void main(String[] args) {
        String in = "C:\\Users\\Administrator\\Desktop\\tmp.txt";
        String out = "C:\\Users\\Administrator\\Desktop\\tmp_copy.txt";

File src = new File(in);
        File target = new File(out);

try {

long start = System.currentTimeMillis();
            CopyFile(src, target);
            long end = System.currentTimeMillis();

System.out.println("Copy Successfully! \nAnd it costs us " + (end - start) + " milliseconds.");
        } catch (IOException e) {
            System.err.println("ERROR: Something wrong while copying !");
            e.printStackTrace();
        }
    }
}

很明显可以看出不同,这里我们定义了一个1024字节的数组,这个数组的大小由自己来决定,其实这就是与第一种方法的结合。

这里我们用到 read() 和 write() 两个方法也跟之前不同了,是带参数的。

read( byte b[] ) 源码如下:

/**
    * Reads some number of bytes from the input stream and stores them into
    * the buffer array <code>b</code>. The number of bytes actually read is
    * returned as an integer.  This method blocks until input data is
    * available, end of file is detected, or an exception is thrown.
    *
    * <p> If the length of <code>b</code> is zero, then no bytes are read and
    * <code>0</code> is returned; otherwise, there is an attempt to read at
    * least one byte. If no byte is available because the stream is at the
    * end of the file, the value <code>-1</code> is returned; otherwise, at
    * least one byte is read and stored into <code>b</code>.
    *
    * <p> The first byte read is stored into element <code>b[0]</code>, the
    * next one into <code>b[1]</code>, and so on. The number of bytes read is,
    * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
    * number of bytes actually read; these bytes will be stored in elements
    * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
    * leaving elements <code>b[</code><i>k</i><code>]</code> through
    * <code>b[b.length-1]</code> unaffected.
    *
    * <p> The <code>read(b)</code> method for class <code>InputStream</code>
    * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
    *
    * @param      b  the buffer into which the data is read.
    * @return    the total number of bytes read into the buffer, or
    *            <code>-1</code> if there is no more data because the end of
    *            the stream has been reached.
    * @exception  IOException  If the first byte cannot be read for any reason
    * other than the end of the file, if the input stream has been closed, or
    * if some other I/O error occurs.
    * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
    * @see        java.io.InputStream#read(byte[], int, int)
    */
    public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/08c8c9bc49a7c38c0d1602da8ec8d0a6.html