Java Path File Content saveByteArrayToFile(byte[] content, Path targetPath)

Here you can find the source of saveByteArrayToFile(byte[] content, Path targetPath)

Description

save Byte Array To File

License

Open Source License

Declaration

public static void saveByteArrayToFile(byte[] content, Path targetPath) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w  w  w  .j a  v a  2 s .  co  m
?Developed with the contribution of the European Commission - Directorate General for Maritime Affairs and Fisheries
? European Union, 2015-2016.
    
This file is part of the Integrated Fisheries Data Management (IFDM) Suite. The IFDM Suite is free software: you can
redistribute it and/or modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or any later version. The IFDM Suite is distributed in
the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a
copy of the GNU General Public License along with the IFDM Suite. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

public class Main {
    public static void saveByteArrayToFile(byte[] content, Path targetPath) throws IOException {
        File file = targetPath.toFile();

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

        FileOutputStream fop = new FileOutputStream(file);

        InputStream is = new ByteArrayInputStream(content);
        byte[] buf = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buf)) != -1) {
            fop.write(buf, 0, bytesRead);
        }

        fop.close();
    }
}

Related

  1. getFileContents(final String absFilePath)
  2. getFileContents(Path file)
  3. getFileContents(String path)
  4. listUris(Path content)
  5. readContent(Path file)
  6. saveFile(String workspacePath, byte[] content)
  7. slurpFileContent(Path path)
  8. write2File(String contents, Path outputPath)
  9. WriteContentsToFile(String planeTextFilePath, byte[] encryptedData)