Java source code examples

Java source code samples. Java code examples.

Entries Tagged as 'java.io'

Delete a file or directory when JVM terminates.

May 27th, 2008 · 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 [...]

[Read more →]

Tags: java.io

Delete an existing file or directory

May 27th, 2008 · 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 [...]

[Read more →]

Tags: java.io

Creating new file

May 27th, 2008 · 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 = [...]

[Read more →]

Tags: java.io

Finds the size of a file.

May 27th, 2008 · 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 [...]

[Read more →]

Tags: java.io

Finds the last modified date of a file or directory.

May 27th, 2008 · 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 [...]

[Read more →]

Tags: java.io

Tests whether the file or directory is hidden or not.

May 27th, 2008 · 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);
}
}

[Read more →]

Tags: java.io

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

May 27th, 2008 · 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 [...]

[Read more →]

Tags: java.io

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

May 27th, 2008 · 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);
}
}

[Read more →]

Tags: java.io

Check whether a file or directory exits or not.

May 27th, 2008 · 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);
}
}

[Read more →]

Tags: java.io

Tests whether a file is writable or not.

May 27th, 2008 · 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);
}
}

[Read more →]

Tags: java.io