90 likes | 314 Views
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.
E N D
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 • 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
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
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)
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"));
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 )
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"));