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:org.wso2.carbon.integration.common.extensions.axis2server.Axis2ServerManager.java

private File copyResourceToFileSystem(String resourceName, String fileName) throws IOException {
    File file = new File(System.getProperty("basedir") + File.separator + "target" + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);/*from w  w  w. j a  va  2  s. co  m*/
    }

    OutputStream os = null;
    InputStream is = null;

    try {
        FileUtils.touch(file);
        os = FileUtils.openOutputStream(file);

        if (resourceName.contains(".aar")) {
            is = new FileInputStream(FrameworkPathUtil.getSystemResourceLocation() + File.separator
                    + "artifacts" + File.separator + "AXIS2" + File.separator + "aar" + File.separator
                    + resourceName);
        } else {
            is = new FileInputStream(FrameworkPathUtil.getSystemResourceLocation() + File.separator
                    + "artifacts" + File.separator + "AXIS2" + File.separator + "config" + File.separator
                    + resourceName);
        }
        if (is != null) {
            byte[] data = new byte[1024];
            int len;
            while ((len = is.read(data)) != -1) {
                os.write(data, 0, len);
            }
        }

    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                log.warn("Unable to close the stream");
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.warn("Unable to close the stream");
            }
        }
    }
    return file;
}

From source file:org.wso2.carbon.integration.common.extensions.axis2server.Axis2ServerManager.java

private File copyServiceToFileSystem(String resourceName, String fileName) throws IOException {
    File file = new File(System.getProperty("basedir") + File.separator + "target" + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);/* ww w.j av a 2  s  .c  om*/
    }
    OutputStream os = null;
    InputStream is = null;
    try {
        FileUtils.touch(file);
        os = FileUtils.openOutputStream(file);
        is = new FileInputStream(FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts"
                + File.separator + "AXIS2" + File.separator + "config" + File.separator + resourceName);
        if (is != null) {
            byte[] data = new byte[1024];
            int len;
            while ((len = is.read(data)) != -1) {
                os.write(data, 0, len);
            }
        }
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                log.warn("Unable to close the stream");
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.warn("Unable to close the stream");
            }
        }
    }
    return file;
}

From source file:org.wso2.carbon.integration.common.utils.FileManager.java

public static File copyResourceToFileSystem(String sourcePath, String targetPath, String fileName)
        throws IOException {
    File file = new File(targetPath + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);//from  w  w  w .ja  v a2  s  .  c  om
    }
    OutputStream os = null;
    InputStream is = null;

    try {
        FileUtils.touch(file);
        os = FileUtils.openOutputStream(file);
        is = new FileInputStream(sourcePath);

        if (is != null) {
            byte[] data = new byte[1024];
            int len;
            while ((len = is.read(data)) != -1) {
                os.write(data, 0, len);
            }
        }
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                log.warn("Unable to close steam");
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.warn("Unable to close steam");
            }
        }
    }
    return file;
}

From source file:org.wso2.esb.integration.common.extensions.axis2server.Axis2ServerManager.java

private File copyResourceToFileSystem(String resourceName, String fileName) throws IOException {
    File file = new File(System.getProperty("basedir") + File.separator + "target" + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);//w  ww. j  a v a2s  .  co m
    }
    FileUtils.touch(file);
    OutputStream os = FileUtils.openOutputStream(file);
    InputStream is;
    if (resourceName.contains(".aar")) {
        is = new FileInputStream(ExtensionUtils.getSystemResourceLocation() + File.separator + "artifacts"
                + File.separator + "AXIS2" + File.separator + "aar" + File.separator + resourceName);
    } else {
        is = new FileInputStream(ExtensionUtils.getSystemResourceLocation() + File.separator + "artifacts"
                + File.separator + "AXIS2" + File.separator + "config" + File.separator + resourceName);
    }
    if (is != null) {
        byte[] data = new byte[1024];
        int len;
        while ((len = is.read(data)) != -1) {
            os.write(data, 0, len);
        }
    }
    os.flush();
    os.close();
    is.close();
    return file;
}

From source file:org.wso2.esb.integration.common.extensions.axis2server.Axis2ServerManager.java

