HOME      BLOG       FORUMS      CONTACT       ABOUT

Delete a file or directory when JVM terminates.

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Delete a file or directory when JVM terminates.

The deleteOnExit method of file class can be used for deleting a file or directory when JVM terminates.

package io;

import java.io.File;
/**
 * File class examples
 *
 * Contains different operations of java.io.File class.
 */
public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test1.txt");

		// Delete the file or directory when JVM terminates
		file.deleteOnExit();
	}
}
AddThis Social Bookmark Button

Delete an existing file or directory

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Delete an existing file or directory

The delete method of file class can be used for deleting a file or directory. The delete method returns true if the file or directory was successfully deleted from the filesystem. If the file object represents a directory, the directory has to be empty for a successful deletion.

package io;

import java.io.File;
/**
 * File class examples
 *
 * Contains different operations of java.io.File class.
 */
public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test1.txt");

		// Delete the file or directory
		boolean isDeleted = file.delete();

		System.out.println(isDeleted);
	}
}
AddThis Social Bookmark Button

Creating new file

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Creating new file

The createNewFile method of file class can be used for creating a new file. The createNewFile method returns true if the file does not exist and was successfully created an empty file.

package io;

import java.io.File;
import java.io.IOException;

public class FileExample {
	public static void main(String[] args) throws IOException{
		File file = new File("d:\\temp\\test1.txt");

		// Create new file.
		boolean isCreated = file.createNewFile();

		System.out.println(isCreated);
	}
}
AddThis Social Bookmark Button

Finds the size of a file.

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Finds the size of a file.

The length method of file class can be used for finding the size of a file. If the file object represents a directory the length method returns zero.

package io;

import java.io.File;

public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test.txt");

		// Find the size of the file in bytes
		long value = file.length();

		// Convert the size in bytes to mega bytes.
		double sizeInMB =(double) value / 1024 / 1024;
		System.out.println(sizeInMB);
	}
}
AddThis Social Bookmark Button

Finds the last modified date of a file or directory.

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Finds the last modified date of a file or directory.

The lastModified method of file class can be used for finding the last modified date of a file or directory.

package io;

import java.io.File;
import java.util.Date;

public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test.txt");

		// Find the last modified date
		long value = file.lastModified();

		// Convert the value to date
		Date lastModifiedDate = new Date(value);
		System.out.println(lastModifiedDate);
	}
}
AddThis Social Bookmark Button

Tests whether the file or directory is hidden or not.

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Tests whether the file or directory is hidden or not.

The isHidden method of file class can be used for checking whether a file or directory is hidden or not.

package io;

import java.io.File;

public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test.txt");

		// Tests whether the file object is hidden or not.
		boolean value = file.isHidden();
		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Find out whether a file object is a file or not.

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Find out whether a file object is a file or not.

The isFile method of file class can be used for checking whether a file object is a file or a directory.

package io;

import java.io.File;

public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test.txt");

		// Tests whether the given file object is a file or directory.
		boolean value = file.isFile();
		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Find out whether a file object is a directory or not.

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Find out whether a file object is a directory or not.

The isDirectory method of file class can be used for checking whether a file object is directory or a file.

package io;

import java.io.File;

public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test.txt");

		boolean value = file.isDirectory();
		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Check whether a file or directory exits or not.

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

Check whether a file or directory exits or not.

The exists method of file class can be used for checking whether a file or directory exists or not.

package io;

import java.io.File;

public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test.txt");

		boolean value = file.exists();
		System.out.println(value);
	}
}
AddThis Social Bookmark Button

Tests whether a file is writable or not.

May 27th, 2008 Albin Joseph Posted in java.io No Comments »

The canWrite method of file class can be used for checking whether the application has a write permission on a file.

package io;

import java.io.File;

public class FileExample {
	public static void main(String[] args){
		File file = new File("d:\\temp\\test.txt");

		boolean value = file.canWrite();
		System.out.println(value);
	}
}
AddThis Social Bookmark Button