Example usage for java.io OutputStreamWriter OutputStreamWriter

List of usage examples for java.io OutputStreamWriter OutputStreamWriter

Introduction

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

Prototype

public OutputStreamWriter(OutputStream out, CharsetEncoder enc) 

Source Link

Document

Creates an OutputStreamWriter that uses the given charset encoder.

Usage

From source file:Converter.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream(new File("input.txt"));
    BufferedReader in = new BufferedReader(new InputStreamReader(fis, "SJIS"));

    FileOutputStream fos = new FileOutputStream(new File("output.txt"));
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));

    int len = 80;
    char buf[] = new char[len];

    int numRead;// ww  w .j av  a  2s .c o  m
    while ((numRead = in.read(buf, 0, len)) != -1)
        out.write(buf, 0, numRead);
    out.close();
    in.close();
}

From source file:MainClass.java

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

    String serverName = System.getProperty("WHOIS_SERVER", DEFAULT_HOST);

    InetAddress server = null;// w ww .j  av a  2s.  c  o m
    server = InetAddress.getByName(serverName);
    Socket theSocket = new Socket(server, DEFAULT_PORT);
    Writer out = new OutputStreamWriter(theSocket.getOutputStream(), "8859_1");
    out.write("\r\n");
    out.flush();
    InputStream raw = theSocket.getInputStream();
    InputStream in = new BufferedInputStream(theSocket.getInputStream());
    int c;
    while ((c = in.read()) != -1)
        System.out.write(c);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileInputStream is = new FileInputStream("your.keystore");

    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(is, "my-keystore-password".toCharArray());

    String alias = "myalias";
    Certificate cert = keystore.getCertificate(alias);

    File file = null;/*from  ww  w.  ja  v  a 2 s .  co  m*/
    byte[] buf = cert.getEncoded();

    FileOutputStream os = new FileOutputStream(file);
    os.write(buf);
    os.close();

    Writer wr = new OutputStreamWriter(os, Charset.forName("UTF-8"));
    wr.write(new sun.misc.BASE64Encoder().encode(buf));
    wr.flush();

}

From source file:ASCII2NATIVE.java

public static void main(String[] args) {
    File f = new File("c:\\mydb.script");
    File f2 = new File("c:\\mydb3.script");
    if (f.exists() && f.isFile()) {
        // convert param-file
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer();
        String line;//w  w w. jav a  2  s  .c o m

        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "JISAutoDetect"));

            while ((line = br.readLine()) != null) {
                System.out.println(ascii2Native(line));
                sb.append(ascii2Native(line)).append(";\n");//.append(";\n\r")
            }

            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f2), "utf-8"));
            out.append(sb.toString());
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            System.err.println("file not found - " + f);
        } catch (IOException e) {
            System.err.println("read error - " + f);
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (Exception e) {
            }
        }
    } else {
        // // convert param-data
        // System.out.print(ascii2native(args[i]));
        // if (i + 1 < args.length)
        // System.out.print(' ');
    }
}

From source file:json_csv.JSON2CSV.java

