GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - File Class
Selected Reading
Java - File ClassJava provides several methods for file handling like creating, reading, updating and deleting the files. Java File ClassIn Java, the File class is used to reprsent the path of a file or a directory in file system. This class is used for creation of files and directories, file searching, file deletion, etc. We need to import java.io.package before using the File class. Create File ObjectTo work with a file in Java, you first create a File object by passing the file name or path as a string. The File object represents the actual file/directory on the disk. import java.io.File; // Import the File class
File myObj = new File("filename.txt"); // Specify the filename
File Class ConstructorsFollowing is the list of constructors to create a File object.
File Class MethodsOnce you have File object in hand, then there is a list of helper methods which can be used to manipulate the files.
ExampleThe following example demonstrates how to use the File object in a Java program − package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
String[] strs = {"test1.txt", "test2.txt"};
try {
// for each string in string array
for(String s:strs ) {
// create new file
f = new File(s);
// true if the file is executable
boolean bool = f.canExecute();
// find the absolute path
String a = f.getAbsolutePath();
// prints absolute path
System.out.print(a);
// prints
System.out.println(" is executable: "+ bool);
}
} catch (Exception e) {
// if any I/O error occurs
e.printStackTrace();
}
}
}
Consider there is an executable file test1.txt and another file test2.txt is non executable in the current directory. Let us compile and run the above program, This will produce the following result − OutputUpon running the code, you will get the following output - /home/cg/root/2880380/test1.txt is executable: false /home/cg/root/2880380/test2.txt is executable: false Advertisements
| ||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |