Example usage for java.io OutputStream close

List of usage examples for java.io OutputStream close

Introduction

In this page you can find the example usage for java.io OutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:dpfmanager.shell.core.DPFManagerProperties.java

public static void setPropertiesConfig(Properties properties) {
    try {//from   www. ja va 2s.  c o m
        File configFile = new File(getDataDir() + "/dpfmanager.properties");
        if (configFile.exists()) {
            OutputStream os = new FileOutputStream(configFile);
            properties.store(os, null);
            os.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static String UdatePlayerBT(String userName, String device) {

    String retStr = "";

    try {//from   www.  j a v a  2s  .  com
        URL url = new URL("http://" + IP_ADDRESS + "/RiddleQuizApp/ServerSide/PostaviBTdevice.php");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(15000);
        conn.setReadTimeout(10000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        Log.e("http", "por1");

        JSONObject data = new JSONObject();

        data.put("username", userName);
        data.put("bt_device", device);

        Log.e("http", "por3");

        Uri.Builder builder = new Uri.Builder().appendQueryParameter("action", data.toString());
        String query = builder.build().getEncodedQuery();

        Log.e("http", "por4");

        OutputStream os = conn.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        bw.write(query);
        Log.e("http", "por5");
        bw.flush();
        bw.close();
        os.close();
        int responseCode = conn.getResponseCode();

        Log.e("http", String.valueOf(responseCode));
        if (responseCode == HttpURLConnection.HTTP_OK) {
            retStr = inputStreamToString(conn.getInputStream());
        } else
            retStr = String.valueOf("Error: " + responseCode);

        Log.e("http", retStr);

    } catch (Exception e) {
        Log.e("http", "greska");
    }
    return retStr;
}

From source file:com.stratio.decision.unit.engine.action.CassandraServer.java

/**
 * Copies a resource from within the jar to a directory.
 * //w w w.  j a  v a  2s.com
 * @param resource
 * @param directory
 * @throws IOException
 */
private static void copy(String resource, String directory) throws IOException {
    mkdir(directory);
    InputStream is = CassandraServer.class.getResourceAsStream(resource);
    String fileName = resource.substring(resource.lastIndexOf("/") + 1);
    File file = new File(directory + File.separator + fileName);
    OutputStream out = new FileOutputStream(file);
    byte buf[] = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    out.close();
    is.close();
}

From source file:Main.java

public static void copyRawFile(Context ctx, String rawFileName, String to) {
    String[] names = rawFileName.split("\\.");
    String toFile = to + "/" + names[0] + "." + names[1];
    File file = new File(toFile);
    if (file.exists()) {
        return;//from ww w.  ja  va  2  s.  c om
    }
    try {
        InputStream is = getStream(ctx, "raw://" + names[0]);
        OutputStream os = new FileOutputStream(toFile);
        int byteCount = 0;
        byte[] bytes = new byte[1024];

        while ((byteCount = is.read(bytes)) != -1) {
            os.write(bytes, 0, byteCount);
        }
        os.close();
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Copy data from a source stream to destFile. Return true if succeed,
 * return false if failed.//w  ww.  j  a  v  a  2  s.  c o m
 */

private static boolean copyToFile(InputStream inputStream, File destFile) {
    try {
        OutputStream out = new FileOutputStream(destFile);
        try {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) >= 0) {
                out.write(buffer, 0, bytesRead);
            }
        } finally {
            out.close();
        }
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:gdv.xport.Main.java

/**
 * Hier werden die Optionen "-xml" und "-html" abgehandelt.
 * Die Option "-java" wird seit 0.9 nicht mehr unterstuetzt.
 *
 * @param cmd the cmd//  w  ww.  j a v  a  2  s. c  o m
 * @param datenpaket the datenpaket
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void formatDatenpaket(final CommandLine cmd, final Datenpaket datenpaket) throws IOException {
    AbstractFormatter formatter = new NullFormatter(new NullWriter());
    if (cmd.hasOption("xml")) {
        formatter = new XmlFormatter();
    } else if (cmd.hasOption("html")) {
        formatter = new HtmlFormatter();
    }
    if (cmd.hasOption("export")) {
        File file = new File(cmd.getOptionValue("export"));
        if (formatter instanceof NullFormatter) {
            String suffix = FilenameUtils.getExtension(file.getName());
            if ("xml".equalsIgnoreCase(suffix)) {
                formatter = new XmlFormatter();
            } else if ("html".equalsIgnoreCase(suffix)) {
                formatter = new HtmlFormatter();
            }
        }
        OutputStream ostream = new FileOutputStream(file);
        try {
            formatter.setWriter(ostream);
            formatter.write(datenpaket);
        } finally {
            ostream.close();
        }
    } else {
        formatter.write(datenpaket);
    }
}

From source file:Main.java

static boolean copyFiles(File sourceLocation, File targetLocation) throws IOException {
    if (sourceLocation.equals(targetLocation))
        return false;

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();/*from  www .  j  av  a2 s.c o m*/
        }
        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++) {
            copyFiles(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
        }
    } else if (sourceLocation.exists()) {
        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    return true;
}

From source file:me.vertretungsplan.parser.DSBMobileParser.java

private static String encode(String input) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    OutputStream gzipOs = new GZIPOutputStream(os);
    gzipOs.write(input.getBytes(ENCODING));
    gzipOs.close();
    byte[] outputBytes = os.toByteArray();
    return Base64.encodeBase64String(outputBytes);
}

From source file:ReadTemp.java

/** Executes the given applescript code and returns the first line of the 
 output as a string *//*from  w  w w  .  ja va  2 s  .c o m*/
static String doApplescript(String script) {
    String line;
    try {
        // Start applescript 
        Process p = Runtime.getRuntime().exec("/usr/bin/osascript -s o -");

        // Send applescript via stdin
        OutputStream stdin = p.getOutputStream();
        stdin.write(script.getBytes());
        stdin.flush();
        stdin.close();

        // get first line of output
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        line = input.readLine();
        input.close();

        // If we get an exit code, print it out
        if (p.waitFor() != 0) {
            System.err.println("exit value = " + p.exitValue());
            System.err.println(line);
        }
        return line;
    } catch (Exception e) {
        System.err.println(e);
    }

    return "";
}

From source file:com.cip.crane.agent.utils.FileExtractUtils.java

/** Untar an input file into an output file.
        /*from w  w w . j  a  v  a  2  s .  c  om*/
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.tar' extension. 
 * 
 * @param inputFile     the input .tar file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException 
 */
public static List<File> unTar(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {
    LOG.debug(
            String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            if (!outputFile.exists()) {
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();
    return untaredFiles;
}