Example usage for java.io Writer close

List of usage examples for java.io Writer close

Introduction

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

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream, flushing it first.

Usage

From source file:Main.java

public static void main(String[] args) {
    char[] c1 = { 'h', 'e', 'l', 'l', 'o' };
    char[] c2 = { 'w', 'o', 'r', 'l', 'd' };

    Writer writer = new PrintWriter(System.out);

    try {//w  ww.  j a v a 2 s.  c  o m
        // write a char array
        writer.write(c1);

        // flush the writer
        writer.flush();

        // write a new char array
        writer.write(c2);

        // flush the stream again
        writer.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String[] words = { "", "e", "a", "c" };
    Writer w = new BufferedWriter(new OutputStreamWriter(System.out, "Cp850"));
    for (int i = 0; i < 4; i++) {
        w.write(words[i] + " ");
    }//from  w w  w . j a v  a2s  . co m
    sortArray(Collator.getInstance(), words);
    for (int i = 0; i < 4; i++) {
        w.write(words[i] + " ");
    }
    w.flush();
    w.close();
}

From source file:Main.java

public static void main(String[] args) {
    String s = "from java2s.com";

    Writer writer = new PrintWriter(System.out);

    try {//  w  ww  .j av  a 2  s  . co m
        // append a string
        writer.append(s);

        // flush the writer
        writer.flush();

        // append a new string
        writer.append("\nThis is an example");

        // flush and close the stream
        writer.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    String s = "from java2s.com";

    Writer writer = new PrintWriter(System.out);

    try {//w  ww .ja  va 2 s.com
        // append a string
        writer.append(s);

        // flush the writer
        writer.flush();

        // append a new string in a new line
        writer.append("\nThis is an example");

        // flush the stream again
        writer.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    char[] c = { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };

    Writer writer = new PrintWriter(System.out);

    try {/*from w  ww.  j  a  v  a  2 s .  c  o m*/
        // write a portion of a char array
        writer.write(c, 0, 5);

        // flush the writer
        writer.flush();

        // write another portion of a char array
        writer.write(c, 5, 5);

        // flush the stream again
        writer.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:cat.tv3.eng.rec.recomana.lupa.visualization.ClustersToJson.java

public static void main(String[] args) throws IOException {

    String host = args[0];/*from ww w .  j  ava  2 s.c o m*/
    int port = Integer.parseInt(args[1]);
    Jedis jedis = new Jedis(host, port, 20000);

    // Cluster to binary tree visualitzation
    Map<String, String> attr_cluster = jedis.hgetAll("ClusterBinaryTree-Arrel");
    String cluster_name = attr_cluster.get("cluster_ids_name");

    JSONObject cluster;
    if (!cluster_name.equals("cluster_splited")) {
        cluster = new JSONObject();
        cluster.put("name", "arrel");
    } else {
        String id_left_centroid = attr_cluster.get("id_left_centroid");
        String id_right_centroid = attr_cluster.get("id_right_centroid");

        String hash_left = attr_cluster.get("hash_left");
        String hash_right = attr_cluster.get("hash_right");

        cluster = new JSONObject();
        cluster.put("name", "arrel");
        cluster.put("children", hashToJSONArrayRepresentationBinaryTree(id_left_centroid, hash_left, jedis,
                id_right_centroid, hash_right));
    }
    jedis.disconnect();

    Writer out = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream("data_toVisualize/cluster.json"), "UTF-8"));
    try {
        out.write(cluster.toJSONString());
    } finally {
        out.close();
    }
}

From source file:examples.nntp.post.java

public final static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;/*from   w w  w.j a v  a  2 s  .c o m*/
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0)
                break;

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0)
            header.addHeaderField("Organization", organization);

        if (references != null && organization.length() > 0)
            header.addHeaderField("References", references);

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

        client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        fileReader.close();

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:examples.nntp.PostMessage.java

public static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;// w  w  w  .  j av  a2  s.  c o m
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0) {
                break;
            }

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0) {
            header.addHeaderField("Organization", organization);
        }

        if (references != null && references.length() > 0) {
            header.addHeaderField("References", references);
        }

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

        client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        if (fileReader != null) {
            fileReader.close();
        }

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:com.zimbra.cs.db.MySQL.java

public static void main(String args[]) {
    // command line argument parsing
    Options options = new Options();
    CommandLine cl = Versions.parseCmdlineArgs(args, options);

    String outputDir = cl.getOptionValue("o");
    File outFile = new File(outputDir, "versions-init.sql");
    outFile.delete();//from  w w w  .  j ava2 s . co m

    try {
        String redoVer = com.zimbra.cs.redolog.Version.latest().toString();
        String outStr = "-- AUTO-GENERATED .SQL FILE - Generated by the MySQL versions tool\n" + "USE zimbra;\n"
                + "INSERT INTO zimbra.config(name, value, description) VALUES\n" + "\t('db.version', '"
                + Versions.DB_VERSION + "', 'db schema version'),\n" + "\t('index.version', '"
                + Versions.INDEX_VERSION + "', 'index version'),\n" + "\t('redolog.version', '" + redoVer
                + "', 'redolog version')\n" + ";\nCOMMIT;\n";

        Writer output = new BufferedWriter(new FileWriter(outFile));
        output.write(outStr);
        if (output != null)
            output.close();
    } catch (IOException e) {
        System.out.println("ERROR - caught exception at\n");
        e.printStackTrace();
        System.exit(-1);
    }
}

From source file:com.zimbra.cs.db.SQLite.java

public static void main(String args[]) {
    // command line argument parsing
    Options options = new Options();
    CommandLine cl = Versions.parseCmdlineArgs(args, options);

    String outputDir = cl.getOptionValue("o");
    File outFile = new File(outputDir, "versions-init.sql");
    outFile.delete();//w w w  .  j a v  a  2 s .c  o  m

    try {
        String redoVer = com.zimbra.cs.redolog.Version.latest().toString();
        String outStr = "-- AUTO-GENERATED .SQL FILE - Generated by the SQLite versions tool\n"
                + "INSERT INTO config(name, value, description) VALUES\n" + "\t('db.version', '"
                + Versions.DB_VERSION + "', 'db schema version');\n"
                + "INSERT INTO config(name, value, description) VALUES\n" + "\t('index.version', '"
                + Versions.INDEX_VERSION + "', 'index version');\n"
                + "INSERT INTO config(name, value, description) VALUES\n" + "\t('redolog.version', '" + redoVer
                + "', 'redolog version');\n";

        Writer output = new BufferedWriter(new FileWriter(outFile));
        output.write(outStr);
        output.close();
    } catch (IOException e) {
        System.out.println("ERROR - caught exception at\n");
        e.printStackTrace();
        System.exit(-1);
    }
}