Example usage for java.io DataInputStream DataInputStream

List of usage examples for java.io DataInputStream DataInputStream

Introduction

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

Prototype

public DataInputStream(InputStream in) 

Source Link

Document

Creates a DataInputStream that uses the specified underlying InputStream.

Usage

From source file:Main.java

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

    URL google = new URL("http://www.google.com/");
    URLConnection googleConnection = google.openConnection();
    DataInputStream dis = new DataInputStream(googleConnection.getInputStream());
    StringBuffer inputLine = new StringBuffer();
    String tmp;/*from w ww  .j ava 2s .co m*/
    while ((tmp = dis.readLine()) != null) {
        inputLine.append(tmp);
        System.out.println(tmp);
    }
    // use inputLine.toString(); here it would have whole source
    dis.close();
}

From source file:EOF.java

public static void main(String args[]) {
    DataInputStream is = null;/*from w  w w  . j a  v  a 2 s.c o  m*/
    byte ch;
    try {
        is = new DataInputStream(new FileInputStream("EOF.java"));
        while (true) { // exception deals catches EOF
            ch = is.readByte();
            System.out.print((char) ch);
            System.out.flush();
        }
    } catch (EOFException eof) {
        System.out.println(" >> Normal program termination.");
    } catch (FileNotFoundException noFile) {
        System.err.println("File not found! " + noFile);
    } catch (IOException io) {
        System.err.println("I/O error occurred: " + io);
    } catch (Throwable anything) {
        System.err.println("Abnormal exception caught !: " + anything);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignored) {
            }
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    byte[] buf = { 1, 2, 3, 4, 5 };

    InputStream is = new ByteArrayInputStream(buf);

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        byte b = dis.readByte();
        System.out.print(b);//w w w  .  j a v a  2s  .  co  m
    }

}

From source file:Main.java

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

    byte[] buf = { 1, 2, 3, 4, 5 };

    InputStream is = new ByteArrayInputStream(buf);

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        System.out.println(dis.readBoolean());
    }//w  w w  .  j  a  v  a2 s  . c  om

}

From source file:MainClass.java

public static void main(String args[]) {
    try {//ww w.  java2  s.co  m

        FileInputStream fis = new FileInputStream("fileName.dat");

        // Create a data input stream
        DataInputStream dis = new DataInputStream(fis);

        // Read and display data
        System.out.println(dis.readBoolean());
        System.out.println(dis.readByte());
        System.out.println(dis.readChar());
        System.out.println(dis.readDouble());
        System.out.println(dis.readFloat());
        System.out.println(dis.readInt());
        System.out.println(dis.readLong());
        System.out.println(dis.readShort());

        // Close file input stream
        fis.close();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:Main.java

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

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

    DataInputStream dis = new DataInputStream(is);

    int count = is.available();

    byte[] bs = new byte[count];

    dis.read(bs);//from  ww w.j a  v a  2s .  com

    for (byte b : bs) {
        char c = (char) b;
        System.out.print(c);
    }

}

From source file:SocketDemo.java

public static void main(String args[]) throws Exception {
    String server = args[0];/*from  w  w  w.j  a  v a  2 s.com*/
    int port = Integer.parseInt(args[1]);
    Socket s = new Socket(server, port);

    InputStream is = s.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    System.out.println(dis.readInt());

    s.close();
}

From source file:Main.java

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

    URL url = new URL("http://www.java.com/");
    URLConnection urlConnection = url.openConnection();
    DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
    String html = "", tmp = "";
    while ((tmp = dis.readUTF()) != null) {
        html += " " + tmp;
    }//from w w  w . ja  v  a 2  s  . c om
    dis.close();

    html = html.replaceAll("\\s+", " ");
    Pattern p = Pattern.compile("<title>(.*?)</title>");
    Matcher m = p.matcher(html);
    while (m.find() == true) {
        System.out.println(m.group(1));
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    byte[] b = new byte[1];
    URL url = new URL("http://www.server.com/a.gif");
    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();// w  ww.  j av a2 s .  c o  m
    DataInputStream di = new DataInputStream(urlConnection.getInputStream());

    FileOutputStream fo = new FileOutputStream("a.gif");
    while (-1 != di.read(b, 0, 1))
        fo.write(b, 0, 1);
    di.close();
    fo.close();
}

From source file:PrimeReader.java

public static void main(String[] arguments) {
    try {//w w  w.ja va2 s  . com
        FileInputStream file = new FileInputStream("400primes.dat");
        BufferedInputStream buff = new BufferedInputStream(file);
        DataInputStream data = new DataInputStream(buff);

        try {
            while (true) {
                int in = data.readInt();
                System.out.print(in + " ");
            }
        } catch (EOFException eof) {
            buff.close();
        }
    } catch (IOException e) {
        System.out.println("Error - " + e.toString());
    }
}