Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Java digital assignment-5

FILE HANDLING PROGRAMS


1. Java Program to Replace First Letter of Every Word with Capital Letter.

Code:

import java.util.*;

import java.io.*;

public class file1 {

public static void main(String[] args)

{ try{

FileReader fr = new FileReader("C:\\Users\\windows


10\\Desktop\\17BCI0128\\filebci0128.txt");

String str = "";

int ch;

while ((ch = fr.read()) != -1) {

str += Character.toString((char) ch);

String upper_case_line = "";

Scanner lineScan = new Scanner(str);

while(lineScan.hasNext()) {

String word = lineScan.next();

upper_case_line += Character.toUpperCase(word.charAt(0)) + word.substring(1) + " ";

System.out.println("File content: " + str);


System.out.println("Output file content: "+upper_case_line.trim());

FileWriter fw=new FileWriter("C:\\Users\\windows


10\\Desktop\\17BCI0128\\outbci0128.txt");

fw.write(upper_case_line.trim());

fw.close();

catch(Exception e){

System.out.println("error");

File input:

File output:
2. Java Program to Reverse the Contents of a File and Print it.

Code:
import java.io.FileReader;

public class file2 {


public static void main(String[] args) {

try {
FileReader fr = new FileReader("C:\\Users\\windows
10\\Desktop\\17BCI0128\\filebci0128.txt");
String str = "";
int ch;
while ((ch = fr.read()) != -1) {
str += Character.toString((char) ch);
}
System.out.println("File content : " + str);
StringBuilder sb = new StringBuilder(str);
System.out.println("Reverse content : " + sb.reverse());
fr.close();
} catch (Exception e) {
System.out.println("error");
}
}
}
3. Java Program to Update Details of Employee Using Files.
Code:

4. Java Program to Convert the Content of File to LowerCase.


Code:
import java.io.*;

public class file7 {


public static void main(String[] args) {

try {
FileReader fr = new FileReader("C:\\Users\\windows
10\\Desktop\\17BCI0128\\f7.txt");
String str = "";
int ch;
while ((ch = fr.read()) != -1) {
str += Character.toString((char) ch);
}
System.out.println("File content : " + str);
System.out.println("lowercase content : " + str.toLowerCase());
FileWriter fw=new FileWriter("C:\\Users\\windows
10\\Desktop\\17BCI0128\\f7out.txt");
fw.write(str.toLowerCase());
fw.close();
fr.close();
} catch (Exception e) {
System.out.println("error");
}
}
}

5. Java Program to Create and Count Number of Characters in a File


Code:.
import java.util.*;
import java.io.*;
import java.lang.*;

public class file5 {


public static void main(String[] args) throws IOException{
int count = 0;
BufferedReader reader = null;
try{
FileReader fread = new FileReader("C:\\Users\\windows
10\\Desktop\\17BCI0128\\filebci0128.txt");
reader = new BufferedReader(fread);
String string = reader.readLine();
while(string!=null){
for(int i = 0; i < string.length(); i++)
{ if(string.charAt(i) != ' ')
count++; }
string = reader.readLine();
}
System.out.println("Number of characters:" + count);
}
catch(IOException e){
e.printStackTrace();
}
catch(Exception e)
{ System.out.println(
e);
}
reader.close();
}
}
Output:
6. Java Program to Join Lines of Two given Files and Store them in a New file
Code:
import java.io.*;

public class file6


{
public static void main(String[] args) throws IOException
{
PrintWriter pw = new PrintWriter("C:\\Users\\windows
10\\Desktop\\17BCI0128\\out.txt");

BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\windows


10\\Desktop\\17BCI0128\\f5.txt"));

String line = br.readLine();

while (line != null)


{
pw.println(line);
line = br.readLine();
}

br = new BufferedReader(new FileReader("C:\\Users\\windows


10\\Desktop\\17BCI0128\\f6.txt"));

line = br.readLine();
while(line != null)
{
pw.println(line);
line = br.readLine();
}

pw.flush();
br.close();
pw.close();

System.out.println("merged");
}
}

7. Java Program to Collect Statistics of a Source File like Total Lines, Total no. of Blank
Lines, Total no. of Lines Ending with Semicolon.
Code:
import java.io.*;
public class file8
{
public static void main(String[] args) throws IOException
{
File file = new File("C:\\Users\\windows 10\\Desktop\\17BCI0128\\f5.txt");
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);

String line;

int countWord = 0;
int sentenceCount = 0;
int characterCount = 0;
int paragraphCount = 1;
int whitespaceCount = 0;
int semiColonLineCount = 0;

while((line = reader.readLine()) != null)


{
if(line.equals(""))
{
paragraphCount++;
}
else {
characterCount += line.length();
String[] wordList = line.split("\\s+");

countWord += wordList.length;
whitespaceCount += countWord -1;

String[] sentenceList = line.split("[!?.:]+");

String[] semi = line.trim().split(" ");


for(String s : semi)
{ if(s.charAt(s.length()-
1)==';'){
semiColonLineCount++;
}
}

sentenceCount += sentenceList.length;
}
}

System.out.println("Total number of Lines = " + sentenceCount);


System.out.println("Number of paragraphs = " + paragraphCount);
System.out.println("Total number of blank = " + whitespaceCount);
System.out.println("Total number of semicolon = " + semiColonLineCount);
}
}
Q. JDBC
Retrieve employee details whose designation is manager using java with mysql database
connectivity

package jdbcemp;

import java.sql.*;

public class DBAccess


{

Connection cn;

PreparedStatement ps;

ResultSet rs;

public DBAccess() {

try {

Class.forName("com.mysql.cj.jdbc.Driver");

cn = DriverManager.getConnection ("jdbc:mysql://localhost/jdbc", "root",


"17BIT0260");

System.out.println("Database Connected..");

}catch(Exception e) {

System.out.println("Database not Connected.." + e);


}

} // End of Constuctor

public void insert(String name, String designation, double salary) {

try {

ps = cn.prepareStatement("insert into employee values(?,?,?)");

ps.setString(1, name);

ps.setString(2, designation);

ps.setDouble(3, salary);

ps.executeUpdate();

System.out.println("1 Record inserted...");

}catch(Exception e) {

System.out.print("\n Unable to insert..\n" + e);

} // end of insert
public void updateX(String id, String name, String addr, String designation, String cell, double
salary) {

try {

ps = cn.prepareStatement("update employee set emp_name=?, designation=?,


salary=? WHERE emp_id=?");

ps.setString(1, name);

ps.setString(2, designation);

ps.setDouble(3, salary);

ps.executeUpdate();

System.out.println("Record Update...");

}catch(Exception e) {

System.out.print("\n Unable to Update..\n" + e);

} // end of insert
public void display() {

try {

ps = cn.prepareStatement("select * from employee");

rs = ps.executeQuery();

System.out.println(" ID | NAME | ADDRESS | DESIGNATION


| PHONE | SALARY");

System.out.println("------------------------------------------------------------------------
--");

while(rs.next()) {

System.out.println( rs.getString(1) + "\t " + rs.getString(2) + "\t " +


rs.getString(3) + "\t " + rs.getString(4) + "\t " + rs.getString(5) + "\t " + rs.getString(6));

} // end of while

}catch(Exception e) {}

} // end of dispay

public void deleteX(String id) {

try {

ps = cn.prepareStatement("delete from employee where emp_id=?");

ps.setString(1, id);
ps.executeUpdate();

System.out.println("Record Deleted..");

}catch(Exception e) {

System.out.println(e);

} // end of deleteX
SERVLETS

Q. login validation page

Index.html
<form action="loginPage" method="post">
User Name:<input type="text" name="uname"/><br/>
Password:<input type="password" name="upass"/><br/>
<input type="submit" value="SUBMIT"/>
</form>

Validation.java
import java.io.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class Validation extends HttpServlet

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException

response.setContentType("text/html");

PrintWriter pwriter = response.getWriter();

String name=request.getParameter("uname");

String pass=request.getParameter("upass");

if(name.equals("Sanjana") &&

pass.equals("welcome"))
{

RequestDispatcher dis=request.getRequestDispatcher("welcome");

dis.forward(request, response);

else

pwriter.print("User name or password is incorrect!");

RequestDispatcher dis=request.getRequestDispatcher("index.html");

dis.include(request, response);

User.java
import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class WelcomeUser extends HttpServlet {

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException

response.setContentType("text/html");

PrintWriter pwriter = response.getWriter();

String name=request.getParameter("uname");

pwriter.print("Hello "+name+"!");

pwriter.print(" Welcome !");

}
}

web.xml

<web-app>

<display-name>welcome!!!</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>Login</servlet-name>

<servlet-class>Validation</servlet-class>

</servlet>

<servlet>

<servlet-name>Welcome</servlet-name>

<servlet-class>WelcomeUser</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Login</servlet-name>

<url-pattern>/loginPage</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>Welcome</servlet-name>

<url-pattern>/welcome</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

You might also like