Found 34489 Articles for Programming

Determine if file or directory exists in Java

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

6K+ Views

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

Check for file or directory in Java

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

388 Views

The method java.io.File.isFile() is used to check whether the given file is an existing file in Java. Similarly, the method java.io.File.isDirectory() is used to check whether the given file is a directory in Java. Both of these methods require no parameters.A program that demonstrates this is given as follows −Examplepublic class Demo {    public static void main(String[] args) {       try {          File file = new File("demo1.txt");          file.createNewFile();          boolean fileFlag = file.isFile();          if (fileFlag) {             ... Read More

Create directories recursively in Java

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

105 Views

The method java.io.File.mkdirs() is used to create the specified directories, including the necessary parent directories. This method requires no parameters and it returns true on the success of the directories creation or 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) {       String recursiveDirectories = "D:\a\b\c\d";       File file = new File(recursiveDirectories);       boolean flag = file.mkdirs();       System.out.println("The directories are created recursively? " + flag);    } }The output of the above program is as follows ... Read More

Java Program to strip a filename of its extension after the last dot

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

3K+ Views

The method removeExtension() is used to strip a filename of its extension after the last dot. This method requires a single parameter i.e. the file name and it returns the file name without its extension.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static String removeExtension(String fname) {       int pos = fname.lastIndexOf('.');       if(pos > -1)          return fname.substring(0, pos);       else          return fname;    }    public static void main(String[] args) {       System.out.println(removeExtension("c:\JavaProgram\demo1.txt")); ... Read More

Java Program to remove path information from a filename returning only its file component

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

1K+ Views

The method fileCompinent() is used to remove the path information from a filename and return only its file component. This method requires a single parameter i.e. the file name and it returns the file component only of the file name.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static String fileComponent(String fname) {       int pos = fname.lastIndexOf(File.separator);       if(pos > -1)          return fname.substring(pos + 1);       else          return fname;    }    public static void main(String[] ... Read More

Java Program to remove file information from a filename returning only its path component

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

85 Views

The method pathCompinent() is used to remove the file information from a filename and return only its path component. This method requires a single parameter i.e. the file name and it returns the path component only of the file name.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static String pathComponent(String fname) {       int pos = fname.lastIndexOf(File.separator);       if (pos > -1)          return fname.substring(0, pos);       else          return fname;    }    public static void main(String[] ... Read More

Determine if two filename paths refer to the same File in Java

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

700 Views

The method java.io.File.equals() is used to find if the two file names refer to the same File in Java. This method requires a single parameter i.e.the file object that is to be compared to the other file object. It returns if the file objects are same 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 file1 = new File("demo1.txt");          File file2 = new File("demo2.txt");          boolean flag ... Read More

Get an Absolute Filename Path from a Relative Filename with Path in Java

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

237 Views

The method java.io.File.getAbsoluteFile() can be used to acquire the absolute filename path from a relative filename with path in Java. This method requires no parameters. It returns the file that is defined by the path name.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] argv) throws Exception {       File file = new File("c:" + File.separator + "JavaProgram" + File.separator + "demo1.txt");       file = file.getAbsoluteFile();       System.out.println(file);    } }The output of the above program is as follows −Outputc:\JavaProgram\demo1.txtNow let us ... Read More

Get an Absolute Filename Path from a Relative Filename Path in Java

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

182 Views

The method java.io.File.getAbsoluteFile() can be used to acquire the absolute filename path from a relative filename path in Java. This method requires no parameters. It returns the file that is defined by the path name.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] argv) throws Exception {       File file = new File("demo1.txt");       file = file.getAbsoluteFile();       System.out.println(file);    } }The output of the above program is as follows −Outputc:\JavaProgram\demo1.txtNow let us understand the above program.The method java.io.File.getAbsoluteFile() is used to ... Read More

Compare two file paths in Java

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

725 Views

Two file paths can be compared lexicographically in Java using the method java.io.File.compareTo(). This method requires a single parameter i.e.the abstract path name that is to be compared. It returns 0 if the two file path names are equal.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 file1 = new File("C:/File/demo1.txt");       File file2 = new File("C:/File/demo1.txt");       if (file1.compareTo(file2) == 0) {          System.out.println("Both the paths are lexicographically equal");       } else ... Read More

Advertisements