Here you can find the source of writeBytes(byte[] bytes, File file, boolean append)
Parameter | Description |
---|---|
append | specifies whether to append to an existing file or to overwrite its contents |
Parameter | Description |
---|---|
IllegalArgumentException | if bytes is null; file is null; file is a directory |
SecurityException | if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies write access to file |
IOException | if an I/O problem occurs |
public static void writeBytes(byte[] bytes, File file, boolean append) throws IllegalArgumentException, SecurityException, IOException
/*/*from w w w . j a v a 2s.c om*/ Copyright ? 2008 Brent Boyer This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 Lesser GNU General Public License for more details. You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project). If not, see <http://www.gnu.org/licenses/>. */ import bb.science.FormatUtil; import bb.util.Check; import bb.util.StringUtil; import bb.util.ThrowableUtil; import bb.util.logging.LogUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.util.Random; import java.util.logging.Level; import org.junit.Assert; import org.junit.Test; public class Main{ /** * Writes bytes into file. * All resources (e.g. OutputStreams) that are created as part of this process will be closed upon method return. * <p> * @param append specifies whether to append to an existing file or to overwrite its contents * @throws IllegalArgumentException if bytes is null; file is null; file is a directory * @throws SecurityException if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies write access to file * @throws IOException if an I/O problem occurs */ public static void writeBytes(byte[] bytes, File file, boolean append) throws IllegalArgumentException, SecurityException, IOException { Check.arg().notNull(bytes); Check.arg().notNull(file); if (file.isDirectory()) throw new IllegalArgumentException("file = " + file.getPath() + " refers to directory, not a normal text file"); //if (!file.canWrite()) throw new IllegalArgumentException("file = " + file.getPath() + " refers to a file that cannot be read by this application"); // DO NOT UNCOMMENT: it will fail if the file does not exist, which could be a common situation; instead let the write below fail OutputStream out = null; try { out = new FileOutputStream(file, append); out.write(bytes); } finally { StreamUtil.close(out); } } }