Found 4336 Articles for Java 8

Display the length of current file in Java

Naveen Singh
Updated on 30-Jul-2019 22:30:24

76 Views

The length of the current file specified by the abstract path name can be displayed using the method java.io.File.length(). The method returns the file length in bytes and does not require any parameters.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("Length of file: " + file.length());       } catch(Exception e) {          e.printStackTrace();       } ... Read More

Java Program to get the File object with the absolute path for the directory or file

Naveen Singh
Updated on 30-Jul-2019 22:30:24

1K+ Views

The method java.io.File.getAbsoluteFile() is used to get the File object with the absolute path for the directory or file. This method requires no parameters.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("The absolute file is: " + file.getAbsoluteFile());    } }The output of the above program is as follows −OutputThe absolute file is:/C:/jdk11.0.2/demo1.javaNow let us understand the above program.The file object with the absolute path is obtained ... Read More

Get the absolute path for the directory or file in Java

Naveen Singh
Updated on 30-Jul-2019 22:30:24

3K+ Views

The method java.io.File.getAbsolutePath() is used to obtain the absolute path name of a file or directory in the form of a string. This method requires no parameters.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("The absolute path name is: " + file.getAbsolutePath());    } }The output of the above program is as follows −OutputThe absolute path name is:/C:/jdk11.0.2/demo1.javaNow let us understand the above program.The absolute pathname of ... Read More

Check whether we can write to a file in Java

Naveen Singh
Updated on 30-Jul-2019 22:30:24

603 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

Naveen Singh
Updated on 30-Jul-2019 22:30:24

440 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

Naveen Singh
Updated on 30-Jul-2019 22:30:24

505 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

Naveen Singh
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

Naveen Singh
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

Naveen Singh
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

Naveen Singh
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

Advertisements