Here you can find the source of getOutputStream(final Object obj, final boolean append)
Parameter | Description |
---|---|
obj | an OutputStream or File. |
append | if obj is a file and this parameter is true, the output stream will be opened in append mode. |
Parameter | Description |
---|---|
IOException | if an error occurs |
public static OutputStream getOutputStream(final Object obj, final boolean append) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Main { /**//w w w . j av a2s . com * Turn an object into an OutputStream if possible. * * @param obj * an OutputStream or File. * @param append * if obj is a file and this parameter is true, the output stream * will be opened in append mode. * @return an OutputStream. If obj is a File, the stream is Buffered. * @throws IOException * if an error occurs */ public static OutputStream getOutputStream(final Object obj, final boolean append) throws IOException { OutputStream stream = null; if (obj instanceof OutputStream) { stream = (OutputStream) obj; } else if (obj instanceof File) { File file = (File) obj; // create parent directory first, if needed File parent = file.getAbsoluteFile().getParentFile(); if (!parent.exists()) { if (!parent.mkdirs()) { throw new IOException("Unable to create directory " + parent.getAbsolutePath()); } } if (!parent.canWrite()) { throw new IOException("Do not have write permission for directory " + parent.getAbsolutePath()); } stream = new BufferedOutputStream(new FileOutputStream(file, append)); } else { throw new IllegalArgumentException("Expected an OutputStream or File"); } return stream; } /** * Same as calling getOutputStream(obj, false). If obj is a file, it will * open a new output stream and will not append. * * @param obj * an OutputStream or File. * @return an OutputStream. If obj is a File, the stream is Buffered. * @throws IOException * if an error occurs. */ public static OutputStream getOutputStream(final Object obj) throws IOException { return getOutputStream(obj, false); } }