Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class Main {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.java2s.com/style/download.png");

        InputStream inputStream = url.openStream();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];

        int n = 0;
        while (-1 != (n = inputStream.read(buffer))) {
            output.write(buffer, 0, n);
        }
        inputStream.close();

        byte[] data = output.toByteArray();

        OutputStream out = new FileOutputStream("data.png");
        out.write(data);
        out.close();

        for (byte b : data) {
            System.out.printf("0x%x ", b);
        }
    }
}