Java source code examples

Java source code samples. Java code examples.

Find out session created time in JSP/Servlet

Find out session created time in JSP/Servlet

The getCreationTime of HttpSession interface allows us to find out the time in which the session was created in JSP and Servlet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		long creationTime = session.getCreationTime();

		System.out.println("Session created time = "+creationTime);
	}

Tags: javax.servlet.http

Create a StringWriter object

The default constructor allows us to create a StringWriter with default string buffer size.

import java.io.StringWriter;

/**
 * StringWriter class examples.
 */
public class StringWriterExample {
	public static void main(String[] args){
		// Create a stringWriter object.
		StringWriter writer = new StringWriter();
	}
}

Tags: java.io

Compare two files using java

Compare two files using java

The compareTo method of File class helps us to create a temporary file. This method returns an integer. If the value of the integer is greater than zero, the file will be greater than the argument, if the file is less than zero, the file will be less than the argument and a value of zero means both the files are equal.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args) {
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Create another file for comparison.
		File cFile = new File("d:\\temp\\Copy of test.txtt");

		// Compare two files.
		int value = file.compareTo(cFile);

		System.out.println("File comparison result :  "+value);
	}
}

Tags: java.io

Create a temp file using java

Create a temp file using java

The createTempFile method of File class helps us to create a temporary file.

import java.io.File;
import java.io.IOException;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args) throws IOException{
		// Create file object representing the source file/directory
		File file = new File("d:\\temp");

		// Create the temporary file.
		File tempFile = File.createTempFile("test", ".tmp",file);
	}
}

Tags: java.io

Finds out total used space in a partition using java

Finds out total used space in a partition using java

In Java we can find the total used space of any drive or partition using getTotalSpace and getFreeSpace methods.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total space available in current drive
		long totalSpace = file.getTotalSpace();

		// Get the total free space available in current partition
		long freeSpace = file.getFreeSpace();

		// Finds the total used space
		long value = totalSpace - freeSpace;

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total used space in the current partition is "+valueGB);
	}
}

Tags: java.io

Finds out total usable space available in a partition using java

Finds out total usable space available in a partition using java

The getUsableSpace method of File class can be used for getting the total usable space available in a partition or drive using java.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total usable space available in current drive
		long value = file.getUsableSpace();

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total free space available in the current partition is "+valueGB);
	}
}

Tags: java.io

Finds out total free space available in a partition using java

Finds out total free space available in a partition using java

The getFreeSpace method of File class can be used for getting the total free space available in a partition or drive using java.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total free space available in current drive
		long value = file.getFreeSpace();

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total free space available in the current partition is "+valueGB);
	}
}

Tags: java.io

Finds out total space available in a partition using java

Finds out total space available in a partition using java

The getTotalSpace method of File class can be used for getting the total space available in a partition or drive using java.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total space available in current drive
		long value = file.getTotalSpace();

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total space available in the current partition is "+valueGB);
	}
}

Tags: java.io

Lists all the drives present in Windows using java

Lists all the drives present in Windows using java

The listRoots method of File class is used for finding out all the drives available in Windows.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Get all the drives
		File drives[] = File.listRoots();

		// Loop through the drive list and display the drives.
		for (int index = 0; index < drives.length; index++) {
			System.out.println(drives[index]);
		}
	}
}

Note: Since Linux does not have any drives concept, the output of the above program in Linux operating system will be "/"

Tags: java.io

Checks whether a file or directory has executable permission or not.

Checks whether a file or directory has executable permission or not.

The canExecute method of File class can be used for finding out whether the given file has executable permission or not.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp");

		// Check whether the file has executable permission or not.
		boolean value = file.canExecute();

		System.out.println("File executable permission status : "+value);
	}
}

Tags: java.io