Strings and File I/O.

Slides:



Advertisements
Similar presentations
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
Advertisements

File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Files and Streams CS 21a Chapter 11 of Horstmann.
1 CS2200 Software Development Lecture 36: File Processing A. O’Riordan, 2008 (Includes slides by Lewis/Loftus 2205 and K. Brown )
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
String Tokenization What is String Tokenization?
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Java Review 2. The Agenda The following topics were highlighted to me as issues: –File IO (Rem) –Wrappers (Rem) –Interfaces (Rem) –Asymptotic Notation.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Two Ways to Store Data in a File Text format Binary format.
Chapter 10 Exceptions and File I/O. © 2004 Pearson Addison-Wesley. All rights reserved10-2 Exceptions Exception handling is an important aspect of object-oriented.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
File IO Basics By Dan Fleck Coming up: Data Streams.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
File Input / Output.
File I/O CLI: File Input CLI: File Output GUI: File Chooser
Lesson 8: More File I/O February 5, 2008
Introduction to programming in java
Reading from a file and Writing to a file
Streams and File I/O.
File Input and Output TOPICS File Input Exception Handling File Output.
12: The Java I/O System stream model.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Computer Programming Methodology File Input
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
I/O Streams- Basics Byte Streams and Character Streams
File Input and Output TOPICS File Input Exception Handling File Output.
Streams and File I/O Chapter 14.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Reading and Writing Text Files
CS 302 Week 13 Jim Williams, PhD.
CSC1401 Input and Output (with Files)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
EEC 484/584 Computer Networks
Java IO Packages Prepared by Mrs.S.Amudha AP/SWE
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Presentation transcript:

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 Option 2: Buffered Reader 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 Binary Files 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