Skip to content

Instantly share code, notes, and snippets.

@tatey
Created November 9, 2008 09:12
Show Gist options
  • Save tatey/23229 to your computer and use it in GitHub Desktop.
Save tatey/23229 to your computer and use it in GitHub Desktop.
// Read input from the console
public void readFromConsole()
{
Scanner console = new Scanner(System.in);
while (console.hasNext())
{
// Do something
}
console.close();
}
/*
Read input from an arbitary file.
Reading from a file MUST be caught, else the compiler will have a heart attack.
You can't say with 100% certainty that the file will always be there. Note: there's
other exceptions you can catch besides the FileNotFoundException.
*/
public void readFromFile()
{
try
{
Scanner file = new Scanner(new File("file.txt"));
while (file.hasNextLine())
{
// Do something
}
file.close();
}
catch (FileNotFoundException exception)
{
// Handle the exception
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment