Example usage for java.io OutputStreamWriter close

List of usage examples for java.io OutputStreamWriter close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:btrplace.fromEntropy.Converter.java

public static void main(String[] args) {
    String src, dst = null, output, scriptDC = null, dirScriptsCL = null;

    if (args.length < 5 || args.length > 6 || !args[args.length - 2].equals("-o")) {
        usage(1);/*from  w  ww .j  a v a2  s  . co m*/
    }
    src = args[0];
    output = args[args.length - 1];
    if (args.length > 5) {
        dst = args[1];
    }
    scriptDC = args[args.length - 4];
    dirScriptsCL = args[args.length - 3];

    OutputStreamWriter out = null;
    try {
        // Convert the src file
        ConfigurationConverter conv = new ConfigurationConverter(src);
        Instance i = conv.getInstance();

        // Read the dst file, deduce and add the states constraints
        if (dst != null) {
            i.getSatConstraints().addAll(conv.getNextStates(dst));
        }

        // Read the script files
        ScriptBuilder scriptBuilder = new ScriptBuilder(i.getModel());
        //scriptBuilder.setIncludes(new PathBasedIncludes(scriptBuilder,
        //        new File("src/test/resources")));

        // Read the datacenter script file if exists
        if (scriptDC != null) {
            String strScriptDC = null;
            try {
                strScriptDC = readFile(scriptDC);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Script scrDC = null;
            try {
                // Build the DC script
                scrDC = scriptBuilder.build(strScriptDC);

            } catch (ScriptBuilderException sbe) {
                System.out.println(sbe);
            }

            // Set the DC script as an include
            BasicIncludes bi = new BasicIncludes();
            bi.add(scrDC);
            scriptBuilder.setIncludes(bi);
        }

        // Read all the client script files
        String scriptCL = null, strScriptCL = null;
        Script scrCL = null;
        Iterator it = FileUtils.iterateFiles(new File(dirScriptsCL), null, false);
        while (it.hasNext()) {
            scriptCL = dirScriptsCL + "/" + ((File) it.next()).getName();

            if (scriptCL != null) {
                // Read
                try {
                    strScriptCL = readFile(scriptCL);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                // Parse
                try {
                    scrCL = scriptBuilder.build(strScriptCL);

                } catch (ScriptBuilderException sbe) {
                    System.out.println(sbe);
                    sbe.printStackTrace();
                }

                // Add the resulting constraints
                if (scrCL.getConstraints() != null) {
                    i.getSatConstraints().addAll(scrCL.getConstraints());
                }
            }
        }

        /************** PATCH **************/
        // State constraints;
        for (Node n : i.getModel().getMapping().getOnlineNodes()) {
            i.getSatConstraints().add(new Online(n));
        }
        for (Node n : i.getModel().getMapping().getOfflineNodes()) {
            i.getSatConstraints().add(new Offline(n));
        }
        // Remove preserve constraints
        for (Iterator<SatConstraint> ite = i.getSatConstraints().iterator(); ite.hasNext();) {
            SatConstraint s = ite.next();
            if (s instanceof Preserve && src.contains("nr")) {
                ite.remove();
            }
        }
        /************************************/

        // Convert to JSON
        InstanceConverter iConv = new InstanceConverter();
        JSONObject o = iConv.toJSON(i);

        // Check for gzip extension
        if (output.endsWith(".gz")) {
            out = new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(output)));
        } else {
            out = new FileWriter(output);
        }

        // Write the output file
        o.writeJSONString(out);
        out.close();

    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        System.exit(1);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                System.err.println(e.getMessage());
                System.exit(1);
            }
        }
    }
}

From source file:Main.java

