Example usage for org.apache.commons.io FileUtils openOutputStream

List of usage examples for org.apache.commons.io FileUtils openOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils openOutputStream.

Prototype

public static FileOutputStream openOutputStream(File file) throws IOException 

Source Link

Document

Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

Usage

From source file:com.pentaho.di.purge.RepositoryCleanupUtil.java

/**
 * Write out to log file//from   ww w.j a v a2s .  c o m
 * 
 * @param message
 * @return
 * @throws Exception
 */
private String writeLog(String message) throws Exception {
    String logName;
    if (logFile != null) {
        logName = logFile;
    } else {
        DateFormat df = new SimpleDateFormat(logFileNameDateFormat);
        logName = DEFAULT_LOG_FILE_PREFIX + df.format(new Date()) + ".txt";
    }
    File file = new File(logName);
    FileOutputStream fout = FileUtils.openOutputStream(file);
    IOUtils.copy(IOUtils.toInputStream(message), fout);
    fout.close();
    return logName;
}

From source file:com.buddycloud.mediaserver.business.dao.MediaDAO.java

protected Media storeMedia(String fileName, String title, String description, String author, String entityId,
        String mimeType, byte[] data, final boolean isAvatar) throws FileUploadException {

    String directory = getDirectory(entityId);
    mkdir(directory);//from  w w  w.j a  v a2 s.  co  m

    // TODO assert id uniqueness
    String mediaId = RandomStringUtils.randomAlphanumeric(20);
    String filePath = directory + File.separator + mediaId;
    File file = new File(filePath);

    LOGGER.debug("Storing new media: " + file.getAbsolutePath());

    try {
        FileOutputStream out = FileUtils.openOutputStream(file);

        out.write(data);
        out.close();
    } catch (IOException e) {
        LOGGER.error("Error while storing file: " + filePath, e);
        throw new FileUploadException(e.getMessage());
    }

    final Media media = createMedia(mediaId, fileName, title, description, author, entityId, mimeType, file,
            isAvatar);

    // store media's metadata
    new Thread() {
        public void start() {
            try {
                dataSource.storeMedia(media);

                if (isAvatar) {
                    if (dataSource.getEntityAvatarId(media.getEntityId()) != null) {
                        dataSource.updateEntityAvatar(media.getEntityId(), media.getId());
                    } else {
                        dataSource.storeAvatar(media);
                    }
                }
            } catch (MetadataSourceException e) {
                // do nothing
                LOGGER.error("Database error", e);
            }
        }
    }.start();

    return media;
}

From source file:edu.cornell.med.icb.goby.modes.TestLastToCompact.java

@Before
public void setUp() throws IOException {
    final File dir = new File("test-results/alignments/last-to-compact/");
    dir.mkdirs();//from  w w  w.  ja  v a2s.  c o  m

    final FileWriter writer1 = new FileWriter("test-results/alignments/last-to-compact/last-101.maf");
    writer1.write(getMafInput1());
    writer1.close();

    final FileWriter writer2 = new FileWriter("test-results/alignments/last-to-compact/last-102.maf");
    writer2.write(getMafInput2());
    writer2.close();

    final FileWriter writer3 = new FileWriter(
            "test-results/alignments/last-to-compact/last-103-variations.maf");
    writer3.write(getMafInput3Variations());
    writer3.close();

    final ReadsWriter referenceWriter = new ReadsWriterImpl(FileUtils.openOutputStream(
            new File("test-results/alignments/last-to-compact/last-reference.compact-reads")));
    referenceWriter.setIdentifier("0");
    referenceWriter.appendEntry();
    referenceWriter.close();
}

From source file:com.tacitknowledge.maven.plugin.crx.CRXPackageInstallerPlugin.java

/**
 * Copies stream to a file./*  www  . j a v  a 2  s  .c  om*/
 * @param input the input stream
 * @param destination  File to write to
 * @throws IOException exception
 */
