File operations (Create, Delete, List files, Filter, Read and Write) in Java

File class (package java.io.*) is an abstract representation of file and directory pathnames. Each operating systems use system-dependent pathname strings to name files and directories(Windows use like this "C:\\DIR_NAME\\File_NAME"  and Linux uses "/usr/local/file_name"). In order to deal each fie in system independent manner , this class presents an abstract, system-independent view of hierarchical pathnames.
How Java makes file handling and associated operations platform independent?
In Java "File class" maintains FileSystem object representing the platform's local file system and also maintains various static fields like separator,pathSeparator which got initialized automatically when File is loaded in JVM. FileSystem object in File Class:- static private FileSystem fs = FileSystem.getFileSystem();

Separator character on UNIX systems is '/' and on Microsoft Windows systems it is '\\'
public static final char separatorChar = fs.getSeparator();
public static final String separator = "" + separatorChar;
// String separator for convenience 
Path separator character on UNIX systems is ':' and  on Microsoft Windows systems it is ';'

public static final char pathSeparatorChar = fs.getPathSeparator();
public static final String pathSeparator = "" + pathSeparatorChar; // String patseparator for convenience
Java ships with very rich I/O classes to support the input and output through bytes stream and file system.In Java File I/O posts we will discuss various operations associated with File class and visit code lines for the same. Before moving ahead with individual examples(creating file, read ,write operations, etc) and understanding various I/O classes available in Java, it needs special mention: File object is created for handling both file and directory and all operation is performed on this File object.When we say File object, we refers to both file(.txt, .xml) or directory.File Class provides API for distinguishing file and directory, however internally both are same(File Object)

Below are sample codes associated with various File operations.Refer each link for detailed explanation and sample Java program for the same.
  1. Create, Rename, Delete a file in Java
  2. Get Meta data of file
  3. Read and write operation in Java
  4. Display all files/directories (File Object) 
  5. Filter files with specific extension (.txt, .doc)
  6. Get and Set File permission
  7. Create temporary file and automatic clean-up

5 Comments

Previous Post Next Post