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.haulmont.cuba.desktop.sys.LoginProperties.java

protected void saveProperties() {
    File propertiesFile = new File(dataDir, "login.properties");
    try {//from  w w  w  . j a  v  a  2 s .  co m
        FileOutputStream stream = FileUtils.openOutputStream(propertiesFile);
        try {
            properties.store(stream, "Login properties");
        } finally {
            IOUtils.closeQuietly(stream);
        }
    } catch (IOException e) {
        log.error("Error saving login properties", e);
    }
}

From source file:de.huxhorn.sulky.groovy.GroovyInstanceTest.java

private void copyIntoFile(String resource, File output) throws IOException {
    FileOutputStream out = null;//from w w w  .j a  v  a2s .c  o m
    InputStream in = null;
    try {
        out = FileUtils.openOutputStream(output);
        in = GroovyInstanceTest.class.getResourceAsStream(resource);
        IOUtils.copy(in, out);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
}

From source file:backtype.storm.utils.LocalStateTest.java

private void touchVersion(String versionPath) throws IOException {
    FileUtils.openOutputStream(new File(versionPath));
    FileUtils.openOutputStream(new File(versionPath + ".version"));
}

From source file:javacommon.excel.ExcelUtils.java

/**
 * ?/*  w  ww. j a v a  2 s  .  c o m*/
 * @param file 
 * @param data ?
 */
public static <T> void write(File file, List<T[]> data) throws IOException {
    Writer w = new ExcelWriter(getWorkBook(file.getName()));
    String sheetName = FilenameUtils.getBaseName(file.getName());
    Sheet sheet = w.createSheet(sheetName);
    for (int i = 0; i < data.size(); i++) {
        w.writeRow(sheet, data.get(i), i);
    }
    OutputStream out = null;
    try {
        out = FileUtils.openOutputStream(file);
        w.flush(out);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.izforge.izpack.installer.unpacker.CompressedFileUnpacker.java

/**
 * Unpacks a pack file.//from   w  ww  . j av  a 2  s . c  o m
 *
 * @param file            the pack file meta-data
 * @param packInputStream the pack input stream
 * @param target          the target
 * @throws IOException        for any I/O error
 * @throws InstallerException for any installer exception
 */
@Override
public void unpack(PackFile file, InputStream packInputStream, File target)
        throws IOException, InstallerException {
    File tmpfile = File.createTempFile("izpack-uncompress", null, FileUtils.getTempDirectory());
    OutputStream fo = null;
    InputStream finalStream = null;

    try {
        fo = IOUtils.buffer(FileUtils.openOutputStream(tmpfile));
        IOUtils.copyLarge(packInputStream, fo, 0, file.size());
        fo.flush();
        fo.close();

        InputStream in = IOUtils.buffer(FileUtils.openInputStream(tmpfile));

        if (compressionFormat == PackCompression.DEFLATE) {
            DeflateParameters deflateParameters = new DeflateParameters();
            deflateParameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
            finalStream = new DeflateCompressorInputStream(in, deflateParameters);
        } else {
            finalStream = new CompressorStreamFactory().createCompressorInputStream(compressionFormat.toName(),
                    in);
        }

        copy(file, finalStream, target);
    } catch (CompressorException e) {
        throw new IOException(e);
    } finally {
        IOUtils.closeQuietly(fo);
        IOUtils.closeQuietly(finalStream);
        FileUtils.deleteQuietly(tmpfile);
    }
}

From source file:net.larry1123.elec.util.logger.LoggerDirectoryHandler.java

protected OutputStream makeFileOutputStreamForLogger(Logger logger) throws IOException {
    File file = getFileForLogger(logger);
    FileOutputStream fileOutputStream = FileUtils.openOutputStream(file);
    return new BufferedOutputStream(fileOutputStream);
}

From source file:com.intuit.tank.agent.AgentStartup.java

public void run() {
    logger.info("Starting up...");
    if (AmazonUtil.usingEip()) {
        try {/*ww w.j  ava 2s .  co m*/
            logger.info("Using EIP. Sleeping for " + WAIT_FOR_RESTART_TIME + " ms.");
            Thread.sleep(WAIT_FOR_RESTART_TIME);
        } catch (InterruptedException e1) {
            logger.info("Exception waiting.");
            System.exit(0);
        }
    }
    try {
        if (controllerBase == null) {
            controllerBase = AmazonUtil.getControllerBaseUrl();
        }

        logger.info("Starting up: ControllerBaseUrl=" + controllerBase);
        URL url = new URL(controllerBase + SERVICE_RELATIVE_PATH + METHOD_SETTINGS);
        logger.info("Starting up: making call to tank service url to get settings.xml " + url.toExternalForm());
        InputStream settingsStream = url.openStream();
        try {
            String settings = IOUtils.toString(settingsStream);
            FileUtils.writeStringToFile(new File("settings.xml"), settings);
            logger.info("got settings file...");
        } finally {
            IOUtils.closeQuietly(settingsStream);
        }
        url = new URL(controllerBase + SERVICE_RELATIVE_PATH + METHOD_SUPPORT);
        logger.info("Making call to tank service url to get support files " + url.toExternalForm());
        ZipInputStream zip = new ZipInputStream(url.openStream());
        try {
            ZipEntry entry = zip.getNextEntry();
            while (entry != null) {
                String name = entry.getName();
                logger.info("Got file from controller: " + name);
                File f = new File(name);
                FileOutputStream fout = FileUtils.openOutputStream(f);
                try {
                    IOUtils.copy(zip, fout);
                } finally {
                    IOUtils.closeQuietly(fout);
                }
                entry = zip.getNextEntry();
            }
        } finally {
            IOUtils.closeQuietly(zip);
        }
        // now start the harness
        String jvmArgs = AmazonUtil.getUserDataAsMap().get(TankConstants.KEY_JVM_ARGS);
        logger.info("Starting apiharness with command: " + API_HARNESS_COMMAND + " -http=" + controllerBase
                + " " + jvmArgs);
        Runtime.getRuntime().exec(API_HARNESS_COMMAND + " -http=" + controllerBase + " " + jvmArgs);
    } catch (Exception e) {
        logger.error("Error in AgentStartup " + e, e);
    }
}

From source file:com.textocat.textokit.commons.io.axml.AXML2XMI.java

private void run() throws ResourceInitializationException, IOException, SAXException {
    Collection<File> inputFiles = FileUtils.listFiles(inputDir, FileFilterUtils.suffixFileFilter(".xml"), null);
    if (inputFiles.isEmpty()) {
        return;//from   www  .j a va 2s  .  co  m
    }
    TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescription(typeSystemDescName);
    CAS cas = CasCreationUtils.createCas(tsd, null, null);
    for (File inputFile : inputFiles) {
        AXMLReader.read(inputFile, cas);
        File outFile = getOutputFile(inputFile);
        OutputStream out = FileUtils.openOutputStream(outFile);
        try {
            XmiCasSerializer.serialize(cas, null, out, true, null);
        } finally {
            out.close();
            cas.reset();
        }
    }
}

From source file:gov.nih.nci.caarray.services.external.v1_0.data.JavaDataApiUtils.java

/**
 * {@inheritDoc}/* w  w  w. j av  a2 s .  com*/
 */
public void downloadFileContentsToDir(Iterable<CaArrayEntityReference> fileRefs, File dir)
        throws InvalidReferenceException, DataTransferException, IOException {
    if (!dir.exists() && !dir.mkdirs()) {
        throw new IOException("Could not create temporary directory " + dir.getAbsolutePath());
    }
    for (CaArrayEntityReference fileRef : fileRefs) {
        FileStreamableContents fsContents = dataService.streamFileContents(fileRef, true);
        FileOutputStream fos = FileUtils.openOutputStream(new File(dir, fsContents.getMetadata().getName()));
        readFully(fsContents.getContentStream(), fos, true);
        IOUtils.closeQuietly(fos);
    }
}

From source file:gov.nih.nci.caarray.services.external.v1_0.data.AbstractDataApiUtils.java

/**
 * {@inheritDoc}/*from   w ww.j a  v  a  2s  . c o  m*/
 */
public void downloadFileContentsZipToFile(Iterable<CaArrayEntityReference> fileRefs, File toFile)
        throws InvalidReferenceException, DataTransferException, IOException {
    OutputStream ostream = FileUtils.openOutputStream(toFile);
    try {
        copyFileContentsZipToOutputStream(fileRefs, ostream);
    } finally {
        if (ostream != null) {
            ostream.close();
        }
    }
}