Example usage for java.nio.channels FileChannel close

List of usage examples for java.nio.channels FileChannel close

Introduction

In this page you can find the example usage for java.nio.channels FileChannel close.

Prototype

public final void close() throws IOException 

Source Link

Document

Closes this channel.

Usage

From source file:info.evanchik.eclipse.karaf.core.KarafCorePluginUtils.java

/**
 * Copies the source file to the destination file
 *
 * @param src/*www  .ja  v a  2  s .c  om*/
 *            the source file
 * @param dst
 *            the destination file
 * @throws IOException
 *             if there is a problem during the file copy
 */
public static void copyFile(final File src, final File dst) throws IOException {
    if (!src.exists()) {
        throw new FileNotFoundException("File does not exist: " + src.getAbsolutePath());
    }

    if (!dst.exists()) {
        dst.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(src).getChannel();
        destination = new FileOutputStream(dst).getChannel();

        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }

        if (destination != null) {
            destination.close();
        }
    }
}

From source file:org.pmedv.core.util.FileUtils.java

/**
 * Copies a file//w  ww.ja va 2 s .  c  om
 * 
 * @param in Source file
 * @param out Destination file
 * @throws IOException
 */
public static void copyFile(File in, File out) throws IOException {

    int bufsize = 8192;
    int transferred = 0;

    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();

    try {
        while (transferred < inChannel.size()) {
            inChannel.transferTo(transferred, bufsize, outChannel);
            transferred += bufsize;
        }

    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:Main.java

/**
 * This method handels the reading of data from a specific file.
 * /*from   w  w w .j  a va  2  s  .com*/
 * @param file the file, to read from.
 * @param blockSize the length of the data-block, which should be read from the specified file.
 * @return the data read from the file.
 */
public static byte[] readFile(File file, long blockSize) {
    FileInputStream fis;
    byte[] fileContent = null;

    try {
        fis = new FileInputStream(file);

        FileChannel fileChannel = fis.getChannel();

        int bytesToRead;
        fileChannel.position(readStreamPosition);

        int dataLen = fis.available();

        // if there is a zero block size specified, read the whole file
        if (blockSize == 0L) {
            bytesToRead = dataLen;
        } else {
            bytesToRead = (int) blockSize;
        }

        fileContent = new byte[bytesToRead];

        // reading the data
        for (int i = 0; i < bytesToRead; i++) {
            fileContent[i] = (byte) fis.read();
        }

        // storing read-position
        readStreamPosition = fileChannel.position();

        fis.close();
        fileChannel.close();

        // zero blockSize indicates, that reading of this file is completed,
        // stream position reset
        if (blockSize == 0L) {
            readStreamPosition = 0L;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return fileContent;
}

From source file:Main.java

public static String readString(String filePath) {
    File file = new File(filePath);
    if (!file.exists())
        return null;

    FileInputStream fileInput = null;
    FileChannel channel = null;
    try {//from w w w  .j a  va  2  s .  co m
        fileInput = new FileInputStream(filePath);
        channel = fileInput.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
        channel.read(buffer);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(buffer.array());
        return byteArrayOutputStream.toString();
    } catch (Exception e) {
    } finally {

        if (fileInput != null) {
            try {
                fileInput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:com.opendesign.utils.CmnUtil.java

/**
 * ? //w ww  . ja  v a 2s .c om
 * 
 * @param oldFilePath
 * @param newFilePath
 */
public static void fileCopy(String oldFilePath, String newFilePath) {
    File oldFile = new File(oldFilePath);
    File newFile = new File(newFilePath);
    try {
        FileInputStream inputStream = new FileInputStream(oldFile);
        FileOutputStream outputStream = new FileOutputStream(newFile);
        FileChannel fcin = inputStream.getChannel();
        FileChannel fcout = outputStream.getChannel();

        long size = fcin.size();
        fcin.transferTo(0, size, fcout);

        fcout.close();
        fcin.close();
        outputStream.close();
        inputStream.close();
    } catch (Exception e) {
    }
}

From source file:com.opendesign.utils.CmnUtil.java

/**
 * ? /*from   w  ww.  ja  v a 2s  .c om*/
 * 
 * @param oldFilePath
 * @param oldFileName
 * @param newFilePath
 * @param newFileName
 */
public static void fileCopy(String oldFilePath, String oldFileName, String newFilePath, String newFileName) {
    File oldFile = new File(oldFilePath, oldFileName);
    File newFile = new File(newFilePath, newFileName);
    try {
        FileInputStream inputStream = new FileInputStream(oldFile);
        FileOutputStream outputStream = new FileOutputStream(newFile);
        FileChannel fcin = inputStream.getChannel();
        FileChannel fcout = outputStream.getChannel();

        long size = fcin.size();
        fcin.transferTo(0, size, fcout);

        fcout.close();
        fcin.close();
        outputStream.close();
        inputStream.close();
    } catch (Exception e) {
    }
}

From source file:org.bibsonomy.lucene.util.generator.LuceneGenerateResourceIndex.java

/**
 * Fast & simple file copy.//from www . j a va 2  s  . co  m
 * 
 * @param source 
 * @param dest 
 * @throws IOException 
 */
public static void copyFile(final File source, final File dest) throws IOException {
    FileChannel in = null, out = null;
    try {
        in = new FileInputStream(source).getChannel();
        out = new FileOutputStream(dest).getChannel();

        final long size = in.size();
        final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);

        out.write(buf);

    } finally {
        if (in != null)
            in.close();
        if (out != null)
            out.close();
    }
}

From source file:com.evolveum.midpoint.util.MiscUtil.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();/*from   w  w  w. j a  va 2s  . c o m*/
    }

    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:org.solmix.commons.util.Files.java

/**
 * ?//from ww w  .  j  a  v a2  s.  co m
 *
 * @param f1
 *            ?
 * @param f2
 *            
 * @throws Exception
 */
public static void copyFile(File f1, File f2) throws Exception {
    int length = 2097152;
    FileInputStream in = new FileInputStream(f1);
    FileOutputStream out = new FileOutputStream(f2);
    FileChannel inC = in.getChannel();
    FileChannel outC = out.getChannel();
    ByteBuffer b = null;
    while (true) {
        if (inC.position() == inC.size()) {
            inC.close();
            outC.close();
        }
        if ((inC.size() - inC.position()) < length) {
            length = (int) (inC.size() - inC.position());
        } else
            length = 2097152;
        b = ByteBuffer.allocateDirect(length);
        inC.read(b);
        b.flip();
        outC.write(b);
        outC.force(false);
    }
}

From source file:com.github.feribg.audiogetter.helpers.Utils.java

/**
 * Copy a file with the option to overwrite existing files
 *
 * @param src/*from  w  w  w  . j  ava 2s. c o  m*/
 * @param dst
 * @param overwrite
 * @throws IOException
 */
public static void copyFile(File src, File dst, Boolean overwrite) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    if (!dst.exists() || (dst.exists() && overwrite)) {
        try {
            if (dst.exists()) {
                Log.d(App.TAG, "copyFile: destination exists but overwriting");
            }
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    } else {
        Log.e(App.TAG, "copyFile: destination already exists:" + dst.getAbsolutePath());
        throw new IOException("copyFile: destination already exists:" + dst.getAbsolutePath());
    }
}