Created
November 9, 2008 09:12
-
-
Save tatey/23229 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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