一定要注意:
传入的参数,应该是包含文件名的完整路径名,不能把一个文件复制到【文件夹】中,因为【文件夹】本身是不能有输入输出流的,只能复制到一个【文件】中,否则会报异常。
以字节流读写的三种方式
public class Test {private static final String FILE_PATH = "e:\\";private static final String FILE_TYPE = ".exe";private static final String FILE_FROM = FILE_PATH + 0 + FILE_TYPE;private static final String COPY_FILE_1 = FILE_PATH + 1 + FILE_TYPE;private static final String COPY_FILE_2 = FILE_PATH + 2 + FILE_TYPE;private static final String COPY_FILE_3 = FILE_PATH + 3 + FILE_TYPE;public static void main(String[] args) throws IOException {copyByBufStream(FILE_FROM, COPY_FILE_1);copyByBufArray(FILE_FROM, COPY_FILE_2);copyByByte(FILE_FROM, COPY_FILE_3);}/*** 利用缓冲输入流读取到一个缓冲容器后再写入。建议使用*/public static boolean copyByBufStream(String filePathFrom, String filePathTo) {try {BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(filePathFrom));BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(filePathTo));int ch = 0;while ((ch = bufis.read()) != -1) {bufos.write(ch);}bufos.close();bufis.close();return true;} catch (FileNotFoundException e) {e.printStackTrace();return false;} catch (IOException e) {e.printStackTrace();return false;}}/*** 每次读取一个指定长度数组的字节。建议使用*/public static boolean copyByBufArray(String filePathFrom, String filePathTo) {try {FileInputStream fis = new FileInputStream(filePathFrom);FileOutputStream fos = new FileOutputStream(filePathTo);byte[] buf = new byte[1024];int len = 0;while ((len = fis.read(buf)) != -1) {fos.write(buf, 0, len);}fos.close();fis.close();return true;} catch (FileNotFoundException e) {e.printStackTrace();return false;} catch (IOException e) {e.printStackTrace();return false;}}/*** 一次读取一个字节。千万不要用,速度超级慢!*/public static boolean copyByByte(String filePathFrom, String filePathTo) {try {FileInputStream fis = new FileInputStream(filePathFrom);FileOutputStream fos = new FileOutputStream(filePathTo);int ch = 0;while ((ch = fis.read()) != -1) {fos.write(ch);}fos.close();fis.close();return true;} catch (FileNotFoundException e) {e.printStackTrace();return false;} catch (IOException e) {e.printStackTrace();return false;}}}
以字符流读写的三种方式
public class Test {private static final String FILE_PATH = "e:\\";private static final String FILE_TYPE = ".txt";private static final String FILE_FROM = FILE_PATH + 0 + FILE_TYPE;private static final String COPY_FILE_1 = FILE_PATH + 1 + FILE_TYPE;private static final String COPY_FILE_2 = FILE_PATH + 2 + FILE_TYPE;private static final String COPY_FILE_3 = FILE_PATH + 3 + FILE_TYPE;public static void main(String[] args) throws IOException {//注意,只能复制纯文本格式的文件,否则就会出现乱码copyByBufLine(FILE_FROM, COPY_FILE_1);copyByBufArray(FILE_FROM, COPY_FILE_2);copyByChar(FILE_FROM, COPY_FILE_3);}/*** 一次写入一行字符*/public static boolean copyByBufLine(String filePathFrom, String filePathTo) {try {BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));String line = null;//另外开辟一个缓冲区,存储读取的一行数据,返回包含该行内容的字符串,不包含换行符,如果已到达流末尾,则返回【 null】while ((line = bufr.readLine()) != null) {bufw.write(line);bufw.newLine();// 写入一个行分隔符bufw.flush();}bufr.close();bufw.close();return true;} catch (FileNotFoundException e) {e.printStackTrace();return false;} catch (IOException e) {e.printStackTrace();return false;}}/*** 一次写入指定个数的字符*/public static boolean copyByBufArray(String filePathFrom, String filePathTo) {try {BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));char[] buf = new char[1024];int len = 0;while ((len = bufr.read(buf)) != -1) {bufw.write(buf, 0, len);bufw.flush();len = 0;}bufr.close();bufw.close();return true;} catch (FileNotFoundException e) {e.printStackTrace();return false;} catch (IOException e) {e.printStackTrace();return false;}}/*** 一次写入一个字符*/public static boolean copyByChar(String filePathFrom, String filePathTo) {try {BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));int ch = 0;while ((ch = bufr.read()) != -1) {bufw.write(ch);//写入单个字符}bufr.close();bufw.close();return true;} catch (FileNotFoundException e) {e.printStackTrace();return false;} catch (IOException e) {e.printStackTrace();return false;}}}