Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 JAVA FILE INPUT/OUTPUT

Similar presentations


Presentation on theme: "CHAPTER 5 JAVA FILE INPUT/OUTPUT"— Presentation transcript:

1 CHAPTER 5 JAVA FILE INPUT/OUTPUT
CSC 238 – Object Oriented Programming

2 Introduction Data stored in a text file is represented in human-readable form. Data stored in a binary file is represented in binary form. Binary files cannot be read. They are designed to be read by programs. For example, Java source programs are stored in text files and can be read by a text editor, but Java classes are stored in binary files and are read by the JVM. Advantage of binary files is that they are more efficient to process than text files.

3 Introduction (cont.) You can imagine a text fileas consisting of a sequence of charactersand a binary fileas consisting of a sequence of bits. For example, the decimal integer 199 is stored as the sequence of three characters, „1‟, „9‟, „9‟ in a text file, and the same integer is stored as a byte- type value C7 in a binary file (hex , 199=12x161+7)

4 How is I/O handled in Java
To perform I/O, you need to create objects using appropriate Java I/O classes. The object contain the methods for reading/writing data from/to a file. There are many I/O classes for various purposes. These can be classified as input classes and output classes. An input class contains the methods to read data, and an output class contains the methods to write data

5 MAJOR TYPES OF FILE I/O Computers do not differentiate binary files and text files. All files are stored in binary format Text files A sequence of characters Easy for reading, modifying & writing a text file using a text editor Binary files A sequence of bits –data items consists of usually 8bit bytes More compact More efficient

6 Text I/O vs. Binary I/O Text I/O
Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding. Encoding and decoding are automatically performed for text I/O. The JVM converts a Unicode to a file-specific encoding when writing a character and converts a file-specific encoding to a Unicode when reading a character.

7 Text I/O vs. Binary I/O (cont.)
Binary I/O doest not require conversions. If you write a numeric value to a file using binary I/O, the exact value in the memory is copied into the file. When you read a byte using binary I/O, one byte value is read from the input. In general, use text input to read a file created by a text editor or a text output program, and use binary input to read a file created by a Java binary output program.

8 Text I/O vs. Binary I/O (cont.)
Binary I/O is more efficient than text I/O, because binary I/O does not require encoding and decoding. Binary files are independent of the encoding scheme on the host machine and thus are portable. Java programs on any machine can reada binary file created by a Java program.

9 Text I/O vs. Binary I/O (cont.)

10 JAVA FILE I/O I/O classes are listed in java.io package
For Java 5.0, the Scanner class can be used for text input Text files Known as character stream i/o. use 16 bit Unicode characters, making them platform independent. Use Reader and Writer classes and their subclasses Binary files Known as byte stream i/o. use 8 bit bytes, platform dependent. Use Input Stream and Output Stream classes and their subclasses.

11 JAVA TEXT FILE CLASSES & METHODS (cont.)
Relevant java text i/o classes Basics Create a File object and associated it to the file File inFile = newFile(“sample.txt”) -locate the file in the current directory Specify the path and file or directory name when call the constructor File inFile = newFile(“C:\\JavaProg”,sample.txt”); or File inFile = newFile(“C:\\JavaProg\\sample.txt”); or File inFile = newFile(“C:\\JavaProg\\fileio”);

12 JAVA TEXT FILE CLASSES & METHODS
INPUT Classes 1. File –encapsulates the properties of a file or a path & does not contain methods to read/writing data from/to a file 2. FileReader –convenience class for reading character files 3. BufferedReader –read text from a character input stream Methods 1. readLine() –read a string line; return a String 2. read() –read a single character; eof is -1 3. close() –close input stream

13 JAVA TEXT FILE CLASSES & METHODS(CONT)
INPUT public class FileReader–convenience class for reading character files public class BufferedReader– read text from a character input stream buffering characters so as to provide for the efficient reading of characters, arrays and lines The buffer size may be specified or the default size may be used

14 JAVA TEXT FILE CLASSES & METHODS(CONT)
INPUT(CONT) public class BufferedReader (cont.) Example: BufferedReader infile = new BufferedReader(new FileReader(“datafile.in”)); //read buffered input from textfile This code will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters and returned –inefficient

15 JAVA TEXT FILE CLASSES & METHODS(CONT)
STEP READING TEXT FILE:

16 JAVA TEXT FILE CLASSES & METHODS(CONT)
Read text file

17 JAVA TEXT FILE CLASSES & METHODS(CONT)
Other Example Read text file import java.io.*; public class numbers { public static void main(String [ ] args) throws IOException FileReader fr = new FileReader(“num.txt”); BufferedReader br = BufferedReader (fr); String line = br. readLine(); while(line != null) System.out.println(line); line = br.readLine( ); } br.close ( );

18 SCANNER CLASS

19 JAVA TEXT FILE CLASSES & METHODS(CONT)
Java 5.0 / j2sdk 1.5: Scanner class Using java.util package Is a simple text scanner which can parse primitive types and strings using regular expressions A Scanner breaks its input into token using delimeter pattern, which by default matches whitespace characters (blanks & certain nonprintable characters. Eg. New line & tab) The resulting tokens may then be converted into values of different types using various next methods

20 JAVA TEXT FILE CLASSES & METHODS(CONT)
Example of Scanner The default whitespace delimeter used by a scanner is recognized by Character.isWhitespace (eg. isWhitespace(.\n.) returns the value true) Methods used in Scanner: hasNext() - return true if this scanner has another token in its input hasNext<Wrapper>()-return true if this scanner has another token of type <Wrapper> in its input eg: hasNextInt()

21 JAVA TEXT FILE CLASSES & METHODS(CONT)
Example of Scanner

22 JAVA TEXT FILE CLASSES & METHODS(CONT)
Methods used in Scanner (cont.): next() –finds and returns the next complete token next<Wrapper>() -finds and returns the next complete token of type <Wrapper> eg: nextInt() close() –close the scanner

23 JAVA TEXT FILE CLASSES & METHODS(CONT)

24 CONTINUE…


Download ppt "CHAPTER 5 JAVA FILE INPUT/OUTPUT"

Similar presentations


Ads by Google