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.terasoluna.tourreservation.tourreserve.common.constants.MessageKeysGen.java

public static void main(String[] args) throws IOException {
    // message properties file

    InputStream applicationMessagesInputStream = new ClassPathResource(
            "i18n/application-messages_en.properties").getInputStream();
    InputStream validationMessagesInputStream = new ClassPathResource("ValidationMessages_en.properties")
            .getInputStream();//from  w w w.  jav  a 2 s.co m
    SequenceInputStream inputStream = new SequenceInputStream(applicationMessagesInputStream,
            validationMessagesInputStream);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

    Class<?> targetClazz = MessageKeys.class;
    File output = new File(
            "src/test/java/" + targetClazz.getName().replaceAll(Pattern.quote("."), "/") + ".java");
    System.out.println("write " + output.getAbsolutePath());
    PrintWriter pw = new PrintWriter(FileUtils.openOutputStream(output));

    try {
        pw.println("/*");
        pw.println(" * Copyright (C) 2013-2016 NTT DATA Corporation");
        pw.println(" *");
        pw.println(" * Licensed under the Apache License, Version 2.0 (the \"License\");");
        pw.println(" * you may not use this file except in compliance with the License.");
        pw.println(" * You may obtain a copy of the License at");
        pw.println(" *");
        pw.println(" *     http://www.apache.org/licenses/LICENSE-2.0");
        pw.println(" *");
        pw.println(" * Unless required by applicable law or agreed to in writing, software");
        pw.println(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
        pw.println(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,");
        pw.println(" * either express or implied. See the License for the specific language");
        pw.println(" * governing permissions and limitations under the License.");
        pw.println(" */");
        pw.println("package " + targetClazz.getPackage().getName() + ";");
        pw.println("/**");
        pw.println(" * Message Id");
        pw.println(" */");
        pw.println("public class " + targetClazz.getSimpleName() + " {");

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] vals = line.split("=", 2);
            if (vals.length > 1) {
                String key = vals[0].trim();
                String value = vals[1].trim();
                pw.println("    /** " + key + "=" + value + " */");
                pw.println("    public static final String " + key.toUpperCase()
                        .replaceAll(Pattern.quote("."), "_").replaceAll(Pattern.quote("-"), "_") + " = \"" + key
                        + "\";");
            }
        }
        pw.println("}");
        pw.flush();
    } finally {
        IOUtils.closeQuietly(applicationMessagesInputStream);
        IOUtils.closeQuietly(validationMessagesInputStream);
        IOUtils.closeQuietly(bufferedReader);
        IOUtils.closeQuietly(pw);
    }
}

From source file:org.uzebox.tools.converters.gfx.Main.java

private static void convertRawToMovieFrame2(String file) throws Exception {

    File outFile = new File("f:\\matrix.uzm");
    if (outFile.exists()) {
        outFile.delete();//w  w  w.  j av  a 2 s  .  c o m
    }

    FileOutputStream fs = FileUtils.openOutputStream(outFile);

    byte[] out = new byte[512 * 38];
    byte[] in;

    for (int f = 1; f < 4317; f++) {
        for (int t = 0; t < 2; t++) { //double field
            File inFile = new File("f:\\temp\\output\\matrix" + pad(f) + ".raw");
            in = FileUtils.readFileToByteArray(inFile);

            int inPos = 0;
            int outPos = 0;
            for (int i = 0; i < in.length; i++) {
                out[outPos++] = in[i];
                inPos++;
                if (inPos == 510) {
                    inPos = 0;
                    out[outPos++] = 0;
                    out[outPos++] = 0;
                }
            }
            IOUtils.write(out, fs);
        }

        System.out.println("Processing frame: " + f);
    }

    fs.flush();
    fs.close();

    //FileUtils.writeByteArrayToFile(outFile, out);
    System.out.println("Processing file " + file + "...Done!");

}

From source file:org.uzebox.tools.converters.gfx.Main.java

