Example usage for java.io DataOutputStream writeChars

List of usage examples for java.io DataOutputStream writeChars

Introduction

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

Prototype

public final void writeChars(String s) throws IOException 

Source Link

Document

Writes a string to the underlying output stream as a sequence of characters.

Usage

From source file:Main.java

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

    String s = "Hello from java2s.com!!";

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");

    DataOutputStream dos = new DataOutputStream(fos);

    dos.writeChars(s);

    dos.flush();/*ww  w  .j a  v  a 2s  .c o  m*/

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        char c = dis.readChar();
        System.out.print(c);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/Chars.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    String str = "this is a test";
    dos.writeChars(str);
    dos.close();//from  w  ww.  ja v a  2  s .c  om
}

From source file:Main.java

public static void main(String[] atgs) throws Exception {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("java.txt")));
    out.writeUTF("");
    out.writeBytes("a");
    out.writeChars("aaa");
    out.close();/* ww w. j a va 2s.co m*/
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("java.txt")));
    System.out.println(in.readUTF());
    byte[] buf = new byte[1024];
    int len = in.read(buf);
    System.out.println(new String(buf, 0, len));
    in.close();
}

From source file:DataIODemo.java

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

    // write the data out
    DataOutputStream out = new DataOutputStream(new FileOutputStream("invoice1.txt"));

    double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
    int[] units = { 12, 8, 13, 29, 50 };
    String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };

    for (int i = 0; i < prices.length; i++) {
        out.writeDouble(prices[i]);// w  ww .j a va 2  s  .c om
        out.writeChar('\t');
        out.writeInt(units[i]);
        out.writeChar('\t');
        out.writeChars(descs[i]);
        out.writeChar('\n');
    }
    out.close();

    // read it in again
    DataInputStream in = new DataInputStream(new FileInputStream("invoice1.txt"));

    double price;
    int unit;
    StringBuffer desc;
    double total = 0.0;

    try {
        while (true) {
            price = in.readDouble();
            in.readChar(); // throws out the tab
            unit = in.readInt();
            in.readChar(); // throws out the tab
            char chr;
            desc = new StringBuffer(20);
            char lineSep = System.getProperty("line.separator").charAt(0);
            while ((chr = in.readChar()) != lineSep)
                desc.append(chr);
            System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price);
            total = total + unit * price;
        }
    } catch (EOFException e) {
    }
    System.out.println("For a TOTAL of: $" + total);
    in.close();
}

From source file:io.lavagna.model.CardFullWithCounts.java

private static String hash(CardFullWithCounts cwc) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream daos = new DataOutputStream(baos);

    try {//from   w ww.  j a  va2s  .  co  m
        // card
        daos.writeChars(Integer.toString(cwc.getId()));

        writeNotNull(daos, cwc.getName());
        writeInts(daos, cwc.getSequence(), cwc.getOrder(), cwc.getColumnId(), cwc.getUserId());
        // end card
        writeNotNull(daos, cwc.creationUser);
        writeNotNull(daos, cwc.creationDate);

        if (cwc.counts != null) {
            for (Map.Entry<String, CardDataCount> count : cwc.counts.entrySet()) {
                writeNotNull(daos, count.getKey());
                CardDataCount dataCount = count.getValue();
                daos.writeChars(Integer.toString(dataCount.getCardId()));
                if (dataCount.getCount() != null) {
                    daos.writeChars(Long.toString(dataCount.getCount().longValue()));
                }
                writeNotNull(daos, dataCount.getType());
            }
        }
        for (LabelAndValue lv : cwc.labels) {
            //
            writeInts(daos, lv.getLabelId(), lv.getLabelProjectId());
            writeNotNull(daos, lv.getLabelName());
            writeInts(daos, lv.getLabelColor());
            writeEnum(daos, lv.getLabelType());
            writeEnum(daos, lv.getLabelDomain());
            //
            writeInts(daos, lv.getLabelValueId(), lv.getLabelValueCardId(), lv.getLabelValueLabelId());
            writeNotNull(daos, lv.getLabelValueUseUniqueIndex());
            writeEnum(daos, lv.getLabelValueType());
            writeNotNull(daos, lv.getLabelValueString());
            writeNotNull(daos, lv.getLabelValueTimestamp());
            writeNotNull(daos, lv.getLabelValueInt());
            writeNotNull(daos, lv.getLabelValueCard());
            writeNotNull(daos, lv.getLabelValueUser());
        }
        daos.flush();

        return DigestUtils.sha256Hex(new ByteArrayInputStream(baos.toByteArray()));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:fiftyone.mobile.detection.webapp.JavascriptProvider.java

/**
 * Returns a base 64 encoded version of the hash for the core JavaScript
 * being returned./* ww w.  j a  v  a  2s.co m*/
 * @param dataSet providing the JavaScript properties.
 * @param request
 * @return 
 */
private static String eTagHash(Dataset dataSet, HttpServletRequest request)
        throws IOException, NoSuchAlgorithmException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeLong(dataSet.published.getTime());
    dos.writeChars(request.getHeader("User-Agent"));
    dos.writeChars(request.getQueryString());
    return Base64.encodeBase64String(MessageDigest.getInstance("MD5").digest(bos.toByteArray()));
}

