Java – Writing XML using JAXB

 
package com.pwn.xml;

import java.io.File;
import javax.xml.bind.JAXB;

public class XMLWriter {

	public static void main(String[] args) {
		User user = new User();
		user.setName("Johnny");
		user.setUserID(12313);
		try {
			JAXB.marshal(user,
					new File("C://install//userDetail.xml"));
		} catch (Exception e) {
			System.err.println(
					"Exception occurred while writing in XML!");
		} finally {
			System.out.println("XML created");
		}

	}

}


@XmlRootElement
class User {
	private long userID;
	private String name;

	public long getUserID() {
		return userID;
	}

	public void setUserID(long userID) {
		this.userID = userID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

Java – Convert java.util.Date() to java.sql.Date()

 

package com.pwn.dateUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class DateToSQLDateConversion {

	public static void main(String[] args) {
		java.util.Date utilDate = new java.util.Date();
		System.out.println("java.util.Date is : " + utilDate);
		java.sql.Date sqlDate = convert(utilDate);
		System.out.println("java.sql.Date is : " + sqlDate);
		DateFormat df = new SimpleDateFormat("dd/MM/YYYY - hh:mm:ss");
		System.out.println(
				"dateFormated date is : " + df.format(utilDate));

	}

	private static java.sql.Date convert(java.util.Date utilDate) {
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		return sqlDate;
	}

}

java.util.Date is : Mon Jan 29 16:53:36 PST 2018
java.sql.Date is : 2018-01-29
dateFormated date is : 29/01/2018 - 04:53:36

Java – Remove Item from Collection

It is tricky to remove items from a list while within a loop, this is due to the fact that the index and length of the list get changed.
It is a good idea to delete an item from List using Iterator interface remove() method. The same goal can be achieved using removeIf() option

 

package com.pwn.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RmvItemFrmCollectoin {

	public static void main(String[] args) {
		List<String> cities = new ArrayList<String>();
		cities.add("San Francisco");
		cities.add("New York");
		cities.add("San Diego");
		System.out.println("Before deleting List " + cities);
		Iterator<String> citiesIterator = cities.iterator();
		while (citiesIterator.hasNext()) {
			String city = citiesIterator.next();

			if ("San Diego".equals(city)) {
				citiesIterator.remove();
			}
		}

		System.out.println("After deleting List " + cities);
		cities = new ArrayList<String>();
		cities.add("San Francisco");
		cities.add("New York");
		cities.add("San Diego");

		cities.removeIf(p -> "San Diego".equals(p));

		System.out.println("After deleting List " + cities);

	}

}
Before deleting List [San Francisco, New York, San Diego]
After deleting List [San Francisco, New York]
After deleting List [San Francisco, New York]

Java – Recursive Folder reading

  

package com.pwn.nio;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

public class NIORecrusiveFolderReading {
	public static void main(String args[]) {

		Path dirPath = new File("C://install").toPath();

		try {
			Files.walkFileTree(dirPath,
					EnumSet.noneOf(FileVisitOption.class), 1,
					new SimpleFileVisitor() {
						@Override
						public FileVisitResult preVisitDirectory(
								Path selectedPath,
								BasicFileAttributes attrs)
								throws IOException {
							System.out.println("d "
									+ selectedPath.toAbsolutePath());
							return FileVisitResult.CONTINUE;
						}

						@Override
						public FileVisitResult visitFile(
								Path selectedPath,
								BasicFileAttributes attrs)
								throws IOException {
							System.out.println("f "
									+ selectedPath.toAbsolutePath());
							return FileVisitResult.CONTINUE;
						}
					});
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
d C:\install
f C:\install\AnypointStudio
f C:\install\Apache Tomcat 8.0.27
f C:\install\apache-maven-3.5.2
f C:\install\eclipse
f C:\install\glassfish-4.1.1
f C:\install\instructions.txt
f C:\install\NetBeans 8.2
f C:\install\spring-tool-suite-3.9.1
f C:\install\sqldeveloper
f C:\install\user.xml
f C:\install\userDetail.xml
f C:\install\workspace