0% found this document useful (0 votes)
7 views12 pages

PDF5 RealWorld Java Programs

The document provides a collection of real-world Java application examples, including file copying, reading properties files, creating a simple socket server and client, basic JDBC operations, a Swing GUI application, JSON handling, HTTP requests, email sending using JavaMail, a command-line ToDo list, JUnit testing, and a Maven POM snippet. Each example includes code snippets demonstrating the functionality. The document serves as a practical guide for various Java programming tasks.

Uploaded by

ramanjicse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

PDF5 RealWorld Java Programs

The document provides a collection of real-world Java application examples, including file copying, reading properties files, creating a simple socket server and client, basic JDBC operations, a Swing GUI application, JSON handling, HTTP requests, email sending using JavaMail, a command-line ToDo list, JUnit testing, and a Maven POM snippet. Each example includes code snippets demonstrating the functionality. The document serves as a practical guide for various Java programming tasks.

Uploaded by

ramanjicse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Real-world Java Applications

1. File Copy
Copy file using streams.
import java.io.*;
public class FileCopy{ public static void main(String[] args) throws Exception{ try(FileIn
putStream in=new FileInputStream("a.txt"); FileOutputStream out=new FileOutputStream("b.tx
t")){ byte[] buf=new byte[4096]; int r; while((r=in.read(buf))!=-1) out.write(buf,0,r); }
} }

Page 1 of 12
2. Properties File
Read properties configuration.
import java.util.*; import java.io.*;
public class PropsDemo{ public static void main(String[] args) throws Exception{ Propertie
s p=new Properties(); try(FileReader r=new FileReader("app.properties")){ p.load(r); } Sys
tem.out.println(p.getProperty("app.name")); } }

Page 2 of 12
3. Simple Socket Server
TCP server accepts connections.
import java.net.*; import java.io.*;
public class SimpleServer{ public static void main(String[] args) throws Exception{ Server
Socket ss=new ServerSocket(5000); Socket s=ss.accept(); BufferedReader br=new BufferedRead
er(new InputStreamReader(s.getInputStream())); System.out.println(br.readLine()); ss.close
(); } }

Page 3 of 12
4. Socket Client
Connect to server and send message.
import java.net.*; import java.io.*;
public class SimpleClient{ public static void main(String[] args) throws Exception{ Socket
s=new Socket("localhost",5000); PrintWriter pw=new PrintWriter(s.getOutputStream(), true)
; pw.println("Hello server"); s.close(); } }

Page 4 of 12
5. JDBC - Select
Basic JDBC select template.
import java.sql.*;
public class JDBCDemo{ public static void main(String[] args) throws Exception{ Class.forN
ame("org.h2.Driver"); try(Connection c=DriverManager.getConnection("jdbc:h2:mem:"); Statem
ent s=c.createStatement()){ s.execute("CREATE TABLE t(id INT); INSERT INTO t VALUES(1); Re
sultSet rs=s.executeQuery("SELECT * FROM t"); while(rs.next()) System.out.println(rs.getIn
t(1)); } } }

Page 5 of 12
6. Swing - Hello
Simple GUI window with a button.
import javax.swing.*;
public class SwingDemo{ public static void main(String[] args){ JFrame f=new JFrame("Win")
; JButton b=new JButton("Click"); b.addActionListener(e-> JOptionPane.showMessageDialog(f,
"Clicked")); f.add(b); f.setSize(200,200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
E); f.setVisible(true); } }

Page 6 of 12
7. JSON Handling
Parse JSON using org.json (example).
/* Example using org.json library: */
import org.json.*;
public class JsonDemo{ public static void main(String[] args){ JSONObject o=new JSONObject
(); o.put("a",1); System.out.println(o.toString()); } }

Page 7 of 12
8. HTTP Request - HttpURLConnection
Simple GET request.
import java.net.*; import java.io.*;
public class HttpGet{ public static void main(String[] args) throws Exception{ URL u=new U
RL("http://example.com"); HttpURLConnection c=(HttpURLConnection)u.openConnection(); try(B
ufferedReader br=new BufferedReader(new InputStreamReader(c.getInputStream()))){ System.ou
t.println(br.readLine()); } } }

Page 8 of 12
9. Email - JavaMail Outline
Send email via JavaMail APIs (outline).
/* Requires JavaMail dependency. Outline:
Properties props = new Properties(); Session session = Session.getInstance(props);
Message msg = new MimeMessage(session); Transport.send(msg);
*/

Page 9 of 12
10. Mini Project - ToDo CLI
Simple command-line todo list using file storage.
import java.nio.file.*; import java.util.*;
public class TodoCLI{ public static void main(String[] args) throws Exception{ Path p=Path
s.get("todo.txt"); List<String> list=Files.exists(p)?Files.readAllLines(p):new ArrayList<>
(); if(args.length>0 && args[0].equals("add")) { list.add(String.join(" ", Arrays.copyOfRa
nge(args,1,args.length))); Files.write(p,list); } else for(String s:list) System.out.print
ln(s); } }

Page 10 of 12
11. Unit Test - JUnit Example
A simple JUnit 5 test case example.
/* Example test:
import org.junit.jupiter.api.*;
public class MathTest { @Test void testSum(){ Assertions.assertEquals(5, 2+3); } }
*/

Page 11 of 12
12. Build - Maven POM
A small pom.xml snippet for a Java project.
<project xmlns="http://maven.apache.org/POM/4.0.0">...</project>

Page 12 of 12

You might also like