File handling implies how to read from and write to file in java.
• Java provides the basic I/O package for reading and writing streams.
• File class from the java.io package, allows do all input and output operations with different formats of files.
• In order to use the File class, you need to create an object of the class and specify the filename or directory name.
File class in java is particularly useful for retrieving information about files or directories fromdisk.
• File class is used to perform the following activities:
• Check if a file exists
• Before you can do anything with the file system or File class, you must obtain a File instance.
• Here is how that is done: Example: File file = new File("DriveLetter\\folderName\\fileName.txt"); Note: Objects of class File do not open files or provide any file-processing capabilities
• Createfile/folder • Read the length of a file • Rename or move a file
• Delete a file • Check if path is file or director.
Examples: File file = new File("D\\folderName\\fileName.txt");
boolean fileExists = file.exists(); //to check if a fileexists,
long length = file.length(); //To read the length of a file in bytes,
file.renameTo (new File("D\\folderName\\newfileName.txt"); // to rename the file
file.delete();
//To delete a file
//To check if a file is a file or a directory and list files in the directory File file = new File("D\\folderName"); boolean isDirectory = file.isDirectory();
Comments
Post a Comment