
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 7442 Articles for Java

354 Views
The name of the specified file or directory can be obtained using the method java.io.File.getName(). The getName() returns the name of the file or the directory 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 + "JavaProgram" + File.separator, "demo1.txt"); System.out.println("File name: " + file.getName()); } }The output of the above program is as follows −OutputFile name: demo1.txtNow let us understand the above program.The name of the specified ... Read More

258 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 pathname 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 + "JavaProgram" + File.separator, "demo1.txt"); System.out.println("File: " + file); System.out.println("Parent: " + file.getParent()); } }The output of the above program is as follows −OutputFile: C:\JavaProgram\demo1.txt Parent: C:\JavaProgramNow ... Read More

2K+ Views
In this article, we will learn to check if a file or directory is readable. We will use the Java File canRead() method of the java.io package to check if a file or directory is readable in Java. This method returns true if the file specified by the abstract path name can be read by an application and false otherwise. Steps to check if a file or directory is readable Following are the steps to check if a file or directory is readable − First we will import the File class from the java.io package ... Read More

7K+ 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

491 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

179 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

4K+ 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

2K+ Views
In this article, we will learn to remove path information from a filename returning only its file component using Java. 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. Problem Statement Write a program in Java to remove path information from a filename returning only its file component − Input "c:\JavaProgram\demo1.txt" The input is the file path. Output demo1.txt Steps to remove path information Following are the steps to remove ... Read More

155 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

1K+ 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