Console Input/Output:
java.io.InputStream: stores information about the connection between an input device and the computer or program.
java.util.Scanner used to read the input available from InputStream object.
// 1. Create a Scanner using the InputStream available.
Scanner scanner = new Scanner( System.in );
// 2. Don't forget to prompt the user
System.out.print( "Type some data for the program: " );
// 3. Use the Scanner to read a line of text from the user.
String input = scanner.nextLine();
// 4. Now, you can do anything with the input string that you need to.
// Like, output it to the user.
System.out.println( "input = " + input );
// 1. Create a single shared Scanner object for keyboard input.
// This must be done in only one class of your program.
// All keyboard input must be handled through that one class.
private static Scanner scanner = new Scanner( System.in );
//1. Get a String of characters that is in an integer format, e.g., "123".
String input = scanner.nextLine(); // from console input example above.
//2. Use the Integer class to parse the string of characters into an integer.
int number = Integer.parseInt( input ); // converts a String into an int value
Here's another way to do this:
//Read the next available input as an int value.
int number = scanner.nextInt(); // from console input example above.
System.out.print(...)
System.out.println(...)
statements for displaying simple text messages to the user.
File Input/Output
java.io.File - stores information about a file on a computer drive.
//Here's a code fragment to illustrate //reading a text file:
System.out.print( "Enter the filename: " ); // Prompt the user for a file name
String fileName = scanner.nextLine(); // get a file name from the user
File file = new File( fileName ); // create a File object
if ( file.exists() ) // check that the file exists
{ // before trying to create a
// Scanner to read the file
// Create a Scanner from the file.
// This statement can cause a FileNotFoundException.
Scanner inFile = new Scanner( file );
// For each line in the file, read in the line and display it with the line number
int lineNum = 0;
// Use the results of calling the hasNext method to
// determine if you are at the end of the file before
// reading the next line of the file.
while ( inFile.hasNext() )
{
line = inFile.nextLine(); // read the next line
// Output the line read to the screen for the user
System.out.println( ++lineNum + ": " + line );
}
// When we're done reading the file,
// close the Scanner object attached to the file
inFile.close();
}
File output:
java.io.PrintStream class for file output.
//Here's an example file output:
// Create a PrintStream attached to a file named "out.txt".
// This will overwrite the file if it already exists
java.io.PrintStream ps = new java.io.PrintStream( "out.txt" );
// Buffer some data to write to the file (doesn't actually write until flush)
ps.print( "Some test data that will be written when flush is called.");
// Flush all buffered data to the file.
ps.flush();
// Buffer some more data.
ps.println( data );
// Close the file (by closing the PrintStream).
// Also flushes any remaining buffered output.
ps.close();