import java.io.*; public class ReadFile { public static void main(String[] args) throws FileNotFoundException, IOException { File file = new File("example.txt"); InputStream inputStream = new FileInputStream(file); int data = inputStream.read(); while(data != -1) { System.out.println((char)data); data = inputStream.read(); } inputStream.close(); } }
import java.io.*; import java.net.URL; public class ReadUrl { public static void main(String[] args) throws IOException { URL url = new URL("https://www.example.com"); InputStream inputStream = url.openStream(); int data = inputStream.read(); while(data != -1) { System.out.print((char)data); data = inputStream.read(); } inputStream.close(); } }In this example, we're reading data from a URL using the openStream() method. We then read each byte of the data until we reach the end of the data, printing out each character as we go. The java.io.InputStream class is part of the Java Standard Library.