private static void convertRawToMovieFrame(String framesPath, String audioPath, String outPath, int frameCount)
        throws Exception {
    //String outFileName="f:\\badluck.uzm";
    String outFileName = outPath;

    File outFile = new File(outFileName);
    if (outFile.exists()) {
        outFile.delete();/*from w  ww. java 2 s.c  o m*/
    }

    FileOutputStream fs = FileUtils.openOutputStream(outFile);

    //      byte[] audio=FileUtils.readFileToByteArray(new File("F:\\temp\\badluck_export\\raw\\Sequence.raw"));
    byte[] audio = FileUtils.readFileToByteArray(new File(audioPath));
    //byte[] audioChunk=new byte[512];

    byte[] out = new byte[512 * 39];
    byte[] in;
    int audioPos = 0;

    for (int f = 1; f < frameCount; f++) {

        for (int t = 0; t < 2; t++) { //double field

            //write audio chunk for field
            for (int c = 0; c < 262; c++) {
                out[c] = audio[audioPos++];
            }

            //File inFile=new File("F:\\temp\\badluck_export\\raw\\Sequence"+pad(f)+".raw");
            File inFile = new File(framesPath + pad(f) + ".raw");
            in = FileUtils.readFileToByteArray(inFile);

            int inPos = 0;
            int outPos = 512;
            for (int i = 0; i < in.length; i++) {
                out[outPos++] = in[i];
                inPos++;
                if (inPos == 510) {
                    inPos = 0;
                    out[outPos++] = 0;
                    out[outPos++] = 0;
                }
            }
            IOUtils.write(out, fs);
        }

        System.out.println("Processing frame: " + f);
    }

    fs.flush();
    fs.close();

    //FileUtils.writeByteArrayToFile(outFile, out);
    System.out.println("Done processing file: " + outFileName + " !");

}

From source file:org.uzebox.tools.converters.gfx.Main.java

public static void toMonoFrame() throws IOException {

    String outFilename = "c://work//uzebox//trunk//demos//tutorial-sdcard//data//logo.dat";
    //byte[] in=FileUtils.readFileToByteArray(new File("c://work//uzebox//trunk//demos//tutorial-sdcard//data//logo.raw"));   
    //       System.out.println("in size="+in.length);

    File outFile = new File(outFilename);
    if (outFile.exists()) {
        outFile.delete();/*  w ww  .j  a v a2s  .  co  m*/
    }
    FileOutputStream fs = FileUtils.openOutputStream(outFile);

    NumberFormat formatter = new DecimalFormat("000");

    byte[] in;
    for (int i = 1; i < 404; i++) {

        in = FileUtils.readFileToByteArray(
                new File("c://Temp//conv//foo-" + formatter.format(i).toString() + ".raw"));
        IOUtils.write(in, fs);
    }
    /*
    byte[] out=new byte[256*224/8];
    int c=0;
    int b;
            
    for(int y=0;y<224;y++){
       for(int x=0;x<256;x+=8){
     if(((y*256)+x)==0x9860){
        System.out.println("Debug");
     }
     b=0;
     b|=(in[(y*256)+x+0])<<7;
     b|=(in[(y*256)+x+1])<<6;
     b|=(in[(y*256)+x+2])<<5;
     b|=(in[(y*256)+x+3])<<4;
     b|=(in[(y*256)+x+4])<<3;
     b|=(in[(y*256)+x+5])<<2;
     b|=(in[(y*256)+x+6])<<1;
     b|=(in[(y*256)+x+7]);
     out[c++]=(byte)(b&0xff);
       }
    }
            
    //duplicate frames for 60 fps 
    for(int i=0;i<(60*5);i++){
       IOUtils.write(out, fs);
    }
    */
    fs.flush();
    fs.close();

    System.out.println("Done processing file: " + outFilename + " !");
}

From source file:org.wso2.carbon.automation.extensions.servers.axis2server.Axis2ServerManager.java

/**
 * replace key store paths of file for HTTPS transport
 * @param file - name of the file to read
 * @param newFile - name of the new file to e created
 * @throws IOException/* w ww .  j a va 2s.c o m*/
 */
private void changeConfiguration(String file, String newFile) throws IOException {
    StringBuilder sb = new StringBuilder();
    // axis2 config
    File config = new File(FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts"
            + File.separator + "AXIS2" + File.separator + "config" + File.separator + file);
    BufferedReader br = null;
    OutputStream os = null;

    try {

        if (config != null) {
            String currentLine;

            br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(config), Charset.defaultCharset()));
            while ((currentLine = br.readLine()) != null) {
                if (currentLine.contains("REPLACE_CK")) {
                    currentLine = currentLine.replace("REPLACE_CK",
                            System.getProperty(FrameworkConstants.CARBON_HOME) + File.separator + "repository"
                                    + File.separator + "resources" + File.separator + "security"
                                    + File.separator + "wso2carbon.jks");
                } else if (currentLine.contains("REPLACE_TS")) {
                    currentLine = currentLine.replace("REPLACE_TS",
                            System.getProperty(FrameworkConstants.CARBON_HOME) + File.separator + "repository"
                                    + File.separator + "resources" + File.separator + "security"
                                    + File.separator + "client-truststore.jks");
                }
                sb.append(currentLine);
            }
        }
        // created axis2 config
        File newConfig = new File(ExtensionUtils.getSystemResourceLocation() + File.separator + "artifacts"
                + File.separator + "AXIS2" + File.separator + "config" + File.separator + newFile);
        if (newConfig.exists()) {
            FileUtils.deleteQuietly(newConfig);
        }

        FileUtils.touch(newConfig);
        os = FileUtils.openOutputStream(newConfig);
        os.write(sb.toString().getBytes("UTF-8"));

    } finally {
        if (os != null)
            os.close();

        if (br != null)
            br.close();
    }

}

