Example usage for java.nio.channels FileChannel write

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

Introduction

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

Prototype

public final long write(ByteBuffer[] srcs) throws IOException 

Source Link

Document

Writes a sequence of bytes to this channel from the given buffers.

Usage

From source file:Main.java

public static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException {
    FileOutputStream fileOutputStream = null;
    FileChannel fileChannel = null;
    try {/* w  w  w  . j a va 2s  .  co m*/
        fileOutputStream = new FileOutputStream(file);
        fileChannel = fileOutputStream.getChannel();
        byte[] bArr = new byte[4096];
        while (true) {
            int read = inputStream.read(bArr);
            if (read <= 0) {
                break;
            }
            fileChannel.write(ByteBuffer.wrap(bArr, SYSTEM_ROOT_STATE_DISABLE, read));
        }

    } catch (Throwable throwable) {
        throwable.printStackTrace();
    } finally {

        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (fileChannel != null) {
            try {
                fileChannel.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (Exception e22) {
                e22.printStackTrace();
            }
        }
    }
}

From source file:ja.centre.util.io.Files.java

public static void copy(String source, String destination) throws IOException {
    FileInputStream fis = null;/*from   www . j  a  v  a  2  s . co m*/
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(destination);

        FileChannel fic = null;
        FileChannel foc = null;
        try {
            fic = fis.getChannel();
            foc = fos.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(COPY_BUFFER_SIZE);
            do {
                buffer.flip();
                foc.write(buffer);
                buffer.clear();
            } while (fic.read(buffer) != -1);
        } finally {
            closeQuietly(fic);
            closeQuietly(foc);
        }
    } finally {
        closeQuietly(fis);
        closeQuietly(fos);
    }
}

From source file:MainClass.java

private static void test() throws Exception {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/test/primes.txt");
    FileOutputStream outputFile = null;
    outputFile = new FileOutputStream(aFile);
    FileChannel file = outputFile.getChannel();
    ByteBuffer[] buffers = new ByteBuffer[3];
    buffers[0] = ByteBuffer.allocate(8);
    buffers[2] = ByteBuffer.allocate(8);
    String primeStr = null;/*from w ww  . j  a v  a  2 s.  co m*/
    for (long prime : primes) {
        primeStr = "prime = " + prime;
        buffers[0].putDouble((double) primeStr.length()).flip();
        buffers[1] = ByteBuffer.allocate(primeStr.length());
        buffers[1].put(primeStr.getBytes()).flip();
        buffers[2].putLong(prime).flip();
        file.write(buffers);
        buffers[0].clear();
        buffers[2].clear();
    }
    System.out.println("File written is " + file.size() + "bytes.");
    outputFile.close();
}

From source file:Main.java

public static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException {
    FileOutputStream fileOutputStream = null;
    Throwable th;//from  w  w w. j  a  va  2 s  . co  m
    FileChannel fileChannel = null;
    try {
        fileOutputStream = new FileOutputStream(file);
        try {
            fileChannel = fileOutputStream.getChannel();
            byte[] bArr = new byte[4096];
            while (true) {
                int read = inputStream.read(bArr);
                if (read <= 0) {
                    break;
                }
                fileChannel.write(ByteBuffer.wrap(bArr, SYSTEM_ROOT_STATE_DISABLE, read));
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (fileChannel != null) {
                try {
                    fileChannel.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (Exception e22) {
                    e22.printStackTrace();
                }
            }
        } catch (Throwable th2) {
            th = th2;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e3) {
                    e3.printStackTrace();
                }
            }
            if (fileChannel != null) {
                try {
                    fileChannel.close();
                } catch (Exception e4) {
                    e4.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (Exception e42) {
                    e42.printStackTrace();
                }
            }
            throw th;
        }
    } catch (Throwable th3) {
        th = th3;
        Object obj = fileChannel;
        if (inputStream != null) {
            inputStream.close();
        }
        if (fileChannel != null) {
            fileChannel.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }

    }
}

From source file:cn.com.sunjiesh.wechat.handler.WechatBaseHandler.java

/**
 * ?//from w  w w .jav  a2 s .  c o  m
 *
 * @param httpResponse httpResponse
 * @param fileTpye fileTpye
 * @return File
 * @throws ServiceException ServiceException
 */
public static File getFileResponseFromHttpResponse(HttpResponse httpResponse, String fileTpye)
        throws ServiceException {
    File file = null;

    try {
        StatusLine httpStatusLine = httpResponse.getStatusLine();
        HttpEntity httpEntity = httpResponse.getEntity();
        int statusCode = httpStatusLine.getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            LOGGER.error(DOWNLOAD_FILE_ERROR);
            throw new ServiceException(DOWNLOAD_FILE_ERROR);
        }

        String fileName = UUID.randomUUID().toString();
        fileName = fileName + "." + fileTpye;
        InputStream is = httpEntity.getContent();
        if (!StringUtils.isEmpty(fileName)) {
            //??
            file = new File("/var/tmp", fileName);
            if (file.exists()) {
                file.delete();
            }
            FileOutputStream fos = new FileOutputStream(file, false);
            FileChannel fileChannel = fos.getChannel();
            ByteBuffer byteBuffer = null;
            byte[] tempBytes = new byte[4096];
            while (is.read(tempBytes) > 0) {
                byteBuffer = ByteBuffer.wrap(tempBytes);
                fileChannel.write(byteBuffer);
            }
            if (fos != null) {
                fos.close();
            }
            fileChannel.close();
        }
    } catch (IllegalStateException e) {
        LOGGER.error(DOWNLOAD_FILE_ERROR, e);
        throw new ServiceException(DOWNLOAD_FILE_ERROR, e);
    } catch (FileNotFoundException e) {
        LOGGER.error(DOWNLOAD_FILE_ERROR, e);
        throw new ServiceException(DOWNLOAD_FILE_ERROR, e);
    } catch (IOException e) {
        LOGGER.error(DOWNLOAD_FILE_ERROR, e);
        throw new ServiceException(DOWNLOAD_FILE_ERROR, e);
    }

    return file;
}

