Unformatted text preview:

Strings and File I O Strings Java String objects are immutable Common methods include boolean equalsIgnoreCase String str String toLowerCase String substring int offset int endIndex String replace char oldChar char newChar int indexOf String str File Input Text Option 1 Scanner File Scans input from given File Input from file instead of System in keyboard Easy to scan ints doubles etc Option 2 BufferedReader FileReader BufferedReader allows you to read a line at a time instead of a character File Input Text Option 1 Scanner Scanner scan new Scanner new File myfile txt String s while scan hasNext acts like an iterator s scan nextLine Option 2 Buffered Reader BufferedReader in new BufferedReader new FileReader myfile txt s in readLine returns null when EOF reached while s null s in readLine in close remember to CLOSE the file Tips Scanner must be imported from java util File and Readers must be imported from java io Must deal with IOException Use try catch for file operations or declare throws IOException in method header public static void main String args throws IOException File Output Text FileWriter allows you to write to a file PrintWriter provides interface of System out Remember to import correct packages and handle exceptions PrintWriter out new PrintWriter new FileWriter myfile txt out println String 1 out println String 2 out close remember to CLOSE the file Misc Path names Relative path name starts looking in current directory Examples myfile txt mydirectory myfile txt Absolute path name starts from top level directory Examples home srollins cs112 myfile txt C srollins cs112 myfile txt Binary Files FileInputStream FileOutputStream read write bytes


View Full Document

USF CS 112 - Strings and File I/O

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Loading Unlocking...
Login

Join to view Strings and File I/O and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Strings and File I/O and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?