From source file:KMeansNDimension.java

private static void solutionWriter(DataSet<Tuple2<Integer, DenseVector>> res) {
    File tT = new File("/Kmeans_results.csv");
    DataOutputStream wTT = null;
    try {/*from w  w w.ja  va  2s.c  o m*/
        wTT = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tT)));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        List<Tuple2<Integer, DenseVector>> tuples = res.collect();

        for (Tuple2<Integer, DenseVector> t : tuples) {
            try {
                wTT.writeChars((double) t.f0 + ","
                        + t.f1.toString().replace("DenseVector", "").replace("(", "").replace(")", "").trim()
                        + "\n");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

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

    //System.out.println(id+","+maxDensity+","+vector.toString());
}

From source file:net.sf.keystore_explorer.crypto.x509.X509ExtensionSet.java

private void saveExtensions(Map<String, byte[]> extensions, DataOutputStream dos) throws IOException {
    dos.writeInt(extensions.size());/*from  www  .  ja  v  a2 s .co m*/

    for (String oid : extensions.keySet()) {
        dos.writeInt(oid.length());
        dos.writeChars(oid);

        byte[] value = extensions.get(oid);
        dos.writeInt(value.length);
        dos.write(value);
    }
}

From source file:com.wsc.myexample.decisionForest.MyTestForest.java

private void testFile(String inPath, String outPath, DataConverter converter, MyDecisionForest forest,
        Dataset dataset, /*List<double[]> results,*/ Random rng, ResultAnalyzer analyzer) throws IOException {
    // create the predictions file
    DataOutputStream ofile = null;

    if (outPath != null) {
        ofile = new DataOutputStream(new FileOutputStream(outPath));
    }//from   ww w.  ja v  a  2  s  .c  o  m

    DataInputStream input = new DataInputStream(new FileInputStream(inPath));
    try {
        Scanner scanner = new Scanner(input);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.isEmpty()) {
                continue; // skip empty lines
            }

            Instance instance = converter.convert(line);
            if (instance == null)
                continue;

            double prediction = forest.classify(dataset, rng, instance);

            if (ofile != null) {
                ofile.writeChars(Double.toString(prediction)); // write the prediction
                ofile.writeChar('\n');
            }

            //        results.add(new double[] {dataset.getLabel(instance), prediction});

            analyzer.addInstance(dataset.getLabelString(dataset.getLabel(instance)),
                    new ClassifierResult(dataset.getLabelString(prediction), 1.0));
        }

        scanner.close();
    } finally {
        Closeables.closeQuietly(input);
        ofile.close();
    }
}

From source file:org.apache.hadoop.io.TestArrayOutputStream.java

private void runComparison(ArrayOutputStream aos, DataOutputStream dos, ByteArrayOutputStream bos)
        throws IOException {
    Random r = new Random();
    // byte/*from w w w  . ja  va2  s .  c  o m*/
    int b = r.nextInt(128);
    aos.write(b);
    dos.write(b);

    // byte[]
    byte[] bytes = new byte[10];
    r.nextBytes(bytes);
    aos.write(bytes, 0, 10);
    dos.write(bytes, 0, 10);

    // Byte
    aos.writeByte(b);
    dos.writeByte(b);

    // boolean
    boolean bool = r.nextBoolean();
    aos.writeBoolean(bool);
    dos.writeBoolean(bool);

    // short
    short s = (short) r.nextInt();
    aos.writeShort(s);
    dos.writeShort(s);

    // char
    int c = r.nextInt();
    aos.writeChar(c);
    dos.writeChar(c);

    // int
    int i = r.nextInt();
    aos.writeInt(i);
    dos.writeInt(i);

    // long
    long l = r.nextLong();
    aos.writeLong(l);
    dos.writeLong(l);

    // float
    float f = r.nextFloat();
    aos.writeFloat(f);
    dos.writeFloat(f);

    // double
    double d = r.nextDouble();
    aos.writeDouble(d);
    dos.writeDouble(d);

    // strings
    String str = RandomStringUtils.random(20);
    aos.writeBytes(str);
    aos.writeChars(str);
    aos.writeUTF(str);
    dos.writeBytes(str);
    dos.writeChars(str);
    dos.writeUTF(str);

    byte[] expected = bos.toByteArray();
    assertEquals(expected.length, aos.size());

    byte[] actual = new byte[aos.size()];
    System.arraycopy(aos.getBytes(), 0, actual, 0, actual.length);
    // serialized bytes should be the same
    assertTrue(Arrays.equals(expected, actual));
}