Java Path File Write nio overwriteFile(final String newFilePath, final String oldFilePath)

Here you can find the source of overwriteFile(final String newFilePath, final String oldFilePath)

Description

Overwrite the destination file with the source file

License

Open Source License

Parameter

Parameter Description
newFilePath the source file to use
oldFilePath the destination file to overwrite with the contents of the source file

Exception

Parameter Description
IOException if there is an error opening either of the files or copying the source file'scontents into the destination file

Declaration

public static void overwriteFile(final String newFilePath, final String oldFilePath) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /** Overwrite the destination file with the source file
     * @param newFilePath the source file to use
     * @param oldFilePath the destination file to overwrite with the contents of the source file
     * @throws IOException if there is an error opening either of the files or copying the source file's
     * contents into the destination file
     *//*from  w w w.  j av a  2 s  .c  o  m*/
    public static void overwriteFile(final String newFilePath, final String oldFilePath) throws IOException {
        FileChannel src = null;
        FileChannel dest = null;
        try {
            File oldFile = new File(oldFilePath).getCanonicalFile();
            File newFile = new File(newFilePath).getCanonicalFile();
            //System.out.println("Overwriting: " + oldFile.getAbsolutePath() + ", with: " + newFile.getAbsolutePath());
            if (!oldFile.isFile()) {
                throw new IOException("old file path does not exist (" + oldFilePath + ")");
            }
            if (!newFile.isFile()) {
                throw new IOException("new file path does not exist (" + newFilePath + ")");
            }
            src = new FileInputStream(newFile).getChannel();
            dest = new FileOutputStream(oldFile).getChannel();
            long transferCount = 0;
            long size = src.size();
            do {
                transferCount += dest.transferFrom(src, transferCount, size - transferCount);
            } while (transferCount < size);
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (src != null) {
                    src.close();
                }
                if (dest != null) {
                    dest.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. isWritePermission(Path p)
  2. newBufferedWriter(Path path)
  3. newBufferedWriter(Path path, OpenOption... options)
  4. newGzipBufferedWriter(Path path)
  5. okayToOverwrite(Path file)
  6. removeMatchingLines( final Map d1LineMap, final Path tableDir, final String table, final BufferedWriter errors)
  7. robustCheckWriteable(Path logFile)
  8. save(Object o, String path)
  9. saveFile(String path, String text)