public static void main(String myHelpers[]) {

    // These code snippets use an open-source library. http://unirest.io/java
    ObjectMapper mapper = new ObjectMapper();
    JSON2CSV run = new JSON2CSV();

    try {/* ww  w.ja va  2  s . c  o  m*/

        run.ibw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ingredient.csv"), "UTF-8"));
        run.rbw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("recipe.csv"), "UTF-8"));
        run.cbw = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream("cuisineRecipe.csv"), "UTF-8"));

        for (int i = 0; i < 100; i++) {

            // These code snippets use an open-source library. http://unirest.io/java
            HttpResponse<JsonNode> response = Unirest.get(
                    "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/random?limitLicense=false&number=100")
                    .header("X-Mashape-Key", "dkK9bOKEkNmshOLDuw9rHq59F0Twp1fvyxcjsn99AoUm6XvMfC")
                    .header("Accept", "application/json").asJson();

            // retrieve the parsed JSONObject from the response
            JSONObject myObj = response.getBody().getObject();

            JSONArray results = myObj.getJSONArray("recipes");

            run.parseRecipe(results);

            run.printRecipeToCSV();

            run.printCuisineLookupTable(run.recipes);
        }

        run.rbw.flush();
        run.rbw.close();

        run.ibw.flush();
        run.ibw.close();

        run.cbw.flush();
        run.cbw.close();

    } catch (UnirestException ue) {

        ue.printStackTrace();

    } catch (JSONException e) {

        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.lieuofs.extraction.etatpays.ExtractionPays.java

/**
 * @param args/*from  w  w  w .jav  a2  s  . co m*/
 */
public static void main(String[] args) throws IOException {
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "beans_lieuofs.xml" });
    EtatTerritoireCritere critere = new EtatTerritoireCritere();
    //critere.setEstEtat(Boolean.FALSE);

    EtatTerritoireDao dao = (EtatTerritoireDao) context.getBean("etatTerritoireDao");
    Set<EtatTerritoirePersistant> etats = dao.rechercher(critere);

    EtatWriter etatWriter = new NumOFSEtatWriter();
    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream("ExtractionPaysOFSReconnuRecemment.txt"), "UTF-8"));

    List<EtatTerritoirePersistant> listeTriee = new ArrayList<EtatTerritoirePersistant>(etats);
    Collections.sort(listeTriee, new Comparator<EtatTerritoirePersistant>() {

        @Override
        public int compare(EtatTerritoirePersistant o1, EtatTerritoirePersistant o2) {
            //return o1.getFormeCourte("fr").compareTo(o2.getFormeCourte("fr"));
            return o1.getNumeroOFS() - o2.getNumeroOFS();
        }

    });

    for (EtatTerritoirePersistant etat : filtre(listeTriee)) {
        String etatStr = etatWriter.ecrireEtat(etat);
        if (null != etatStr) {
            writer.append(etatStr);
        }
    }
    writer.close();
}

From source file:org.lieuofs.extraction.commune.historisation.ExtractionNumHistorisationCommune.java

/**
 * @param args/*from   www. jav  a2  s .c om*/
 */
public static void main(String[] args) throws IOException {
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "beans_lieuofs.xml" });
    CommuneCritere critere = new CommuneCritere();
    IGestionCommune gestionnaire = (IGestionCommune) context.getBean("gestionCommune");
    List<ICommuneSuisse> communes = gestionnaire.rechercher(critere);
    Collections.sort(communes, new Comparator<ICommuneSuisse>() {
        public int compare(ICommuneSuisse com1, ICommuneSuisse com2) {
            return com1.getNumeroOFS() - com2.getNumeroOFS();
        }
    });

    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream("NumeroHistorisationCommune.sql"), "Windows-1252"));
    NumeroHistorisationCommuneWriter commWriter = new NumeroHistorisationRefonteSQLWriter();
    for (ICommuneSuisse commune : communes) {
        String numHistStr = commWriter.ecrireNumero(commune);
        if (null != numHistStr) {
            writer.append(numHistStr);
        }
    }
    writer.close();

}

From source file:com.cncounter.test.httpclient.ProxyTunnelDemo.java

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

    ProxyClient proxyClient = new ProxyClient();
    HttpHost target = new HttpHost("www.google.com", 80);
    HttpHost proxy = new HttpHost("localhost", 1080);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd");
    Socket socket = proxyClient.tunnel(proxy, target, credentials);
    try {//from   ww w .  j a v  a  2 s  . c  om
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Agent: whatever\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } finally {
        socket.close();
    }
}

From source file:com.dlmu.heipacker.crawler.client.ProxyTunnelDemo.java

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

    ProxyClient proxyClient = new ProxyClient();
    HttpHost target = new HttpHost("www.yahoo.com", 80);
    HttpHost proxy = new HttpHost("localhost", 8888);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd");
    Socket socket = proxyClient.tunnel(proxy, target, credentials);
    try {//w  w w  .ja va  2s  .c  o  m
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Agent: whatever\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } finally {
        socket.close();
    }
}

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   w  w  w  .  j av  a2  s .co  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();
    }
}