You are on page 1of 9

WriteFile

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 );

public static void display( String[] elems )


{
System.out.println( "\nString Array:" );
for( int i = 0; i < elems.length; i++ )
System.out.println( "Element " +i+ " is " + elems[i] );
}
public static void display( int[] elems )
{
System.out.println( "\nInteger Array:" );
for( int i = 0; i < elems.length; i++ )
System.out.println("Element " +i+ " is " + elems[i] );
}
}

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);
}
}
}

System.out.println("\nThanks, you are reading " + input );

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 )
{

System.out.println( "A read error has occurred." ) ;


}

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
{
}

System.out.println( "Folder not found." ) ;

}
}

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) ) ;

/*

*/
}

// OUTPUT UNFORMATTED VALUES...


// UNCOMMENT THIS BLOCK TO SEE THE CAUSE OF THE PROBLEM...
System.out.println("\nItem :\t" + item ) ;
System.out.println("Tax :\t" + tax ) ;
System.out.println("Total :\t" + 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() );

String fields = "\nYear:\t\t\t" + cal.get(Calendar.YEAR);


fields += "\nMonth:\t\t\t" + cal.get(Calendar.MONTH);
fields += "\nDay of the month:\t" + cal.get(Calendar.DAY_OF_MONTH);
fields += "\nDay of the week:\t" + cal.get(Calendar.DAY_OF_WEEK);
fields += "\nDay of the year:\t" + cal.get(Calendar.DAY_OF_YEAR);
fields += "\nWeek of the year:\t" + cal.get(Calendar.WEEK_OF_YEAR);
fields += "\nWeek of the month:\t" +
cal.get(Calendar.WEEK_OF_MONTH);
fields += "\nDay of week in month:\t" +
cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
fields += "\nHour (0-11):\t\t" + cal.get(Calendar.HOUR);
fields += "\nA.M.(0) or P.M.(1):\t" + cal.get(Calendar.AM_PM);
fields += "\nHour (0-23):\t\t" + cal.get(Calendar.HOUR_OF_DAY);
fields += "\nMinute:\t\t\t" + cal.get(Calendar.MINUTE);
fields += "\nSecond:\t\t\t" + cal.get(Calendar.SECOND);
System.out.println( fields ) ;
}
}

You might also like