next up previous
Next: Output streams Up: Unit 09 Previous: Input/output streams

Input streams

Keyboard

We use the predefined object System.in of the class InputStream.

Example:

InputStream is = System.in;

File

We create an object of the class FileInputStream (subclass ofInputStream) associated to the File object that identifies the file to open for reading.

Example:

FileInputStream is = new FileInputStream("data.txt");

We could also create a File object starting from the filename, and use such an object to create the FileInputStream:

File f = new File("data.txt");
FileInputStream is = new FileInputStream(f);

Internet

We create an object of the class URL (defined in the librery java.net) that identifies the Internet resource we want to open for reading, and we invoke the method openStream(), which returns the corresponding InputStream object.

Example:

URL u = new URL("http://www.inf.unibz.it/");
InputStream is = u.openStream();


next up previous
Next: Output streams Up: Unit 09 Previous: Input/output streams