Hey, Remember Java? Part 2 Written by J.J. Shepherd
File I/O Reading from a file works much like reading from the keyboard using a Scanner Concept Must be in a try-catch block Must close the stream at the end Scanner inputStream = new Scanner( new File(“./turtles.txt”)); int turtleAmt = inputStream.nextInt(); inputStream.close();
File I/O Writing to a file works much like System.out Concept for setting up Must be in a try-catch block Must close the file at the end PrintWriter outputStream = new PrintWriter( new FileOutputstream(fileName, true)); outputStream.println(“Hello World”); outputStream.close();
String Manipulation Used heavily in File I/O and throughout this course This is used to analyze and potentially modify strings
String Manipulation Use the methods in the following slides to assist The only one that is missing is.contains which will return a true or false value depending if the string contains that string anywhere More information on strings can be found in the javadoc lang/String.html
String Manipulation When in doubt… Read it character by character using charAt If you truly do not understand regular expressions (REGEX’s) then avoid using methods that use them