Here you can find the source of saveToFile(final String file, final byte[] data, final boolean overwrite)
Parameter | Description |
---|---|
file | file path |
data | data to save into a file |
overwrite | true if the existing file can be overwritten |
Parameter | Description |
---|---|
IOException | an exception |
public static void saveToFile(final String file, final byte[] data, final boolean overwrite) throws IOException
//package com.java2s; /**/*from w w w .jav a 2 s . c o m*/ * Copyright (c) 2014 Netflix, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * save byte array into a file * * @param file file path * @param data data to save into a file * @param overwrite true if the existing file can be overwritten * @throws IOException */ public static void saveToFile(final String file, final byte[] data, final boolean overwrite) throws IOException { assertNotNull(file, "file"); assertNotNull(data, "data"); final File f = new File(file); if (f.exists() && !overwrite) { throw new IllegalArgumentException("cannot overwrite file " + file); } FileOutputStream fos = null; try { fos = new FileOutputStream(f); fos.write(data); } finally { if (fos != null) try { fos.close(); } catch (final IOException ignore) { } } } /** * @param param parameter * @param name parameter name */ public static void assertNotNull(final Object param, final String name) { if (param == null) throw new IllegalArgumentException(String.format("NULL %s", (name != null) ? name : "parameter")); } }