From source file:org.neo4j.io.fs.FileUtils.java

public static void writeAll(FileChannel channel, ByteBuffer src) throws IOException {
    long bytesToWrite = src.limit() - src.position();
    int bytesWritten;
    while ((bytesToWrite -= (bytesWritten = channel.write(src))) > 0) {
        if (bytesWritten <= 0) {
            throw new IOException("Unable to write to disk, reported bytes written was " + bytesWritten);
        }/* w  w  w.  j av a2 s. c o m*/
    }
}

From source file:com.log4ic.compressor.utils.FileUtils.java

/**
 * ?/*w ww  .  j  a va 2  s . c  om*/
 *
 * @param content
 * @param filePath
 * @return
 */
public static File writeFile(byte[] content, String filePath) {
    FileOutputStream out = null;
    FileChannel outChannel = null;
    File file = new File(filePath);

    if (file.exists()) {
        file.delete();
    }

    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }

    ByteBuffer outBuffer = ByteBuffer.allocate(content.length);
    outBuffer.put(content);
    outBuffer.flip();
    try {
        out = new FileOutputStream(file);

        outChannel = out.getChannel();

        outChannel.write(outBuffer);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (outChannel != null) {
            try {
                outChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return file.exists() ? file : null;
}

From source file:Main.java

public static boolean fileCopy(File srcFile, File dstFile) {
    int length = 1048891;
    FileChannel inC = null;/*from  w ww .  ja v a 2  s  .  c om*/
    FileChannel outC = null;
    try {
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(dstFile);
        inC = in.getChannel();
        outC = out.getChannel();
        ByteBuffer b = null;
        while (inC.position() < inC.size()) {
            if ((inC.size() - inC.position()) < length) {
                length = (int) (inC.size() - inC.position());
            } else {
                length = 1048891;
            }
            b = ByteBuffer.allocateDirect(length);
            inC.read(b);
            b.flip();
            outC.write(b);
            outC.force(false);
        }

        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (inC != null && inC.isOpen()) {
                inC.close();
            }
            if (outC != null && outC.isOpen()) {
                outC.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:net.filterlogic.util.imaging.ToTIFF.java

private static String bigEndian2LittleEndian(String fileName) throws FileNotFoundException, IOException {

    //import java.nio.ByteBuffer;
    //import java.nio.ByteOrder;
    //import java.nio.channels.FileChannel;
    //import java.io.FileOutputStream;

    byte[] imgData = loadFileToByteArray(fileName);
    String newFileName = fileName + ".be2le.tif";

    ByteBuffer buffer = ByteBuffer.allocate(imgData.length);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(imgData);// w w w  .  j a v a 2s  .  c o m
    FileChannel out = new FileOutputStream(newFileName).getChannel();
    out.write(buffer);
    out.close();

    return newFileName;
}

From source file:com.ah.ui.actions.home.clientManagement.service.CertificateGenSV.java

public static void writeFile(String pathName, byte[] bytes) throws IOException {
    File fl = new File(pathName);
    if (existFile(pathName)) {
        fl.delete();// w  w  w  . ja va  2s  .co  m
    }
    ByteBuffer bb = ByteBuffer.allocate(bytes.length);
    bb.put(bytes);
    bb.flip();
    FileChannel fileChannel = null;
    try {
        fileChannel = new FileOutputStream(pathName, false).getChannel();
        fileChannel.write(bb);
    } finally {
        if (fileChannel != null) {
            try {
                fileChannel.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}