Download presentation
Published byCurtis McCoy Modified over 9 years ago
1
2. I/O Text Files CCSA225 - Advanced Java Programming Sarah Alodan
8- Feb -2015 *These slides are largely based on Liang, Introduction to java Programming, Sixth Edition,2007 Pearson Education.
2
Objectives To understand how I/O is processed in Java.
To Distinguish between Text file & Binary File. To discover file/directory properties, to delete and rename files/directories, and to create directories using the File class . To write data to a file using the PrintWriter class. To read data from a file using the Scanner class . To understand how data is read using a Scanner. To develop a program that replaces text in a file.
3
What is File I/O? Reading and Writing Data.
An I/O Stream represents an input source or an output destination. – A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.
4
Files and Streams Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a file, such as an end-of-file marker or a count of the total bytes in the file that is recorded in a system maintained administrative data structure.
5
Files and Streams (cont.)
Streams that input and output bytes are known as byte-based streams, representing data in its binary format. Streams that input and output characters are known as character-based streams, representing data as a sequence of characters.
6
Text File vs Binary File
Files that are created using byte-based streams are referred to as binary files, while files created using character-based streams are referred to as text files. Text files can be read by text editors, while binary files are read by programs that understand the specific content of the file and the ordering of that content.
7
File Operations There are three basic operations that you will need to perform when you are working with files: 1-Open the file for input or output (for Reading or Writing ). 2-Process the file, by reading from or Writing to file. 3-Close the file
8
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.
9
Obtaining file properties and manipulating file
10
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.
11
Writing Data Using PrintWriter
12
Reading Data Using Scanner
13
Problem: 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.
14
Solution : Replacing Text (1)
15
Solution : Replacing Text (2)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.