public static void copyStreamToFile(InputStream input, File destination) throws IOException {

    try {
        FileOutputStream output = FileUtils.openOutputStream(destination);
        try {
            IOUtils.copy(input, output);
        } finally {
            IOUtils.closeQuietly(output);
        }
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:com.ettrema.httpclient.Host.java

/**
 *
 * @param path - the path to get, relative to the base path of the host
 * @param file - the file to write content to
 * @param listener//w ww.j  ava 2s.  c  o m
 * @throws IOException
 * @throws NotFoundException
 * @throws com.ettrema.httpclient.HttpException
 * @throws com.ettrema.httpclient.Utils.CancelledException
 * @throws NotAuthorizedException
 * @throws BadRequestException
 * @throws ConflictException
 */
public synchronized void doGet(Path path, final java.io.File file, ProgressListener listener)
        throws IOException, NotFoundException, com.ettrema.httpclient.HttpException, CancelledException,
        NotAuthorizedException, BadRequestException, ConflictException {
    LogUtils.trace(log, "doGet", path);
    if (fileSyncer != null) {
        fileSyncer.download(this, path, file, listener);
    } else {
        String url = this.buildEncodedUrl(path);
        transferService.get(url, new StreamReceiver() {

            @Override
            public void receive(InputStream in) throws IOException {
                OutputStream out = null;
                BufferedOutputStream bout = null;
                try {
                    out = FileUtils.openOutputStream(file);
                    bout = new BufferedOutputStream(out);
                    IOUtils.copy(in, bout);
                    bout.flush();
                } finally {
                    IOUtils.closeQuietly(bout);
                    IOUtils.closeQuietly(out);
                }

            }
        }, null, listener);
    }
}

From source file:de.fosd.jdime.artifact.file.FileArtifact.java

/**
 * Writes the {@link #content} of this {@link FileArtifact} to its {@link #file}.
 *
 * @throws IOException/*  w ww. j  a  v  a2  s  .  co  m*/
 *         see {@link FileUtils#openOutputStream(File)}
 */
private void writeToFile() throws IOException {
    try (OutputStreamWriter out = new OutputStreamWriter(FileUtils.openOutputStream(file), UTF_8)) {
        out.write(content);
    }
}

From source file:edu.cornell.med.icb.goby.modes.TestDiscoverSequenceVariantsMode.java

@Before
public void setUp() throws IOException {

    final File dir = new File(BASE_TEST_DIR);
    dir.mkdirs();//from  w ww . j a  va  2s.  co  m

    final ReadsWriter referenceWriter = new ReadsWriterImpl(FileUtils.openOutputStream(
            new File("test-results/alignments/last-to-compact/last-reference.compact-reads")));
    referenceWriter.setIdentifier("0");
    referenceWriter.appendEntry();
    referenceWriter.close();

}

From source file:io.milton.httpclient.Host.java

/**
 *
 * @param path - the path to get, relative to the base path of the host
 * @param file - the file to write content to
 * @param listener//from  ww w  .j ava  2 s  .c o m
 * @throws IOException
 * @throws NotFoundException
 * @throws com.ettrema.httpclient.HttpException
 * @throws com.ettrema.httpclient.Utils.CancelledException
 * @throws NotAuthorizedException
 * @throws BadRequestException
 * @throws ConflictException
 */
public synchronized void doGet(Path path, final java.io.File file, ProgressListener listener)
        throws IOException, NotFoundException, io.milton.httpclient.HttpException, CancelledException,
        NotAuthorizedException, BadRequestException, ConflictException {
    LogUtils.trace(log, "doGet", path);
    if (fileSyncer != null) {
        fileSyncer.download(this, path, file, listener);
    } else {
        String url = this.buildEncodedUrl(path);
        transferService.get(url, new StreamReceiver() {
            @Override
            public void receive(InputStream in) throws IOException {
                OutputStream out = null;
                BufferedOutputStream bout = null;
                try {
                    out = FileUtils.openOutputStream(file);
                    bout = new BufferedOutputStream(out);
                    IOUtils.copy(in, bout);
                    bout.flush();
                } finally {
                    IOUtils.closeQuietly(bout);
                    IOUtils.closeQuietly(out);
                }

            }
        }, null, listener, newContext());
    }
}

From source file:com.microsoft.o365_android_onenote_rest.snippet.PagesSnippet.java

protected File getImageFile(String imagePath) {
    URL imageResource = getClass().getResource(imagePath);
    File imageFile = null;/*  w  ww  . j  a v a  2s. co m*/
    try {
        imageFile = File.createTempFile(FilenameUtils.getBaseName(imageResource.getFile()),
                FilenameUtils.getExtension(imageResource.getFile()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        IOUtils.copy(imageResource.openStream(), FileUtils.openOutputStream(imageFile));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageFile;
}

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

public static void splitZipToFolder(File inputFile, File outputFolder, Set<String> includeEnties)
        throws IOException {
    if (!outputFolder.exists()) {
        outputFolder.mkdirs();/*w  w  w.  j a v  a 2 s.c  o  m*/
    }
    if (null == includeEnties || includeEnties.size() < 1) {
        return;
    }
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inputFile);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            if (!includeEnties.contains(name)) {
                continue;
            }
            File destFile = new File(outputFolder, name);
            destFile.getParentFile().mkdirs();
            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            FileOutputStream fout = FileUtils.openOutputStream(destFile);
            while ((count = zis.read(buffer)) != -1) {
                fout.write(buffer, 0, count);
            }
            fout.close();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
}