class Decimal Format - PowerPoint PPT Presentation

About This Presentation
Title:

class Decimal Format

Description:

Import package java.text. Create DecimalFormat ... Import classes from package java.io ... Remember to import necessary java.io classes. BufferedReader keyboard ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 9
Provided by: janice67
Category:

less

Transcript and Presenter's Notes

Title: class Decimal Format


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
  • Import classes from package java.io
  • Declare and associate appropriate variables with
    I/O sources
  • Use appropriate methods with declared variables
  • 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
Write a Comment
User Comments (0)
About PowerShow.com