HOME      BLOG       FORUMS      CONTACT       ABOUT

Moving a file or directory in Java

June 2nd, 2008 Albin Joseph Posted in java.io | No Comments »

Moving a file or directory in Java

The renameTo method of file class can be used for moving a file or directory to another destination.


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 the destination file/directory.
		File destintation = new File("d:\\temp\\dir\\test.txt");

		// Move the file
		boolean status = file.renameTo(destintation);

		System.out.println("Moving status - "+status);
	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Renaming a file in Java

June 2nd, 2008 Albin Joseph Posted in java.io | No Comments »

Renaming a file in Java

The renameTo method of file class can be used for renaming a file or directory.

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 the destination file/directory.
		File destintation = new File("d:\\temp\\test1.text");

		// Rename the file.
		boolean status = file.renameTo(destintation);

		System.out.println("Renaming status - "+status);

	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Creating a directory with sub directory

June 2nd, 2008 Albin Joseph Posted in java.io | No Comments »

Creating a directory with sub directory

The mkdirs method of file class is used for creating a direcory and sub directories in Java

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

		// Create directory and sub directories.
		boolean status = file.mkdirs();

		System.out.println("Directory creation status - "+status);

	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Create a directory using java

June 2nd, 2008 Albin Joseph Posted in java.io | No Comments »

Create a directory using java

The mkdir method of file class is used for creating a new directory in java.

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

		// Create directory
		boolean status = file.mkdir();

		System.out.println("Directory creation status - "+status);

	}
}

Tags: , , , ,

AddThis Social Bookmark Button

Creating a FileFilter

June 2nd, 2008 Albin Joseph Posted in java.io | No Comments »

Creating a FileFilter

We need to implement the FileFilter class for creating a file filter. The FileFilter interface contains only one method accept(File). This method is used for checking whether a file should be included or excluded.

import java.io.File;
import java.io.FilenameFilter;
/**
 *
 * A file filter.
 *
 */
class TextFileFilter implements FileFilter {
	/**
	 * Pattern would contain the extension of
	 * the file.
	 */
	private String pattern;

	/**
	 * Initializes the pattern.
	 *
	 * Provide the file extension to filter.
	 *
	 * @param pattern
	 */
	public TextFileFilter(String pattern){
		this.pattern  = pattern;
	}

	public boolean accept(File pathname) {
		return pathname.getName().endsWith(this.pattern);
	}

}
}

To invoke the above FileFilter create an object of the TextFileFilter class like

	TextFileFilter  filter = new TextFileFilter("txt");

The argument to TextFileFilter will be the extension of the file we need to include.

Tags: , , , ,

AddThis Social Bookmark Button

List all the contents of a directory according to a filter.

June 2nd, 2008 Albin Joseph Posted in java.io | No Comments »

List all the contents of a directory according to a filter.

The list method of File class can be used for listing all the contents of a directory according to a filter.

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

		// Create a FilenameFilter for filtering all the text files.
		FileFilter filter = new FileFilter("txt");

		// List the contents of the directory.
		String contents[] = file.list(filter);

		// Loop through the array and display the file/directory names.
		for (int i = 0; i < contents.length; i++) {
			System.out.println(contents[i]);
		}

	}
}

/**
 *
 * A file filter.
 *
 */
class FileFilter implements FilenameFilter{
	/**
	 * Pattern would contain the extension of
	 * the file.
	 */
	private String pattern;

	/**
	 * Initializes the pattern.
	 *
	 * Provide the file extension to filter.
	 *
	 * @param pattern
	 */
	public FileFilter(String pattern){
		this.pattern  = pattern;
	}

	public boolean accept(File dir, String name) {
		return name.endsWith(this.pattern);
	}

}

The above examples filters out all the non text files from the directory. The list method returns an array of file names that matches the filter. But if we want an array of File objects instead of file names we should use listFiles method instead of list method.

Tags: , , , , , ,

AddThis Social Bookmark Button

Writing a filename filter.

June 2nd, 2008 Albin Joseph Posted in java.io | No Comments »

Writing a filename filter.

We need to implement the FilenameFilter class for writing a filename filter. The FilenameFilter interface contains only one method accept(File, String). This method is used for checking whether a file should be included.

import java.io.File;
import java.io.FilenameFilter;
/**
 *
 * A file filter.
 *
 */
class FileFilter implements FilenameFilter{
	/**
	 * Pattern would contain the extension of
	 * the file.
	 */
	private String pattern;

	/**
	 * Initializes the pattern.
	 *
	 * Provide the file extension to filter.
	 *
	 * @param pattern
	 */
	public FileFilter(String pattern){
		this.pattern  = pattern;
	}

	public boolean accept(File dir, String name) {
		return name.endsWith(this.pattern);
	}

}

Tags: , , , ,

AddThis Social Bookmark Button

List all the contents of a directory

June 2nd, 2008 Albin Joseph Posted in java.io | No Comments »

The list method of File class can be used for listing all the contents of a directory.

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

		// List the contents of the directory.
		String contents[] = file.list();

		// Loop through the array and display the file/directory names.
		for (int i = 0; i < contents.length; i++) {
			System.out.println(contents[i]);
		}

	}
}

Tags: , , , ,

AddThis Social Bookmark Button

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();
	}
}

Tags: , , , , ,

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);
	}
}

Tags: , , , , ,

AddThis Social Bookmark Button