JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: All file operations.


Description:
Below example shows basic operations on File object.
canRead(): Tests whether the application can read the file canWrite(): Tests whether the application can modify the file createNewFile(): Tests whether the application can modify the file delete(): Deletes the file or directory exists(): Tests whether the file or directory exists. getAbsolutePath(): Returns the absolute pathname string. isDirectory(): Tests whether the file is a directory or not. isHidden(): Tests whether the file is a hidden file or not. list(): Returns an array of strings naming the files and directories in the directory.

Code:
package com.java2novice.files;

import java.io.File;

public class MyFileOperations {

	public static void main(String[] a){
		
		try{
			File file = new File("fileName");
			//Tests whether the application can read the file 
			System.out.println(file.canRead());
			//Tests whether the application can modify the file
			System.out.println(file.canWrite());
			//Tests whether the application can modify the file 
			System.out.println(file.createNewFile());
			//Deletes the file or directory
			System.out.println(file.delete());
			//Tests whether the file or directory exists.
			System.out.println(file.exists());
			//Returns the absolute pathname string.
			System.out.println(file.getAbsolutePath());
			//Tests whether the file is a directory or not.
			System.out.println(file.isDirectory());
			//Tests whether the file is a hidden file or not.
			System.out.println(file.isHidden());
			//Returns an array of strings naming the 
			//files and directories in the directory.
			System.out.println(file.list());
		} catch(Exception ex){
			
		}
	}
}
 Next Program >>

Java File IO Operations Sample Code Examples

  1. All file operations.
  2. Show list of all file names from a folder.
  3. How to get list of all files from a folder?
  4. Filter the files by file extensions and show the file names.
  5. How to read file content using byte array?
  6. How to read file content line by line in java?
  7. How to read property file in static context?
  8. How to read input from console in java?
  9. How to get file list from a folder filtered by extensions?
  10. How to get file URI reference?
  11. How to store and read objects from a file?
  12. How to create and store property file dynamically?
  13. How to store property file as xml file?
  14. How to get file last modified time?
  15. How to convert byte array to inputstream?
  16. How to convert inputstream to reader or BufferedReader?
  17. How to convert byte array to reader or BufferedReader?
  18. How to set file permissions in java?
  19. How to read a file using BufferedInputStream?
  20. How to create temporary file in java?
  21. How to write or store data into temporary file in java?
  22. How to delete temporary file in java?
  23. How to write string content to a file in java?
  24. How to write byte content to a file in java?
Knowledge Centre
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.