Example usage for java.io InputStream read

List of usage examples for java.io InputStream read

Introduction

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

Prototype

public abstract int read() throws IOException;

Source Link

Document

Reads the next byte of data from the input stream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream in = System.in;
    BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));

    int letter;/*from   ww  w .j a  v  a  2 s .  c om*/

    while ((letter = in.read()) != -1) {
        bw.write((char) letter);
        bw.flush();
    }
}

From source file:MainClass.java

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

    String hostname = "time.nist.gov";
    int port = 37;

    InputStream raw = null;
    Socket theSocket = new Socket(hostname, port);
    raw = theSocket.getInputStream();//www.j  av  a 2  s.  co  m

    System.out.println(raw.read());

    raw.close();
}

From source file:MainClass.java

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

    InputStream in = null;
    try {/*from  www.  j a  v a2 s  . c  o m*/
        URL u = new URL("http://www.java2s.com");
        in = u.openStream();
        for (int c = in.read(); c != -1; c = in.read()) {
            System.out.write(c);
        }
        in.close();
    } catch (MalformedURLException ex) {
        System.err.println("not a URL Java understands.");
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    int c;//from  w  w w .j  av  a 2s  . c  o m
    URL hp = new URL("http://www.internic.net");
    URLConnection hpCon = hp.openConnection();

    long d = hpCon.getDate();
    if (d == 0)
        System.out.println("No date information.");
    else
        System.out.println("Date: " + new Date(d));

    System.out.println("Content-Type: " + hpCon.getContentType());

    d = hpCon.getExpiration();
    if (d == 0)
        System.out.println("No expiration information.");
    else
        System.out.println("Expires: " + new Date(d));

    d = hpCon.getLastModified();
    if (d == 0)
        System.out.println("No last-modified information.");
    else
        System.out.println("Last-Modified: " + new Date(d));

    int len = hpCon.getContentLength();
    if (len == -1)
        System.out.println("Content length unavailable.");
    else
        System.out.println("Content-Length: " + len);

    if (len != 0) {
        InputStream input = hpCon.getInputStream();
        int i = len;
        while (((c = input.read()) != -1)) { // && (--i > 0)) {
            System.out.print((char) c);
        }
        input.close();

    } else {
        System.out.println("No content available.");
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("C://test.txt");

    // read and print characters one by one
    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());

    // mark is set on the input stream
    is.mark(0);//from  ww w  .  j a v a2s  . c  o  m

    System.out.println("Char : " + (char) is.read());
    System.out.println("Char : " + (char) is.read());

    if (is.markSupported()) {
        // reset invoked if mark() is supported
        is.reset();
        System.out.println("Char : " + (char) is.read());
        System.out.println("Char : " + (char) is.read());
    }
    is.close();
}

From source file:MainClass.java

public static void main(String[] args) {

    String hostname = "time.nist.gov";

    try {/*  www  . j a  v  a2 s  . co m*/
        Socket theSocket = new Socket(hostname, 13);
        InputStream timeStream = theSocket.getInputStream();
        StringBuffer time = new StringBuffer();
        int c;
        while ((c = timeStream.read()) != -1)
            time.append((char) c);
        String timeString = time.toString().trim();
        System.out.println("It is " + timeString + " at " + hostname);
    } // end try
    catch (UnknownHostException ex) {
        System.err.println(ex);
    } catch (IOException ex) {
        System.err.println(ex);
    }

}

From source file:Main.java

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

    int i;/*from  w  w w . j ava2s.c o m*/

    // new input stream created
    InputStream is = new FileInputStream("C://test.txt");

    System.out.println("Characters printed:");

    // reads till the end of the stream
    while ((i = is.read()) != -1) {
        // converts integer to character
        char c = (char) i;

        System.out.print(c);
    }
    is.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table images (Id int, b BLOB);");

    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)");
    ps.setString(1, "10");
    ps.setBinaryStream(2, fis);//from   w  w w. j a v a 2 s  .c  o m
    ps.executeUpdate();

    ResultSet rset = st.executeQuery("select b from images");
    InputStream stream = rset.getBinaryStream(1);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int a1 = stream.read();
    while (a1 >= 0) {
        output.write((char) a1);
        a1 = stream.read();
    }
    Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
    output.close();

    ps.close();

    fis.close();
    st.close();
    conn.close();
}

From source file:com.mtea.macrotea_httpclient_study.ClientConnectionRelease.java

public final static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {/*from  w  ww. j av a  2s. co m*/
        HttpGet httpget = new HttpGet("http://www.apache.org/");

        // Execute HTTP request
        System.out.println("executing request " + httpget.getURI());
        HttpResponse response = httpclient.execute(httpget);

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        System.out.println("----------------------------------------");

        HttpEntity entity = response.getEntity();

        //?????
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {
                instream.read();
            } catch (IOException ex) {
                //IOExceptionConnectionManager
                throw ex;
            } catch (RuntimeException ex) {
                //RuntimeExceptionhttpget.abort();
                httpget.abort();
                throw ex;
            } finally {
                // instream.close() ?ConnectionManager
                try {
                    instream.close();
                } catch (Exception ignore) {
                }
            }
        }

    } finally {
        //??httpclient???
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:Main.java

public static void main(String[] args) {
    byte[] b = { 'h', 'e', 'l', 'l', 'o' };
    try {/*from w  w w .j  a  v a 2 s  . co  m*/

        OutputStream os = new FileOutputStream("test.txt");

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

        os.write(b, 0, 3);

        for (int i = 0; i < 3; i++) {
            System.out.print((char) is.read());
        }
        os.close();
        is.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}