Jump to content

Java Programming/Basic IO: Difference between revisions

From Wikibooks, open books for an open world
[unreviewed revision][unreviewed revision]
Content deleted Content added
No edit summary
m Genesis
Line 1: Line 1:
This section covers the Java platform classes used for '''basic input and output'''. But before we begin we need to have a concrete understanding of what input and output means in programming. To grasp this concept, think of the Java platform as a ''system''.
{{cleanup-nc}}


==Understanding input and output==
==Basics==
The Java platform is an isolated entity, a space on your OS in a way, where everything outside this system is its ''environment''. The interaction between the system and its environment is a two-way dialogue of sorts. Either the system receives messages from its environment, or it conveys its messages to the same. When a message is received to the system, it is called an ''[[w:Input|input]]'', its opposite is an ''[[w:Output|output]]''. On a whole, this communication is termed ''[[w:Input/output|input/output]]'' abbreviated as ''I/O''.
All Java input/output is handled through subclasses of the [http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html InputStream]and [http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html OutputStream] classes. By default, Java provides an InputStream (System.in) from the keyboard and an OutputStream (System.out) to the screen.

===Java Output===
====Writing to the Screen====
Writing to the screen is very easy, and can be accomplished using one of two methods:
* System.out.print("Hello world") will print "Hello world" (no quotes) to the screen without advancing to a new line.
* System.out.println("Hello world") will print "Hello world" (no quotes) to the screen and advance to a new line.

===Java Input===
====Inputting from the keyboard====
As of version 1.5.0, Java provides a class in the java.util package called Scanner that simplifies keyboard input.

Scanner kbdIn = new Scanner(System.in); //Instantiating a new Scanner object
System.out.print("Enter your name: "); //Printing out the prompt
String name = kbdIn.nextLine(); //Reading a line of input (until the user hits enter) from the keyboard and putting it in a String variable called name
System.out.println("Welcome, " + name); //Printing out welcome, followed by the user's name

Alternatively, one could write a method to handle keyboard input:

public String readLine()
{ BufferedReader x = new BufferedReader(new InputStreamReader(System.in)); //Creates a new BufferedReader object
return x.readLine(); //Reads a line of input and returns it directly
}
Note that the code above shouldn't be used in most applications, as it creates new Objects every time the method is run.
A better alternative would be to create a separate class file to handle keyboard input.

[[Category:Java]]

Revision as of 02:27, 8 May 2008

This section covers the Java platform classes used for basic input and output. But before we begin we need to have a concrete understanding of what input and output means in programming. To grasp this concept, think of the Java platform as a system.

Understanding input and output

The Java platform is an isolated entity, a space on your OS in a way, where everything outside this system is its environment. The interaction between the system and its environment is a two-way dialogue of sorts. Either the system receives messages from its environment, or it conveys its messages to the same. When a message is received to the system, it is called an input, its opposite is an output. On a whole, this communication is termed input/output abbreviated as I/O.