IO Tutorial
IO stand for input and output.
IO is an API provided by Java that is used to read data from Input Devices and write data into Output Devices.
Using IO you can exchange the information from one device to another device.
IO API exist in java.io package.
IO can be divided into two parts:
- File Handling
- IO Stream
1. File Handling
In File Handling you can learn how to create a folder , file , delete a folder or file etc.
IO package provide a predefine class for file handling that name is File.
File class provide several following methods:
Type | Name & Description |
---|---|
boolean |
canExecute();
Test whether the file is executable or not. |
boolean |
canRead();
Test whether the file is readable or not. |
boolean |
canWrite();
Test whether the file is writable or not. |
boolean |
createNewFile();
This method is used to create a blank file. |
boolean |
delete();
This method is used to delete a file. |
void |
deleteOnExit();
This method is used to delete file when JVM is shutdown. |
boolean |
exists();
Test whether the file is exists or not. |
File |
getAbsoluteFile();
Returns the absolute path of a file. |
String |
getAbsolutePath();
Returns the absolute path of a file. |
File |
getCanonicalFile();
Returns the canonical path of a file. |
String |
getCanonicalPath();
Returns the canonical path of a file. |
long |
getFreeSpace();
Returns the number of unallocated bytes in the partition name by this abstract path name. |
String |
getName();
Returns the file name. |
String |
getParent();
Returns the parent directory of this file name. |
File |
getParentFile();
Returns the parent directory of this file name. |
String |
getPath();
Convert this abstract pathname into a pathname string. |
long |
getTotalSpace();
Returns the total size of this abstract path name. |
long |
getUsableSpace();
Returns the available bytes of this abstract path name. |
boolean |
isAbsolute();
Test whether this path name is absolute or not. |
boolean |
isDirectory();
Test whether this path name is directory or not. |
boolean |
isFile();
Test this is a file or not. |
boolean |
isHidden();
Test this file is hidden or not. |
long |
lastModified();
Returns the time was last modified. |
long |
length();
Returns the length of this file. |
String [] |
list();
Returns the list of files and directories. |
String [] |
list(FilenameFilter filenamefilter);
Returns the list of files and directories based on filters. |
File [] |
listFiles();
Returns the list of files and directories. |
File [] |
listFiles(FileFilter filefilter);
Returns the list of files and directories based on filters. |
File [] |
listFiles(FilenameFilter filenamefilter);
Returns the list of files and directories based on filters. |
File [] |
listRoots();
Returns the list of roots in filesystem. |
boolean |
mkdir();
This method is used to create a folder. |
boolean |
mkdirs();
This method is used to create sub folders. |
boolean |
renameTo(File f);
This method is used to rename a file. |
boolean |
setExecutable(boolean s);
Mark a file as an executable. |
boolean |
setExecutable(boolean s1,boolean s2);
Mark the owner's execute permission for this abstract path name. |
boolean |
setLastModified(long a);
Set the lastmodified time of a file or directory. |
boolean |
setReadable(boolean a);
Mark a file as a readable. |
boolean |
setReadable(boolean a,boolean b);
Mark the owner's read permission for this abstract path name. |
boolean |
setReadOnly();
Mark as read only operations are allowed. |
boolean |
setWritable(boolean s);
Mark a file as a writable. |
boolean |
setWritable(boolean s1,boolean s2);
Mark the owner's write permission for this absolute path name. |
java.net.URI |
toURI();
Constructs a file to URI. |
File Handling Examples
1. How to get list of drives.import java.io.File; public class Test { public static void main(String[] args) { File[] files = File.listRoots(); for (int i = 0; i < files.length; i++) { System.out.println(files[i]); } } }
Result : C:/
D:/
2. How to get list of files from a particular drive.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("C://"); String [] files= file.list(); for (int i = 0; i < files.length; i++) { System.out.println(files[i]); } } }
Result :
$Recycle.Bin
$WINDOWS.~BT
$Windows.~WS
....
....
Windows
3. How to check a file is directory or not.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://deep"); System.out.println(file.isDirectory()); } }
Result : true
4. How to check a file is file or not.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://deep"); System.out.println(file.isFile()); } }
Result : false
5. How to check a file is exist or not.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://deep/filedemo.txt"); System.out.println(file.exists()); } }
Result : true
6. How to create a folder or directory.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://deep"); boolean b=file.mkdir(); System.out.println(b); } }
Result : true
7. How to create a sub folder or directory.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://deep/java"); boolean b=file.mkdirs(); System.out.println(b); //or File f1 = new File("D://main"); boolean b1=f1.mkdir(); File f2=new File(f1,"java"); boolean b2=f2.mkdir(); System.out.println(b1+"\t"+b2); } }
Result : true
true true
8. How to create an empty file.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://deep/filename.txt"); boolean b=file.createNewFile(); System.out.println(b); } }
Result : true
9. How to get file name.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://deep/filename.txt"); String name=file.getName(); System.out.println(name); } }
Result : filename.txt
10. How to get absolute path of file.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://deep/filename.txt"); String path=file.getAbsolutePath(); System.out.println(path); } }
Result : D://deep/filename.txt
11. How to get lastmodified of a file.
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File file = new File("D://filename.txt"); long mod=file.lastModified(); System.out.println(mod); } }
Result : 1585984259029
2 comments:
Good Explanation.....
too much sir
Post a Comment