public static void logToFile(String filename, String log) {
    File myFile = new File("/sdcard/SAGETablet/" + filename + ".txt");
    try {//from   w  ww .  j  a v  a2s  . c  om
        if (!myFile.exists()) {
            myFile.createNewFile();
        }
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append(log);
        myOutWriter.close();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public synchronized static void writeXmlToFile(Document document, File outputFile)
        throws TransformerFactoryConfigurationError, TransformerException, IOException {
    if (document != null && outputFile != null) {
        FileOutputStream fos = new FileOutputStream(outputFile);
        OutputStreamWriter writer = new OutputStreamWriter(fos);
        writeXml(document, writer, null);
        writer.close();
    }//from  w  w  w .  j  a va  2s  . c  o  m
}

From source file:Main.java

public static void reboot() {
    try {/*ww w  .  j a  v a 2  s  .  c  o  m*/
        Process su = Runtime.getRuntime().exec("su");
        OutputStreamWriter out = new OutputStreamWriter(su.getOutputStream());
        out.write("reboot");
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static public void writeToFile(String filename, String content) {
    try {//from   w w  w.j a  va 2s  . c  om
        File file = new File(filename);
        OutputStream outputStream = new FileOutputStream(file);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        outputStreamWriter.write(content);
        outputStreamWriter.close();
    } catch (IOException e) {
        //         Log.e("Exception", "File write failed: " + e.toString());
    }
}

From source file:Main.java

public static void writeToPublicDirectory(String filename, String string, String directory,
        String environmentDirectory) throws Exception {
    File publicDirectory = new File(Environment.getExternalStoragePublicDirectory(environmentDirectory),
            directory);//ww  w.j  av a2  s .co m
    boolean result = publicDirectory.mkdirs();
    File file = new File(publicDirectory, filename);
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
    outputStreamWriter.write(string);
    outputStreamWriter.close();
}

From source file:Main.java

public static void writeToFile(String path, String data) {
    try {//ww  w. ja  v  a2  s  .c  om
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(path));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    } catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

From source file:networkedassets.Useful.java

public static String http(String obj, String addr, String method) throws IOException, JSONException {

    URL url = new URL(addr);
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
    httpCon.setDoOutput(true);/*from  w w w .  j a  v a 2 s.c o  m*/
    httpCon.setRequestMethod(method);
    OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
    out.write(obj);
    out.close();
    InputStream is = httpCon.getInputStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

        return readAll(rd);
    } finally {
        is.close();
    }
}

From source file:Main.java

public static JSONObject updateRequest(String query) {
    HttpURLConnection connection = null;
    try {//from   w ww .j a  v a  2s .  c o m
        connection = (HttpURLConnection) new URL(url + query).openConnection();
        connection.setRequestMethod("PUT");
        connection.setRequestProperty("Accept-Charset", charset);

        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("Resource content");
        out.close();

        statusCode = connection.getResponseCode();
        if (statusCode != 200) {
            return null;
        }

        InputStream response = connection.getInputStream();
        BufferedReader bR = new BufferedReader(new InputStreamReader(response));
        String line = "";

        StringBuilder responseStrBuilder = new StringBuilder();
        while ((line = bR.readLine()) != null) {
            responseStrBuilder.append(line);
        }
        response.close();
        return new JSONObject(responseStrBuilder.toString());

    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
    return new JSONObject();
}

From source file:Main.java

/**
 * Use a temporary file to obtain the name of the default system encoding
 * @return name of default system encoding, or null if write failed
 *//* www  . j a va 2 s  .c o m*/
private static String determineSystemEncoding() {
    File tempFile = null;
    String encoding = null;
    try {
        tempFile = File.createTempFile("gpsprune", null);
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile));
        encoding = getEncoding(writer);
        writer.close();
    } catch (IOException e) {
    } // value stays null
      // Delete temp file
    if (tempFile != null && tempFile.exists()) {
        if (!tempFile.delete()) {
            System.err.println("Cannot delete temp file: " + tempFile.getAbsolutePath());
        }
    }
    // If writing failed (eg permissions) then just ask system for default
    if (encoding == null)
        encoding = Charset.defaultCharset().name();
    return encoding;
}