文件拷贝
字节流:
package com.ldd.second.wenjian; import sun.rmi.runtime.Log; import java.io.*; import java.util.Scanner; import java.util.logging.Logger; /** * @author ldd * @version 1.0.0 * @create 2018.5.4 * @decription InputStream * <p> * Constructors * Constructor and Description * * InputStream() * Method Summary * Methods * Modifier and Type Method and Description * * int available() * Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. * * void close() * Closes this input stream and releases any system resources associated with the stream. * void mark(int readlimit) * Marks the current position in this input stream. * boolean markSupported() * Tests if this input stream supports the mark and reset methods. * * abstract int read() * Reads the next byte of data from the input stream. * * int read(byte[] b) * Reads some number of bytes from the input stream and stores them into the buffer array b. * * int read(byte[] b, int off, int len) * Reads up to len bytes of data from the input stream into an array of bytes. * * void reset() * Repositions this stream to the position at the time the mark method was last called on this input stream. * * long skip(long n) * Skips over and discards n bytes of data from this input stream. * Methods inherited from class java.lang.Object * * clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait */ public class FileByteCopy { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入源文件路径"); String srcPath = sc.next(); System.out.println("请输入源文件路径是:" + srcPath); System.out.println("请输入目标文件路径"); String destPath = sc.next(); System.out.println("请输入目标文件路径是 :" + destPath); try { copyFile(srcPath, destPath); Logger.getLogger("finished", "copy"); } catch (Exception e) { e.printStackTrace(); } } /** * 利用字节流来实现文件的复制(字节流可以处理一切数据) * * @param srcPath : 源文件路径 * @param destPath : 目标文件路径 * @throws Exception */ public static void copyFile(String srcPath, String destPath) throws Exception { //构建源文件和目标文件的File对象 File src = new File(srcPath); File dest = new File(destPath); //如果源文件不存在,抛出异常 if (!src.exists()) { throw new IOException("文件不存在!"); } //如果目标文件父路径不存在,创建父路径 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } //实例化输入流和输出流 InputStream is = new FileInputStream(src); OutputStream os = new FileOutputStream(dest); //定义缓冲字节数组,用来接收读取的内容 byte buf[] = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { os.write(buf, 0, len); os.flush(); } //关闭流 os.close(); is.close(); } }
字符流:
package com.ldd.second.wenjian; import java.io.File; import java.io.FileReader; import java.io.FileWriter; /** * @author liudongdong19 * @create 2018.5.4 * <p> * Constructor and Description * <p> * FileReader(File file) * Creates a new FileReader, given the File to read from. * <p> * FileReader(FileDescriptor fd) * Creates a new FileReader, given the FileDescriptor to read from. * <p> * FileReader(String fileName) * Creates a new FileReader, given the name of the file to read from. * Method Summary * Methods inherited from class java.io.InputStreamReader * <p> * close, getEncoding, read, read, ready * Methods inherited from class java.io.Reader * mark, markSupported, read, read, reset, skip * <p> * Methods inherited from class java.lang.Object * <p> * clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait */ public class FileCharCopy { public static void main(String[] args) { FileCharCopy f = new FileCharCopy(); try { /** * 这里遇到一个错误关于文件路径 * window 下 a.txt 文件的.txt 后缀默认是没有的 * */ f.copy("D:\\File\\1.txt", "D:\\File\\2.txt"); System.out.println("copy end"); } catch (Exception e) { e.printStackTrace(); } } /** * @param f1 源文件路径 * @param f2 目标文件路径 * @function 实现文件字符流拷贝 */ public void copy(String f1, String f2) throws Exception { FileReader fr = new FileReader(new File(f1)); FileWriter fw = new FileWriter(new File(f2), false); char[] chars = new char[1024]; int len = fr.read(chars); while (len != -1) { fw.write(chars, 0, len); fw.flush(); len = fr.read(chars); } fr.close(); fw.close(); } }