Download presentation
Presentation is loading. Please wait.
1
class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat DecimalFormat twoDecimal = new DecimalFormat("0.00"); twoDecimal.format(56.379); ► Result: 56.38
2
File Input/Output ► File: area in secondary storage used to hold information ► class FileReader is used to input data from a file ► class FileWriter and class PrintWriter send output to files
3
File Input/Output ► Java file I/O process: 1. Import classes from package java.io 2. Declare and associate appropriate variables with I/O sources 3. Use appropriate methods with declared variables 4. Close output file
4
Reading from System.in ► System.in is a predefined input data stream that is by default assigned to the keyboard ► System.in is an object of type InputStream ► Remember to import necessary java.io classes BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in) ); ► Note that BufferedReader( Reader inputReader) InputStreamReader( InputStream inputStream)
5
Inputting (Reading) Data from a File ► Import Necessary classes from java.io ► Declare and associate variables with the input sources: Create BufferedReader Object to read entire line Use class FileReader Specify file name and location ► Use appropriate method to input data ReadLine() BufferedReader inFile = new BufferedReader(new FileReader("a:\\prog.dat"));
6
Writing to System.out ► System.out is a predefined output data stream object that is by default assigned to the console ► System.out is an object of type PrintStream ► Information is printed to the console using the methods below (associated with System.out) System.out.println( stringExp ) System.out.print( stringExp ) System.out.flush( stringExp )
7
Outputting (Printing) Data to a File ► Import Necessary classes from java.io ► Declare and associate variables with the output sources: Create PrintWriter Object Use classes FileWriter and PrintWriter Specify file name and location ► Use appropriate methods to input data outFile.println, outFile.print, outFile.flush ► Close output file outFile.close() PrintWriter outFile = new PrintWriter(new FileWriter("a:\\prog.out"));
8
Skeleton of I/O Program
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.