Java/Files

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Java Data Types, Variables,and Arrays
Operators
Java Packages, Classes, and Interfaces
Java Program
Java Modifiers
Java Control Statements
Java Exception Handling
Java Object Oriented Programming
Java Wrappers
Java Strings
Java Math
Java Arrays
Java Random Numbers
Java Date and Time
Java Regular Expressions
Java Collections
Java Generics
Java Thread and Multithreading
Java IO and file Handling
* Files & Directories
* Java Byte Oriented IO
* Java Character Oriented IO
* Java Object Serialization
Java Network Programming
RMI
Java Image, Audio, Video
Java GUI
Java Applets
Java Internationalization
JDBC
Java and XML

[edit] Java File Handling

Most of the classes defined by java.io operate on streams, the File class does not. It deals directly with files and the file system. That is, the File class does not specify how information is retrieved from or stored in files; it describes the properties of a file itself. A File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate subdirectory hierarchies. Files are a primary source and destination for data within many programs. Although there are severe restrictions on their use within applets for security reasons, files are still a central resource for storing persistent and shared information.


There are different constructors can be used to create File objects:

File(File parent, String child)
File(String pathname)
File(String parent, String child)
File(URI uri)


In all these four cases ,directoryPath is the path name of the file, filename is the name of the file, dirObj is a File object that specifies a directory, and uriObj is a URI object that describes a file.

File defines various methods through which we can easily obtain the required information related to any file . For example:

1. getName ( ) method is used to get the name of a particular file

2. getParent ( ) method gives the name of the parent directory exists ( ) method return the value “ true “ if a file exists else gives “ false” as result if it doesn’t find the file of that name which it is trying to locate a particular file.

3. isFile ( ) return true if it is called on a file and false if called on a directory isAbsolute ( ) method return “ true” if the file is of absolute type end returns “ false” if the file is of relative type.

4. renameTo( ) methiod is used to change the name of an existing file boolean renameTo(File newName) It will return the value as “true” if it successfully changes the name of the file else returns “false” if it is not bale to locate the file which has to be changed.

5. delete( ) This method is used to delete a particular file and it return value as “ true” if it successfully deletes the false else returns “false” if it fails in its attempt. Boolean delete ( )

6. deleteOnExit( ) This method removes the file associated with the invoking object when the Java Virtual Machine terminates.

7. isHidden( ) Returns true if the particular file is hidden. And gives value as false in the other case.

We will show you different usage of java.io.File by small code snipnet. First we will look how to create a file in Java

public class FileCreate {
 
  public static void main() {
 
     File file = new File("myfile.txt");  // this creates a new file
  }
 
}


This will create a file named "myfile.txt" in the current directory from where you will execute this code.

Below example displays the usage of other file APIs.

import java.io.File;
import java.io.IOException;
 
 
public class RenameToExample {
 
	public static void main(String[] args) throws IOException {
		File file = new File("myfile.txt");
		file.createNewFile();
		System.out.println(file.getAbsoluteFile());
		System.out.println(file.getCanonicalFile());
		System.out.println(file.getName());
		System.out.println(file.getPath());
		System.out.println(file.canRead());
		System.out.println(file.canWrite());
		System.out.println(file.canExecute());
		System.out.println(file.isDirectory());
		System.out.println(file.isHidden());
		file.renameTo(new File("rename.txt"));
	}
}

[edit] Directories

Directory can be defined as a repository of a number of files and directories at a place. Whenever we create a file which is also a directory then the method isDirectory ( ) will return value as “true” else will give “false” as the result.

Two methods which are directly related to the directory are mkdir ( ) and mkdirs( ). The mkdir( ) method creates a directory and gives value as” True” if it is able tp successfullt create the directory and returns” false” if fails in the operation.

The mkdirs( ) method is used to create a directory for which no path exists, It creates both a directory and all the parents of the directory.

import java.io.File;
 
public class CreateDirectoryExample {
 
	public static void main(String[] args) {
		File file = new File("mydir");
		file.mkdir();
		System.out.println("Is Directory :" + file.isDirectory());
	}
}