From source file:org.wso2.carbon.automation.extensions.servers.axis2server.Axis2ServerManager.java

/**
 * copy resources//from  w  w  w  . j  a va2  s.c o  m
 * @param resourceName
 * @param fileName
 * @return
 * @throws IOException
 */

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);
    }
    FileUtils.touch(file);
    OutputStream os = FileUtils.openOutputStream(file);
    InputStream is = null;

    try {
        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);
            }
        }
    } finally {
        os.flush();
        os.close();

        if (is != null)
            is.close();
    }

    return file;
}

From source file:org.wso2.carbon.automation.extensions.servers.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 va  2  s.  c o m
    }

    FileUtils.touch(file);
    OutputStream os = FileUtils.openOutputStream(file);
    InputStream is = null;

    try {

        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();

        }
    } finally {
        os.close();
        is.close();
    }

    return file;
}

From source file:org.wso2.carbon.automation.test.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);//from   www . j  a va2 s. co  m
    }
    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.carbon.core.persistence.metadata.ArtifactMetadataManager.java

private void saveParameters(ArtifactMetadata metadataObject) throws ArtifactMetadataException {
    FileOutputStream fileOutputStream = null;
    File file = metadataObject.getFile();
    if (!file.getParentFile().exists()) {
        boolean created = file.getParentFile().mkdirs();
        if (!created) {
            throw new ArtifactMetadataException(
                    "Unable to create directory structure for persisting metafiles" + file.getPath());
        }/*from  w ww  .  java 2  s .co m*/
    }
    String comment = metadataObject.getArtifactName() + "@@@" + metadataObject.getArtifactType();
    try {
        fileOutputStream = FileUtils.openOutputStream(file);
        metadataObject.getProperties().store(fileOutputStream, "UTF-8");

    } catch (FileNotFoundException e) {
        handleException("File can not be opened for writing. " + e.getMessage(), e);
    } catch (IOException e) {
        handleException(" Error while saving artifact metafiles for " + metadataObject.getArtifactName() + ". "
                + e.getMessage(), e);
    } finally {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:org.wso2.carbon.esb.proxyservice.test.proxyservices.HttpsServiceViaHttpProxyTestCase.java

/**
 * Method to change the axis2 config file dynamically
 *
 * @param file : Config file/*ww w .  j  a v  a  2s.com*/
 * @throws java.io.IOException
 */
private void changeConfiguration(String file) throws IOException {
    StringBuilder sb = new StringBuilder();
    File config = new File(FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts"
            + File.separator + "AXIS2" + File.separator + "config" + File.separator + file);
    if (config != null) {
        String currentLine;
        BufferedReader br = new BufferedReader(new FileReader(config));
        while ((currentLine = br.readLine()) != null) {
            if (currentLine.contains("REPLACE_CK")) {
                currentLine = currentLine.replace("REPLACE_CK",
                        System.getProperty(ServerConstants.CARBON_HOME) + File.separator + "repository"
                                + File.separator + "resources" + File.separator + "security" + File.separator
                                + "wso2carbon.jks");
            } else if (currentLine.contains("REPLACE_TS")) {
                currentLine = currentLine.replace("REPLACE_TS",
                        System.getProperty(ServerConstants.CARBON_HOME) + File.separator + "repository"
                                + File.separator + "resources" + File.separator + "security" + File.separator
                                + "client-truststore.jks");
            }
            sb.append(currentLine);
        }
        br.close();
    }
    File newConfig = new File(FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts"
            + File.separator + "AXIS2" + File.separator + "config" + File.separator + MODIFIED_RESOURCE_NAME);
    if (newConfig.exists()) {
        FileUtils.deleteQuietly(newConfig);
    }

    FileUtils.touch(newConfig);
    OutputStream os = FileUtils.openOutputStream(newConfig);
    os.write(sb.toString().getBytes("UTF-8"));
    os.close();
}