uk13
Q1)
import java.io.BufferedReader;
import java.io.FileReader;
public class main
{
public static void main(String[] args) throws Exception {
String line;
int count = 0;
int lineCount = 0;
//Opens a file in read mode
FileReader file = new FileReader("data.txt");
BufferedReader br = new BufferedReader(file);
//Gets each line till end of file is reached
while((line = br.readLine()) != null) {
lineCount++;
//Splits each line into words
String words[] = line.split(" ");
//Counts each word
count = count + words.length;
}
System.out.println("Number of words present in given file: " + count);
System.out.println("Number of lines present in given file: " + lineCount);
br.close();
}
}
Q2)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class NewClass {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String strDate = formatter.format(date);
System.out.println("Current date is: "+strDate);
formatter = new SimpleDateFormat("MM-dd-yyyy");
strDate = formatter.format(date);
System.out.println("Current date is: "+strDate);
formatter = new SimpleDateFormat("EEEEEE MMMM dd yyyy");
strDate = formatter.format(date);
System.out.println("Current date is: "+strDate);
formatter = new SimpleDateFormat("E MMMM dd HH:mm:ss z yyyy");
strDate = formatter.format(date);
System.out.println("Current date and time is: "+strDate);
formatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss a Z");
strDate = formatter.format(date);
System.out.println("Current date and time is: "+strDate);
formatter = new SimpleDateFormat("hh:mm:ss");
strDate = formatter.format(date);
System.out.println("Current time is: "+strDate);
formatter = new SimpleDateFormat("w");
strDate = formatter.format(date);
System.out.println("Current week of year is: "+strDate);
formatter = new SimpleDateFormat("W");
strDate = formatter.format(date);
System.out.println("Current week of the month is: "+strDate);
formatter = new SimpleDateFormat("D");
strDate = formatter.format(date);
System.out.println("Current day of the year: "+strDate);
}
}