- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
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
83 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
672 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
234 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
176 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
711 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
1K+ Views
The double slash in the file path is required as to create the ’\’ character , another ‘\’ needs to be added to escape it. 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:\jdk11.0.2\demo1.txt"); System.out.println(file); } }The output of the above program is as follows −OutputC:\jdk11.0.2\demo1.txtNow let us understand the above program.The file path of the file is provided with double slash and then it is printed. A code snippet that demonstrates this is ... Read More
3K+ Views
The path for a file can be obtained using the method java.io.File.getPath(). This method returns the abstract pathname in the form of a pathname string and it 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("Path = " + file.getPath()); } }The output of the above program is as follows −OutputPath = C:/jdk11.0.2/demo1.javaNow let us understand the above program.The path name of the file ... Read More
2K+ Views
The method java.io.File.getAbsolutePath() is used to obtain the absolute path of a file 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 the file is ... Read More
520 Views
The file extension is the suffix that is attached to the computer file and it denotes the format of the file. A program that demonstrates getting the file extension name is given as follows −Example Live Demoimport java.io.File; public class Demo { private static String fileExtension(File file) { String name = file.getName(); if(name.lastIndexOf(".") != -1 && name.lastIndexOf(".") != 0) return name.substring(name.lastIndexOf(".") + 1); else return ""; } public static void main(String[] args) { File file = new File("demo1.txt"); ... Read More