- Trending Categories
- 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 34489 Articles for Programming
6K+ Views
A temporary file can be created using the method java.io.File.createTempFile(). This method requires two parameters i.e. the prefix to define the file name and the suffix to define the file extension. It also returns the abstract path name for the temporary file created.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) throws Exception { File file = File.createTempFile("temp", null); System.out.println(file.getAbsolutePath()); file.deleteOnExit(); } }The output of the above program is as follows −OutputC:\Users\amit_\AppData\Local\Temp\temp6072597842246154962.tmpNow let us understand the above ... Read More
6K+ Views
The method java.lang.System.getProperty() is used to obtain the system property. This system property is specified by the key which is the parameter for the method. To obtain the current working directory, the key used is user.dir.A program that demonstrates this is given as follows −Example Live Demopublic class Demo { public static void main(String[] argv) throws Exception { String currentDirectory = System.getProperty("user.dir"); System.out.println("The current working directory is " + currentDirectory); } }The output of the above program is as follows −OutputThe current working directory is c:\JavaProgramNow let us understand the above program.The current ... Read More
3K+ Views
The method java.io.File.list() is used to obtain the list of the files and directories in the specified directory defined by its path name. This list of files is stored in a string array. If the length of this string array is greater than 0, then the specified directory is not empty. Otherwise, it is empty.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 directory = new File("C:\JavaProgram"); if (directory.isDirectory()) { String[] files = directory.list(); ... Read More
314 Views
The method java.io.File.renameTo() is used to rename a file or directory. This method requires a single parameter i.e. the name that the file or directory is renamed to and it returns true on the success of the renaming 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) { try { File file1 = new File("demo1.txt"); File file2 = new File("demo2.txt"); file1.createNewFile(); file2.createNewFile(); ... Read More
749 Views
A file can be set to read-only by using the method java.io.File.setReadOnly(). This method requires no parameters and it returns true if the file is set to read-only and false otherwise. The method java.io.File.canWrite() is used to check whether the file can be written to in Java and if not, then the file is confirmed to be read-only.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) { boolean flag; try { File file = new File("demo1.txt"); ... Read More
233 Views
A file or directory can be deleted on termination of the program i.e. after the virtual machine terminates using the method java.io.File.deleteOnExit(). This method requires no parameters and it does not return a value.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: " + file); file.deleteOnExit(); } catch(Exception e) { e.printStackTrace(); ... Read More
154 Views
The contents of a directory can be obtained using the method java.io.File.listFiles(). This method requires no parameters and it returns the abstract path names that specify the files and directories in the required directory.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo { public static void main(String[] args) { File directory = new File("C:\JavaProgram"); File[] contents = directory.listFiles(); for (File c : contents) { if(c.isFile()) System.out.println(c + " is a file"); ... Read More
245 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
208 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
The method java.io.File.canRead() is used 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.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