1 / 8

class Decimal Format

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. File Input/Output.

alta
Download Presentation

class Decimal Format

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 PrintWritersend 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 = newPrintWriter(new FileWriter("a:\\prog.out"));

  8. Skeleton of I/O Program

More Related