如何使用java复制文件呢?
下文笔者讲述java代码复制文件的方法大全,如下所示
实现思路: 方式1:使用FileStream 方式2:使用FileChannel 方式3:使用ApacheCommonsIO 方式4:使用FileWriter对象复制文件FileReader读取文件例:
package com.java265; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.FileChannel; import java.nio.file.Files; import org.apache.commons.io.FileUtils; public class fileUtill { public static void main(String[] args) { File source = new File("D:/test/sourceFile.txt"); File dest = new File("D:/test/targetFile.txt"); try { copyFileUsingFileStreams(source,dest); copyFileUsingFileChannels(source,dest); copyFileUsingApacheCommonsIO(source,dest); copyFileUsingJava7Files(source,dest); } catch (IOException e) { e.printStackTrace(); } } //使用FileStreams复制 private static void copyFileUsingFileStreams(File source, File dest) throws IOException { InputStream input = null; OutputStream output = null; try { input = new FileInputStream(source); output = new FileOutputStream(dest); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) != -1) { output.write(buf, 0, bytesRead); } } finally { input.close(); output.close(); } } //使用FileInputStream/FileOutputStream字节流进行文件的复制操作 private static void copyFileUsingFileChannels(File source, File dest) throws IOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputChannel = new FileInputStream(source).getChannel(); outputChannel = new FileOutputStream(dest).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { inputChannel.close(); outputChannel.close(); } } //使用Commons IO复制 private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException { FileUtils.copyFile(source, dest); } //使用Java7的Files类复制 private static void copyFileUsingJava7Files(File source, File dest) throws IOException { Files.copy(source.toPath(), dest.toPath()); } /** * 利用字节流复制 * 使用BufferedInputStream/BufferedOutputStream高效字节流进行复制文件 */ public static void copy(String source,String dest) throws IOException{ InputStream is = new BufferedInputStream(new FileInputStream(source)); OutputStream os = new BufferedOutputStream(new FileOutputStream(dest)); //文件拷贝u,-- 循环+读取+写出 byte[] b = new byte[1024];//缓冲大小 int len = 0;//接收长度 //读取文件 while (-1!=(len = is.read(b))) { //读入多少,写出多少,直到读完为止。 os.write(b,0,len); } //强制刷出数据 os.flush(); //关闭流,先开后关 os.close(); is.close(); } /** * 字符流复制 * 使用BufferedReader/BufferedWriter高效字符流进行文件复制 * 注意这种方式只能复制只包含字符的文件 * 也就意味着你用记事本打开该文件你能够读懂 */ public static void copy1(String source,String dest) throws IOException{ //字符输入流 BufferedReader reader = new BufferedReader(new FileReader(source)); //字符输出流 BufferedWriter writer = new BufferedWriter(new FileWriter(dest)); char[] cbuf = new char[1024]; int len = 0; //边读入边写出 while ((len = reader.read(cbuf)) != -1) { writer.write(cbuf, 0, len); } //关闭流 writer.close(); reader.close(); } //使用FileReader/FileWriter字符流进行文件复制 //注意这种方式只能复制只包含字符的文件,也就意味着你用记事本打开该文件你能够读懂 private static void readerWriterCopyFile(File source, File dest) throws IOException { //使用字符流进行文件复制,注意:字符流只能复制只含有汉字的文件 FileReader fr = new FileReader(source); FileWriter fw = new FileWriter(dest); Integer by = 0; while((by = fr.read()) != -1) { fw.write(by); } fr.close(); fw.close(); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。