private File copyServiceToFileSystem(String resourceName, String fileName) throws IOException {
    File file = new File(System.getProperty("basedir") + File.separator + "target" + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);/*from  w w w  .  j a  v  a 2  s  .com*/
    }
    FileUtils.touch(file);
    OutputStream os = FileUtils.openOutputStream(file);
    InputStream is = new FileInputStream(
            ExtensionUtils.getSystemResourceLocation() + File.separator + "artifacts" + File.separator + "AXIS2"
                    + File.separator + "config" + File.separator + resourceName);
    if (is != null) {
        byte[] data = new byte[1024];
        int len;
        while ((len = is.read(data)) != -1) {
            os.write(data, 0, len);
        }
        os.flush();
        os.close();
        is.close();
    }
    return file;
}

From source file:org.wso2.esb.integration.common.utils.common.FileManager.java

public static File copyResourceToFileSystem(String sourcePath, String targetPath, String fileName)
        throws IOException {

    File file = new File(targetPath + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);// ww w . j  av a 2  s  .  c  om
    }

    FileUtils.touch(file);
    OutputStream os = FileUtils.openOutputStream(file);

    InputStream is = new FileInputStream(sourcePath);

    if (is != null) {
        byte[] data = new byte[1024];
        int len;
        while ((len = is.read(data)) != -1) {
            os.write(data, 0, len);
        }
        os.flush();
        os.close();
        is.close();
    }
    return file;
}

From source file:org.wso2.esb.integration.common.utils.servers.axis2.SampleAxis2Server.java

private File copyResourceToFileSystem(String resourceName, String fileName) throws IOException {
    File file = new File(System.getProperty("basedir") + File.separator + "target" + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);/* w  ww .  j  a v  a  2  s  .c o  m*/
    }

    FileUtils.touch(file);
    OutputStream os = FileUtils.openOutputStream(file);
    InputStream is;
    if (resourceName.contains(".aar")) {
        is = new FileInputStream(FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts"
                + File.separator + "AXIS2" + File.separator + "aar" + File.separator + resourceName);
    } else {
        is = new FileInputStream(FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts"
                + File.separator + "AXIS2" + File.separator + "config" + File.separator + resourceName);
    }

    if (is != null) {
        byte[] data = new byte[1024];
        int len;
        while ((len = is.read(data)) != -1) {
            os.write(data, 0, len);
        }
        os.flush();
        os.close();
        is.close();
    }
    return file;
}

From source file:org.wso2.esb.integration.common.utils.servers.axis2.SampleAxis2Server.java

private File copyServiceToFileSystem(String resourceName, String fileName) throws IOException {
    File file = new File(System.getProperty("basedir") + File.separator + "target" + File.separator + fileName);
    if (file.exists()) {
        FileUtils.deleteQuietly(file);//from ww w  . j  a  v  a 2s  .  co m
    }

    FileUtils.touch(file);
    OutputStream os = FileUtils.openOutputStream(file);

    InputStream is = new FileInputStream(
            FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts" + File.separator
                    + "AXIS2" + File.separator + "config" + File.separator + resourceName);
    if (is != null) {
        byte[] data = new byte[1024];
        int len;
        while ((len = is.read(data)) != -1) {
            os.write(data, 0, len);
        }
        os.flush();
        os.close();
        is.close();
    }
    return file;
}

From source file:org.wte4j.impl.service.LocalFileStore.java

@Override
public OutputStream getOutStream(String fileName) throws IOException {
    File templateFile = new File(templateDirectory, fileName);
    if (!templateFile.exists()) {
        templateFile.createNewFile();/*w  w  w .  j  a va2s  .  c o  m*/
    }
    return FileUtils.openOutputStream(templateFile);
}

From source file:org.wte4j.impl.word.Docx4JWordTemplateTest.java

@Test
public void writeAsOPCXmlTest() throws IOException, Docx4JException, JAXBException {
    Docx4JWordTemplate doc = new Docx4JWordTemplate();

    File file = File.createTempFile("test", "docx");
    OutputStream out = FileUtils.openOutputStream(file);
    try {/*from   ww  w.j ava  2s  .com*/
        doc.writeAsOPCXml(out);
    } finally {
        IOUtils.closeQuietly(out);
    }
    String content = FileUtils.readFileToString(file);
    assertTrue(content.startsWith("<?xml"));
}