SimpleCodeSnippets


    AboutMe      MyCurrentLocation     Blog      GuestBook     Think Big- Possibilities are Endless     HOME 

1. How to create a file in a specified directory ? The new Directory should be by the name Rajat_Bhatnagar and the path of it should be in the current directory of the Program?

 Solution:

Use mkdir() and createFile() methods of the File class

package generalProgsConsoleBased;
import java.io.*;
public class FileCreation {
    public static void main(String[] args)throws IOException{
        //Fetching the Directory of the Program
        String str = System.getProperty("user.dir");
        System.out.println("Current Directory : "+str);
        String strDirectory = "Rajat_Bhatnagar";
        //creating the new Directory
        boolean success = new File(strDirectory).mkdir();
        if(success){
            System.out.println("Directory " +strDirectory+ " Created");
        }
        //appending the directory to the current directory by the + operator
        String filePath = str + "\\"+ strDirectory ;
        System.out.println("filePath = "+filePath );
        File f = new File(filePath,"NewlyCreatedFile.txt");
        f.createNewFile();
        //Checking if File is created
        System.out.println("Is file Created ? "+f.exists());
    }
}

 

2. Write a program which fetches the name of all the directory , subdirectories and files and there corresponding paths on the command line:.Here the Root directory should be the directory of your Java file.?

Solution:

Use Recursion concept:

package generalProgsConsoleBased;
import java.io.*;
public class DirectoryFilePaths {
    //Class Demonstrarting the Recursion Concept
    public static void main(String[] args){
        String curDirPath = System.getProperty("user.dir");
        System.out.println("Current Directory Path is : "+curDirPath);
        System.out.println("==========================================");
        File dirFile = new File(curDirPath);
        if(dirFile.isDirectory()){
            String dirfiles[] = dirFile.list();
            for(int i=0;i<dirfiles.length;i++){
                File subDirFile = new File(dirFile + "\\" + dirfiles[i] );
                if(subDirFile.isFile()){
                    System.out.println("FileName is "+subDirFile.getName()+ " and FilePath is: "+subDirFile.getAbsolutePath());
                    System.out.println("===============================================");
                }
                else{
                    System.out.println(subDirFile +" is a Directory" );
                    System.out.println("Retrieving Subdirectory listings");
                    System.out.println("========================================");
                    //Calling method that does Recursion
                    processingSubDirectory(subDirFile);
                }
            }
        }
            
    }
    public static void processingSubDirectory(File subDirFile){
        if(subDirFile.isDirectory()){
            String dirfiles[] = subDirFile.list();
            for(int i=0;i<dirfiles.length;i++){
                File subDirFile1 = new File(subDirFile + "\\" + dirfiles[i] );
                if(subDirFile1.isFile()){
                    System.out.println("FileName is "+subDirFile1.getName()+ " and FilePath is: "+subDirFile1.getAbsolutePath());
                    System.out.println("=========================================");
                }
                else{
                    System.out.println("SUBDIRECTORY>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                    System.out.println(subDirFile1.getName());
                    //Using Recursion concept here
                    processingSubDirectory(subDirFile1);
                }
            }
        }
        
    }    

Write a JSP page which connects to oracle database and Fetches the Corresponding Records in a Table?

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*,oracle.jdbc.*,oracle.jdbc.driver.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Displaying Editable Records</title>
</head>
<body BGCOLOR="#FDF5E6">
<h1><center><u>Double click a corresponding cell to update</u></center></h1>
<br>
<%
    Connection conn = null;
    ResultSet rset = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
    }

    catch (ClassNotFoundException e) {
        System.out.println("Unable to load Driver");
    }

    try {
        DriverManager
        .registerDriver(new oracle.jdbc.driver.OracleDriver());
        conn = DriverManager.getConnection(
        "jdbc:oracle:thin:@kaikeyee:1521:enr9i", "TNS51628",
        "TNS51628");

        Statement stmt = conn.createStatement();
        rset = stmt.executeQuery("SELECT NAME,AGE,CITY FROM CONTACTS");
       
    }

    catch (SQLException se) {
        se.printStackTrace();
    }
%>

<TABLE BORDER="1" align="center">
    <TR>
        <TH>Name</TH>
        <TH>Age  </TH>
        <TH>City</TH>

    </TR>
    <%
    while (rset.next()) {
    %>
   
    <TR>
        <TD><%=rset.getString(1)%></TD>
        <TD><%=rset.getString(2)%></TD>
        <TD><%=rset.getString(3)%></TD>

    </TR>
   
    <%
    }
    %>
</TABLE>
<br>
<b><center>Note : Only Age and City is Editable</center></b>
</body>
</html>

Write a Program to throw a Dice 6000 Times and Print how many times 1,2,3,4,5,6 are encountered?

package generalProgsConsoleBased;
import java.util.Random;
public class RollDice6000Times {
    /*
     * This class Roles a Dice 6000 times and prints the number of times 1,2,3,4,5,6 is encountered
     */
    public static void main(String[] args){
        int face;
        int frequency1 = 0;
        int frequency2 = 0;
        int frequency3 = 0;
        int frequency4 = 0;
        int frequency5 = 0;
        int frequency6 = 0;
        //Generating a Random Object
        Random randomNumber = new Random();
        //Throwing the Dice 6000 Times
        for(int counter=1;counter<=6000;counter++){
            face = 1 + randomNumber.nextInt(6);
            //face when incremented by 1 can take Values 1,2,3,4,5,6
            switch(face){
            case 1:
                ++frequency1;
                break;
            case 2:
                ++frequency2;
                break;
            case 3:
                ++frequency3;
                break;
            case 4:
                ++frequency4;
                break;
            case 5:
                ++frequency5;
                break;
            case 6:
                ++frequency6;
                break;
            }
        }       
        //Loop Ends
        System.out.println("Face\tFrequency");
        System.out.printf("1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n",frequency1,frequency2,frequency3,frequency4,frequency5,frequency6);
    }
}