Professional Documents
Culture Documents
import java.io.* ;
class WriteFile
{
public static void main( String[] args )
{
try
{
FileWriter file = new FileWriter( "tam.txt" );
BufferedWriter buffer = new BufferedWriter( file );
buffer.write("The wind blew as if it had blown its last");
buffer.newLine();
buffer.write("The rattling showers rose on is blast");
buffer.newLine();
buffer.write("The speedy gleams the darkness swallowed");
buffer.newLine();
buffer.write("Loud, deep and long the thunder bellowed");
buffer.newLine();
buffer.write("That night a child might understand");
buffer.newLine();
buffer.write("The devil had business on his hand.");
}
}
buffer.close();
}
catch( IOException e )
{
System.out.println( "A write error has occurred." );
}
Sort
import java.util.Arrays ;
class Sort
{
public static void main( String[] args )
{
String[] names = { "Mike", "Dave", "Andy" } ;
int[] nums = { 200, 300, 100 } ;
display( names );
display( nums );
Arrays.sort( names );
Arrays.sort( nums );
display( names );
display( nums );
ReadString
import java.io.*;
class ReadString
{
public static void main (String[] args)
{
System.out.print( "Enter the title of this book: " );
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader buffer = new BufferedReader( isr );
String input = "";
try
{
input = buffer.readLine();
buffer.close() ;
}
catch (IOException e )
{
System.out.println(e);
}
}
}
ReadFile
import java.io.* ;
class ReadFile
{
public static void main( String[] args )
{
try
{
FileReader file = new FileReader( "oscar.txt" ) ;
BufferedReader buffer = new BufferedReader( file );
String line = "" ;
while( ( line = buffer.readLine() ) != null )
{
System.out.println( line ) ;
}
buffer.close() ;
}
catch( IOException e )
{
ListFiles
import java.io.* ;
class ListFiles
{
public static void main( String[] args )
{
File dir = new File( "data" ) ;
if( dir.exists() )
{
String[] files = dir.list() ;
System.out.println( files.length + " files found..." );
for( int i = 0; i < files.length; i++ )
{
System.out.println( files[i] ) ;
}
}
else
{
}
}
}
Formats
import java.text.NumberFormat ;
import java.text.SimpleDateFormat;
public class Formats
{
public static void main(String[] args)
{
NumberFormat nf = NumberFormat.getNumberInstance();
System.out.println( "\nNumber : " + nf.format(123456789) );
NumberFormat cf = NumberFormat.getCurrencyInstance();
System.out.println( "\nCurrency : " + cf.format(1234.50f) );
NumberFormat pf = NumberFormat.getPercentInstance();
System.out.println( "\nPercent : " + pf.format(0.75f) );
java.util.Calendar cal = java.util.Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("M/d/y");
System.out.println( "\nDate : " + df.format( cal.getTime() ) );
SimpleDateFormat tf = new SimpleDateFormat("H:m");
System.out.println( "\nTime : " + tf.format( cal.getTime() ) );
}
}
Decimals
import java.math.BigDecimal ;
public class Decimals
{
public static void main(String[] args)
{
java.text.NumberFormat cf = java.text.NumberFormat.getCurrencyInstance();
// VARIABLES...
double item = 0.70 , rate = 0.05;
double tax = item * rate;
double total = item + tax;
// BigDecimal OBJECTS...
// COMMENT OUT THE VARIABLES AND UNCOMMENT THIS BLOCK TO SOLVE THE
PROBLEM.
/*
BigDecimal item = new BigDecimal(0.70);
BigDecimal rate = new BigDecimal(0.05);
BigDecimal tax = item.multiply(rate);
BigDecimal total = item.add(tax );
*/
// OUTPUT FORMATTED VALUES...
System.out.println("\nItem :\t" + cf.format(item) ) ;
System.out.println("Tax :\t" + cf.format(tax) ) ;
System.out.println("Total :\t" + cf.format(total) ) ;
/*
*/
}
Dates
import java.util.Calendar;
public class Dates
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
cal.set( Calendar.YEAR, 2012 );
System.out.println("\nIt is now " + cal.getTime() );
cal.set( Calendar.YEAR, 2015 );
System.out.println("\nDate is now " + cal.getTime() );