Example usage for java.io FileInputStream getChannel

List of usage examples for java.io FileInputStream getChannel

Introduction

In this page you can find the example usage for java.io FileInputStream getChannel.

Prototype

public FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file input stream.

Usage

From source file:Main.java

public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
    FileChannel fromChannel = null;
    FileChannel toChannel = null;
    try {//from   ww  w  .  j  av  a2 s  . c  o  m
        fromChannel = fromFile.getChannel();
        toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        try {
            if (fromChannel != null) {
                fromChannel.close();
            }
        } finally {
            if (toChannel != null) {
                toChannel.close();
            }
        }
    }
}

From source file:Main.java

private static void copyFile(File src, File dst) throws IOException {
    FileInputStream in = new FileInputStream(src);
    FileOutputStream out = new FileOutputStream(dst);
    FileChannel fromChannel = null, toChannel = null;
    try {/*from   w ww  . ja v a  2  s .  c o  m*/
        fromChannel = in.getChannel();
        toChannel = out.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        if (fromChannel != null)
            fromChannel.close();
        if (toChannel != null)
            toChannel.close();
    }
}

From source file:Main.java

/** Copia un fichero.
 * @param source Fichero origen con el contenido que queremos copiar.
 * @param dest Fichero destino de los datos.
 * @throws IOException SI ocurre algun problema durante la copia */
public static void copyFile(final File source, final File dest) throws IOException {
    if (source == null || dest == null) {
        throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$
    }/*from   w  w  w .ja va 2  s  .c  om*/

    final FileInputStream is = new FileInputStream(source);
    final FileOutputStream os = new FileOutputStream(dest);
    final FileChannel in = is.getChannel();
    final FileChannel out = os.getChannel();
    final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
    out.write(buf);

    in.close();
    out.close();
    is.close();
    os.close();

}

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;//w w w.ja  v  a2s .c om
    try {
        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.bellman.bible.service.common.FileManager.java

public static boolean copyFile(File fromFile, File toFile) {
    boolean ok = false;
    try {//  ww  w. j ava  2s.  c o  m
        // don't worry if tofile exists, allow overwrite
        if (fromFile.exists()) {
            //ensure the target dir exists or FileNotFoundException is thrown creating dst FileChannel
            File toDir = toFile.getParentFile();
            toDir.mkdir();

            long fromFileSize = fromFile.length();
            log.debug("Source file length:" + fromFileSize);
            if (fromFileSize > CommonUtils.getFreeSpace(toDir.getPath())) {
                // not enough room on SDcard
                ok = false;
            } else {
                // move the file
                FileInputStream srcStream = new FileInputStream(fromFile);
                FileChannel src = srcStream.getChannel();
                FileOutputStream dstStream = new FileOutputStream(toFile);
                FileChannel dst = dstStream.getChannel();
                try {
                    dst.transferFrom(src, 0, src.size());
                    ok = true;
                } finally {
                    src.close();
                    dst.close();
                    srcStream.close();
                    dstStream.close();
                }
            }
        } else {
            // fromfile does not exist
            ok = false;
        }
    } catch (Exception e) {
        log.error("Error moving file to sd card", e);
    }
    return ok;
}

From source file:com.twitter.ambrose.util.JSONUtil.java

public static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {/*from   w  w w. j a  va  2 s. c  o  m*/
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:edu.isi.webserver.FileUtil.java

public static String readFileContentsToString(File file) throws IOException {
    FileInputStream stream = new FileInputStream(file);
    try {/*from ww w.  j a  v  a  2s .  c o m*/
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:org.springframework.hateoas.VndErrorsMarshallingTest.java

private static String readFile(org.springframework.core.io.Resource resource) throws IOException {

    FileInputStream stream = new FileInputStream(resource.getFile());

    try {// w w  w. j ava  2 s. c o m
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:Grep.java

public static void setFile(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    FileChannel fc = fis.getChannel();

    // Get the file's size and then map it into memory
    int sz = (int) fc.size();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
    // Decode the file into a char buffer
    indexFile = decoder.decode(bb);//from   w  w w. ja v a2  s . c  o m

    fc.close();
}

From source file:thv.th.reader.FramesReader.java

public static THFrame readByIndex(int frameIndex, FileInputStream frameStream, FileInputStream listStream,
        FileInputStream elementStream, File tabFile, File chunksFile, THPalette palette) throws IOException {
    frameStream.getChannel().position(frameIndex * 10);

    int listIndex = EndianUtils.readSwappedInteger(frameStream) * 2;
    int width = frameStream.read();
    int height = frameStream.read();

    if (width == 0 || height == 0)
        return null;

    frameStream.skip(1);//w w  w.  j a v a  2s.  c  o m
    int flags = frameStream.read();
    int nextFrame = EndianUtils.readSwappedShort(frameStream);

    THFrame res = new THFrame(frameIndex, width, height, flags, nextFrame, chunksFile, tabFile, palette);

    Vector<Integer> elementList = new Vector<Integer>();

    listStream.getChannel().position(listIndex);

    while (true) {
        int elementIndex = EndianUtils.readSwappedShort(listStream);

        if (elementIndex == -1)
            break;

        SpriteElement element = SpriteElementReader.readByIndex(elementStream, elementIndex);
        res.addElement(element);
    }

    return res;
}