Here you can find the source of getOutputStream(final File file)
Parameter | Description |
---|---|
file | the file. |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred. |
public static OutputStream getOutputStream(final File file) throws IOException
//package com.java2s; /**/*from w w w . j a v a 2s .c o m*/ * Copyright (C) 2007 Asterios Raptis * * 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.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Main { /** * Gets the output stream from a File object. * * @param file * the file. * @return the output stream. * @throws IOException * Signals that an I/O exception has occurred. */ public static OutputStream getOutputStream(final File file) throws IOException { return getOutputStream(file, false); } /** * Gets the output stream from a File object. * * @param file * the file * @param createFile * If true and the file does not exist it will be create a new file. * @return the output stream * @throws IOException * Signals that an I/O exception has occurred. */ public static OutputStream getOutputStream(final File file, final boolean createFile) throws IOException { OutputStream os = null; BufferedOutputStream bos = null; if (file.exists()) { os = new FileOutputStream(file); } else { if (createFile) { file.createNewFile(); os = new FileOutputStream(file); } else { throw new FileNotFoundException("File " + file.getName() + " does not exist."); } } bos = new BufferedOutputStream(os); return bos; } }