Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1 File I/O in java chapter.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
1 Chapter 3 Strings and Text I/O. 2 Objectives F To use the String class to process fixed strings. F To use the Character class to process a single character.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
2. I/O Text Files CCSA225 - Advanced Java Programming Sarah Alodan
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
INF120 Basics in JAVA Programming AUBG, COS dept, Fall semester 2013 Reference books: Malik D.S., Java Programming, From Problem Analysis to Program Design,
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 12 Exception Handling and Text.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Exception Handling and Text.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
1 Chapter 9 Strings and Text I/O. 2 Objectives F To use the String class to process fixed strings. F To use the Character class to process a single character.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Files in Java 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 9 Strings.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS 112 Programming 2 Lecture 08 Exception Handling & Text I/O (1)
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Chapter 9 Strings and Text I/O
Chapter 12 Exceptions and File Input/Output
Strings.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 8 Strings and Text I/O
Chapter 9 Strings and Text I/O
Introduction to Classes and Methods
Chapter 9 Strings and Text I/O
A+ Computer Science INPUT.
Text I/O.
Unit-2 Objects and Classes
Chapter 11 Exception Handling and Text I/O
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
Chapter 12 Exception Handling and Text IO
Chapter 9 Strings and Text I/O
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO Part 2
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Presentation transcript:

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved File I/O in java chapter 8 (pages ) Using File class from java.io.File java.util.Scanner class to read from file java.util.PrintWriter to write to file The java.util.PrintWriter and java.util.Scanner classes are higher-level and easy to use. All the programs written using the lower-level text I/O classes can be revised using the PrintWriter and Scanner classes. Therefore, the PrintWriter and Scanner classes supersede the lower-level text I/O classes.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Pedagogical NOTE The previous edition of this book introduced text I/O using many subclasses of java.io.Writer and java.io.Reader. These classes are lower-level and difficult to learn. The java.util.PrintWriter and java.util.Scanner classes are higher-level and easy to use. All the programs written using the lower-level text I/O classes can be revised using the PrintWriter and Scanner classes. Therefore, the PrintWriter and Scanner classes supersede the lower-level text I/O classes. If students need to know these lower-level text I/O classes, please refer to Supplement V.I, “Text I/O Using Reader and Writer.”

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved The File Class The File class is intended to provide an abstraction that deals with most of the machine-dependent complexities of files and path names in a machine-independent fashion. The filename is a string. The File class is a wrapper class for the file name and its directory path.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Obtaining file properties and manipulating file

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Example: Using the File Class TestFileClass Run Objective: Write a program that demonstrates how to create files in a platform-independent way and use the methods in the File class to obtain their properties. Figure 16.1 shows a sample run of the program on Windows, and Figure 16.2 a sample run on Unix.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Text I/O A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file. In order to perform I/O, you need to create objects using appropriate Java I/O classes. The objects contain the methods for reading/writing data from/to a file. This section introduces how to read/write strings and numeric values from/to a text file using the Scanner and PrintWriter classes.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Writing Data Using PrintWriter to write to a file WriteData Run

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Reading Data Using Scanner ReadData Run

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Example: Replacing Text Write a class named ReplaceText that replaces a string in a text file with a new string. The filename and strings are passed as command-line arguments as follows: java ReplaceText sourceFile targetFile oldString newString For example, invoking java ReplaceText FormatString.java t.txt StringBuilder StringBuffer replaces all the occurrences of StringBuilder by StringBuffer in FormatString.java and saves the new file in t.txt. ReplaceText Run

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved The Scanner Class The delimiters are single characters in StringTokenizer. You can use the new JDK 1.5 java.util.Scanner class to specify a word as a delimiter. JDK 1.5 Feature String s = "Welcome to Java! Java is fun! Java is cool!"; Scanner scanner = new Scanner(s); scanner.useDelimiter("Java"); while (scanner.hasNext()) System.out.println(scanner.next()); Creates an instance of Scanner for the string. Sets “Java” as a delimiter. hasNext() returns true if there are still more tokens left. The next() method returns a token as a string. Welcome to ! is fun! is cool! Output

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Scanning Primitive Type Values If a token is a primitive data type value, you can use the methods nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), or nextBoolean() to obtain it. For example, the following code adds all numbers in the string. Note that the delimiter is space by default. JDK 1.5 Feature String s = " "; Scanner scanner = new Scanner(s); int sum = 0; while (scanner.hasNext()) sum += scanner.nextInt(); System.out.println("Sum is " + sum);

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Console Input Using Scanner Another important application of the Scanner class is to read input from the console. For example, the following code reads an int value from the keyboard: JDK 1.5 Feature System.out.print("Please enter an int value: "); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); NOTE: StringTokenizer can specify several single characters as delimiters. Scanner can use a single character or a word as the delimiter.