java file operation one

file copy

byte stream:

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("Please enter the source file path");
        String srcPath = sc.next();
        System.out.println("Please enter the source file path is: " + srcPath);
        System.out.println("Please enter the target file path");
        String destPath = sc.next();
        System.out.println("Please enter the destination file path is: " + destPath);
        try {
            copyFile(srcPath, destPath);
            Logger.getLogger("finished", "copy");
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }

    /**
     * Use byte stream to copy files (byte stream can process all data)
     *
     * @param srcPath : source file path
     * @param destPath : destination file path
     * @throws Exception
     */
    public static void copyFile(String srcPath, String destPath) throws Exception {
        // Build the File object of the source and target files
        File src = new File(srcPath);
        File dest = new File(destPath);
        //If the source file does not exist, throw an exception
        if (!src.exists()) {
            throw new IOException("File does not exist!");
        }
        //If the parent path of the target file does not exist, create the parent path
        if (!dest.getParentFile().exists()) {
            dest.getParentFile (). mkdirs ();
        }
        //Instantiate input and output streams
        InputStream is = new FileInputStream(src);
        OutputStream os = new FileOutputStream(dest);
        //Define a buffered byte array to receive the read content
        byte buf[] = new byte[1024];
        int len ​​= 0;
        while ((len = is.read(buf)) != -1) {
            os.write(buf, 0, len);
            os.flush();
        }
        //close the stream
        os.close();
        is.close();
    }
}

character stream:

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 {

            /**
             * An error was encountered here regarding the file path
             * The .txt suffix of a.txt file under window is not available by default
             * */
            f.copy("D:\\File\\1.txt", "D:\\File\\2.txt");
            System.out.println("copy end");
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }

    /**
     * @param f1 source file path
     * @param f2 target file path
     * @function implements file character stream copy
     */
    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();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325566898&siteId=291194637