Here you can find the source of saveFile(final byte[] bytes, final File target)
Parameter | Description |
---|---|
bytes | a parameter |
target | a parameter |
public static void saveFile(final byte[] bytes, final File target)
//package com.java2s; /**//from w ww .jav a2 s.co m * This file is part of Atomic Tagging. * * Atomic Tagging 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 (at your option) any later * version. * * Atomic Tagging 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 Atomic Tagging. If not, see * <http://www.gnu.org/licenses/>. */ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * save an array of bytes * * @param bytes * @param target */ public static void saveFile(final byte[] bytes, final File target) { if (!target.exists()) { try { if (!target.createNewFile()) { throw new IllegalArgumentException( "Can't write to given target file: " + target.getAbsolutePath()); } } catch (final IOException e) { throw new IllegalArgumentException("Can't write to given target file: " + target.getAbsolutePath(), e); } } FileOutputStream fos = null; try { fos = new FileOutputStream(target); } catch (final FileNotFoundException ignore) { // Was checked previously. } if (fos == null) { throw new RuntimeException("Failed to create file streams."); } try { fos.write(bytes, 0, bytes.length); } catch (final IOException e) { // FIXME throw new RuntimeException("Failed to copy file.", e); } finally { try { fos.close(); } catch (final IOException e) { // Nothing we can do, or is there? e.printStackTrace(); } } } }