Found 9313 Articles for Object Oriented Programming

Check whether we can write to a file in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

605 Views

The method java.io.File.canWrite() is used to check whether the file can be written to in Java. This method returns true if the file specified by the abstract path name can be written to by an application and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("The file can be written to? " + file.canWrite());       } catch(Exception e) { ... Read More

Check whether the file can be read in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

442 Views

The method java.io.File.canRead() is used to check whether the file can be read in Java. This method returns true if the file specified by the abstract path name can be read by an application and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("The file can be read? " + file.canRead());       } catch(Exception e) {       ... Read More

Check whether the file is hidden or not in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

506 Views

The method java.io.File.isHidden() is used to check whether the given file specified by the abstract path name is a hidden file in Java. This method returns true if the file specified by the abstract path name is hidden and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("Is file hidden? " + file.isHidden());       } catch(Exception e) {   ... Read More

Check whether the given file is an existing file in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

99 Views

The method java.io.File.isFile() is used to check whether the given file specified by the abstract path name is an existing file in Java. This method returns true if the file specified by the abstract path name is a file and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("Is file? " + file.isFile());       } catch(Exception e) {   ... Read More

Check whether a file is a directory in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

4K+ Views

The method java.io.File.isDirectory() checks whether a file with the specified abstract path name is a directory or not. This method returns true if the file specified by the abstract path name is a directory and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("Is directory? " + file.isDirectory());       } catch(Exception e) {          e.printStackTrace();   ... Read More

Java Program to check whether a file exists or not

Samual Sam
Updated on 30-Jul-2019 22:30:24

175 Views

The method java.io.File.exists() is used to check whether a file exists or not. This method returns true if the file specified by the abstract path name exists and false if it does not exist.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          System.out.println("File exists? " + file.exists());       } catch(Exception e) {          e.printStackTrace();       } ... Read More

Get the String representation of the current File object in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

156 Views

The string representation of the current file object can be obtained using the method java.io.File.toString(). This method returns the abstract path name in the string form.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");       System.out.println("File: " + file.toString());    } }The output of the above program is as follows −OutputFile: C:/jdk11.0.2/demo1.javaNow let us understand the above program.The abstract path name is printed in the string form using the ... Read More

Java Program to get the name of the parent directory of the file or directory

Samual Sam
Updated on 30-Jul-2019 22:30:24

561 Views

The name of the parent directory of the file or directory can be obtained using the method java.io.File.getParent(). This method returns the parent directory path name string or null if there is no parent named.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");       System.out.println("File: " + file);       System.out.println("Parent: " + file.getParent());    } }The output of the above program is as follows −OutputFile: C:/jdk11.0.2/demo1.java Parent: ... Read More

Check if the File object refers to an absolute pathname in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

313 Views

The method java.io.File.isAbsolute() is used to check if the file object refers to an absolute pathname. This method returns true if the abstract path name is absolute and false if the abstract path name is not absolute.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");       System.out.println(file.isAbsolute());    } }The output of the above program is as follows −OutputfalseNow let us understand the above program.The isAbsolute() method is used ... Read More

Get the name of the file and path in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

6K+ Views

The name of the file and the name of the path can be obtained using the methods java.io.File.getName() and java.io.File.getPath() respectively. The getName() returns the name of the file or the directory. The getPath() returns the abstract pathname in the form of a pathname string.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java");       System.out.println("File name: " + file.getName());       System.out.println("Path name: " + file.getPath());    } ... Read More

Advertisements