Commons-io-1.4.jarパッケージ、IOUtilsの使用



Commons Io 1 4 Jar Package



ファイルコピー用のIOストリーム

方法1:



public static void main(String[] args) throws IOException { File file = new File('F:/B/test.txt') if(!file.exists()){ / / Create a new empty file file.createNewFile() } FileOutputStream out = new FileOutputStream(file) FileInputStream in = new FileInputStream(new File('F:/a.txt')) byte[] arr = new byte[1024] int len = 0 while((len=in.read(arr))!=-1){ out.write(arr,0,len) } in.close() out.close() }

方法2:IOUtils

public static void main(String[] args) throws IOException { File file = new File('F:/B/test.txt') if(!file.exists()){ / / Create a new empty file file.createNewFile() } FileOutputStream out = new FileOutputStream(file) FileInputStream in = new FileInputStream(new File('F:/a.txt')) / / Output the data of the input stream to the output stream IOUtils.copy(in, out) / / Close the IO stream IOUtils.closeQuietly(in) IOUtils.closeQuietly(out) }